I would like to use Castle Windsor for dependency injection for my solution consisting of the following projects:
Use installers. from the global.asax you can call:
_container.Install(FromAssembly.InDirectory(new AssemblyFilter(HttpRuntime.BinDirectory, "YourNamespaceToScan*.dll")));
In your business, DA and Model projects you can then add installers to install dependancies for each:
public class ServicesInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
...);
}
}
You should configure the DI container in the MVC project. This is where everything comes into live. This where all the assemblies must be referenced including the data access layer of course (without referencing a concrete data access your MVC application simply cannot work). So the MVC application knows all about the other layers. Application_Start in Global.asax is a good place to configure a DI container.
Create and place Windsor installer classes (IWindsorInstaller interface implementations) 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 install your dependencies:
protected virtual void Application_Start()
{
new WindsorContainer().Install(FromAssembly.InDirectory(new AssemblyFilter(HttpRuntime.BinDirectory)));
}
You have to ask yourself one thing: "do I really need 4 different assemblies for my MVC project?" You should break the code into assemblies according to deployment boundaries, not just based on some vague notion that Model code should be physically separate from Business code & similar. You should use namespaces for such things instead of separate projects.
Here's a good article on this issue: Advices on partitioning code through .NET assemblies by Patrick Smacchia.