Castle Windsor: register components in multiple projects in a solution

I would like to use Castle Windsor to inject dependencies for my solution consisting of the following projects:

  • Mvc [ASP.NET MVC 3 Web Application]: presentation level (depends on business and models)
  • Business [Class Library]: Business Layer (Depends on DataAccess and Models)
  • DataAccess [Class Library]: data access level (varies by model)
  • Models [Class Library]: model layer

There is a class in the business layer called PostService that implements IPostService , which manages blog posts. PostsController PostsController depends on IPostService . However, the PostService (corresponding specific implementation) is dependent on the IPostRepository .

Where can I configure Castle Windsor to return an instance of PostRepository to resolve IPostRepository ? The Mvc project does not know about the DataAccess project. Thus, I cannot configure component bindings in global.asax or elsewhere in Mvc.


[Refresh] Dependency Chart

Now that I have found a solution (thanks again, Darin Dimitrov!), I would like to share with you the current dependency diagram.

Project dependencies

+3
source share
4 answers

You must configure the DI container in the MVC project. Here everything comes to life. This is where all assemblies should be specified, including, of course, the level of data access (without reference to specific data, access to your MVC application simply cannot work). This way the MVC application knows all about the other layers. Application_Start in Global.asax is a good place to set up a DI container.

0
source

Create and place the Windsor installer classes (implementations of the IWindsorInstaller interface) in your class libraries, for example:

 public class PostRepositoryInstaller: IWindsorInstaller { public void Install(IWindsorContainer container, IConfigurationStore store) { var allTypesFromBinDir = AllTypes .FromAssemblyInDirectory(new AssemblyFilter(HttpRuntime.BinDirectory)); container.Register(allTypesFromBinDir .BasedOn<IPostRepository>() .WithService.FromInterface() .Configure(c => c.LifeStyle.Transient)); } } 

Then in your Global.asax application, install your dependencies:

 protected virtual void Application_Start() { new WindsorContainer().Install(FromAssembly.InDirectory(new AssemblyFilter(HttpRuntime.BinDirectory))); } 
+9
source

Use installers. from global.asax you can call:

 _container.Install(FromAssembly.InDirectory(new AssemblyFilter(HttpRuntime.BinDirectory, "YourNamespaceToScan*.dll"))); 

In your DA and Model business projects, you can add installers to install dependencies for each of them:

  public class ServicesInstaller : IWindsorInstaller { public void Install(IWindsorContainer container, IConfigurationStore store) { container.Register( ...); } } 
+1
source

You should ask yourself: "Do I really need 4 different builds for my MVC project?" You must break the code into assemblies according to the boundaries of the deployment , and not just based on the vague idea that the model code should be physically separated from the business code, etc. You should use namespaces for such things instead of separate projects.

Here's a good article on this issue: Tips for partitioning code through .NET assemblies by Patrick Smack.

0
source

Source: https://habr.com/ru/post/902722/


All Articles