Quick Unity Questions.

Aug 8, 2011 at 4:15 PM

Hi 

I have some very quick Unity questions. I am implementing a very basic scenario where I am passing in the interface implementation to a Service class (Constructor Injection). 

 

MyService service = new MyService(myContainer.Resolve<ITestRepository>()); 

I have the code working however, I have to ensure that the myContainer is initialised with the values loaded from the config file. Now I can understand that I can use the singleton pattern here and always return an initalised instance of the container.

Is that the best way? Do I have to keep a reference of the UnityContainer and always use that? 

Finally, I read somewhere that if the pluggable architecture is not required then it is best to register the types in code. Is that correct? Is loading from the config that slow?

 

Thanks

Amar

Aug 8, 2011 at 5:07 PM

Hi Amar,

I'm not sure if I fully understand your question, so please bear with me as I'll give you a longer answer and that would hopefully provide you some direction.

If all your classes have dependencies on abstractions (interfaces, etc) and are properly configured with the container, then you should only require the container to resolve the starting class of your program and from there on let Unity resolve all the dependencies. 
Using your example, you have:

class MyService : IMyService
  ctor(ITestRepository repo)
class MainProgram 
  ctor (IMyService aService)
  Run()

and the correspoding registrations, say tucked away in a Bootstrap method
unity.RegisterType()
unity.RegisterType()
unity.RegisterType()

then in the entry point in your app, usually Program.cs you would have code similar to the following:

void Main(string[] args) {
  var container = Bootstrap();
  var main = container.Resolve();
  main.Run();
}

 And then everything will fall nicely into place. That does not require that you keep a UnityContainer around beyond the starting point.

That might not be always possible and you might need to resolve instances at various points in your code rather than have them injected (which BTW indicates a bit of a code smell). If that's the case, you might want to look into the Service Locator Patter, and perhaps use the CommonServiceLocator project to hide the UnityContainer behind it.

Aug 9, 2011 at 9:19 AM

Thanks for the reply Philip. What I meant was, that each type has to be registered somehow in the container before the resolve can be called. This means that the most efficient way of working with unity is to only intialise the container once and then keep a reference of it for future use, hence the singleton pattern.

I guess Bootstrap method is the one what I was talking about

static IUnityContainer container = null;

public static void Bootstrap()

{

lock(this)

{

if(container == null)

{

container = new UnityContainer();

//Register types either in code of from Config

}

}

return container;

}

haha I like your word, code smell. I have looked into the Service Locator Pattern which you mentioned and I think I will use that in my project. 

Thanks.