last day (14 days later) » 

10:57 AM
1
A: Unity container can't resolve my class

bbonchYou should register container in the Global.asax.cs public class Global : HttpApplication { private void Application_Start(object sender, EventArgs e) { IUnityContainer container = new UnityContainer(); container.RegisterType<IUserService, UserService

 
Thanks. Will make that change. Can I still ref reference "container" from my controller? And all other controllers?
Now my Global.asax file needs reference to all my layers. Is that right?
 
@Craig: "Can I still ref reference "container" from my controller? And all other controllers?". You should absolutely not reference the controller from anywhere in the application but the start up path. This is a bad practice.
 
Thanks @Steven - So the container shouldn't be in the global.asax? Where should the container reside?
 
@Craig the global.asax is exactly where your container should be. The global.asax is the startup path.
 
Thanks @Steven - I'm just not sure how my controllers, then, would be able to access the container object.
 
10:57 AM
@Craig that's the whole idea of Dependency Injection. They never access the container. They should get all the dependencies they need through their consrructor.
 
Thanks again. What you say is making sense, but if the container object is in the global.asax.cs file.... how do I do : var service = _container.Resolve<IUserService>();
I need to have a reference to _container in the controller.
So within the Controller - how should I be accessing the Container object which has been instatiated in the global.asax?
 
Your controller should have a constructor with a parameter of type IUserService. Unity will analyse the controller's constructor and will inject that service for you. The whole idea of a DI container is built around this concept: you statically define a component's dependencies as constructor arguments and the container will build up a complete graph of nested dependencies.
 
Ah, right! I think I understand now. So the controller doesn't 'initiate' the container. It's declared in the global file... and then when it 'sees' IUserService, it'll take care of the injection. I'll try that now. Thanks.
 
The controller shouldn't know of the existence of the container. That would make your code hard to test, hard to change, hard to maintain.
And to make things worse, DI containers are optimized for having a single insrance for the whole application. Creating a new container per request can have severe performance implications in a production app.
 
11:17 AM
My glbl file:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);

IUnityContainer container = new UnityContainer();
container = new UnityContainer();
container.RegisterType<IUserService, UserService>();
container.RegisterType<IUserLogic, UserLogic>();
container.RegisterType<IData, Data>();
container.RegisterType<ILog, Logger.Log>();
container.RegisterType<ISnowballLogic, SnowballLogic>();

}
And my controller:
public class HomeController : Controller
{

private IUserService _userService;
private ISnowballLogic _logicService;

public HomeController(IUserService userService, ISnowballLogic logicService)
{

}
But I'm unsure how to use the IUserService. Where does my 'Resolve' go?
Currently I do:
var service = _container.Resolve<IUserService>();
That should use my _userService - but now sure how to resolve it in the controller.
 
12:09 PM
Please take a look at bbonch's answer. It shows you how to correctly set things up. You need to hook up your DependencyResolver. This allows forwarding the creation of controllers to Unity.
Once that is set up, Unity will create your controller and will create the IUserService dependency with its dependencies, recursively.
And don't expect any magic. You will have to store the incoming dependencies in the controller's constructor in the private fields.
 

last day (14 days later) »