I'm new to Unity, but this question is more generic to IoC, and I’m pretty new to implementing IoC as a whole. I have VS2010 solution with this project structure (simplified slightly):
All project reference the DomainModel project.
Repository references the DataAccess project.
Service Layer references the Repository project.
WebUI references the Service project & the Unity assemblies.
I have Unity configured to inject all my service types correctly in the WebUI (global.asax via a custom UnityControllerFactory.cs). But how do I configure Unity in the service layer to inject the repository objects?
I DON’T want to reference the Repository project from the WebUI to ensure during development no one shortcuts and bypass the Service layer.
Couple Ideas I have (not sure if it will solve it):
Any direction would be greatly appreciated, specifically to how to configure Unity for the service layer / Repository, but also in general about the project.
Add a bootstrapper of some sort in the Service project. Then reference the bootstrapper in the WebUI.
One way to do this would be to write a small Unity extension. Something like this:
public class ServiceLayerBootstrap : UnityContainerExtension
{
protected override void Initialize()
{
Container.RegisterType<IRepository, WhateverRepositoryImplementation>();
// etc.
}
}
Then, in the web project where you create the container and initialize it, do this:
var container = new UnityContainer()
.AddNewExtension<ServiceLayerBootstrap>();