We are planning to use Unity in our project to dynamically load objects/types residing in different assemblies. To register the types we are using the .config file. I have written a small sample app to test this out and all works well. Following is the sample code:
/* App.config
...
<container name="xyz">
<register type="IOperations" mapTo="COperations">
<lifetime type="external" />
</register>
<register type="ITest" mapTo="CTest">
<lifetime type="external" />
</register>
</container>
...
*/
public class CA
{ }
public class CB
{ }
public interface IMyOperations : IDisposable
{
void DoSomething(string str);
}
public interface ITest : IDisposable
{
void DoSomethingElse(string str);
}
class Program
{
static void Main(string[] args)
{
string containerName = "xyz";
using ( IUnityContainer container = new UnityContainer() )
{
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Configure(container, containerName);
IMyOperations myOps = container.Resolve<IMyOperations>();
myOps.DoSomething("Hello");
myOps.Dispose();
ITest test = container.Resolve<ITest>();
test.DoSomethingElse("World!");
test.Dispose();
}
}
}
public class COperations
{
private IUnityContainer container;
public COperations(IUnityContainer container)
{
this.container = container;
}
public void DoSomethingElse(string str)
{
// based on runtime values create an instance of class CA or CB and register them with the container
if (str.Length > 10)
container.RegisterInstance<CA>("InstanceCA", new CA());
else
container.RegisterInstance<CB>("InstanceCB", new CB());
}
public void Dispose()
{ }
}
public class CTest
{
private IUnityContainer container;
public CTest(IUnityContainer container)
{
this.container = container;
}
public void DoSomethingElse(string str)
{
// based on runtime values get the registered instance of class CA or CB
if (str.Length > 100)
container.Resolve<CA>("InstanceCA");
else
container.Resolve<CB>("InstanceCB");
}
public void Dispose()
{ }
}
I am also creating new objects (of class CA and CB in the code above) in the container-resolved-type and registering those with the container using ResisterInstance(). To do this I am changing the the signature of ctor of my container-resolved-type looks like the following:
public COperations(IUnityContainer container)
I am not sure if this is the right approach. Is there a better way to use the container to register instance from the container-resolved-type object? I am trying to share a few objects among each other, which I can know only at run time. Also this makes my components hardwired with Unity and not helpful in case we decide to move to another container at a later point of time.
I’d suggest creating a Registration Factory if you want to register things in code. You’d then be passing the Registration Factory via the constructor and it would register your objects. This would allow you to decouple from Unity.
So:
you’d obviously need to implement IRegistrationFactory. All this would be is a class that takes a IUnityContainer in its constructor and then implemented the interface.
If you decide to change from Unity to another DI container all you need to do is tweak IRegistrationFactory to handle the new DI container.
Also, if you’re registering instances you can do it like this:
_container.RegisterType(new ContainerControlledLifetimeManager());
this will always return the same instance of the object so it like a RegisterInstance. The advantage of this approach is that Unity will resolve any of CAs dependencies for you, whereas if you use RegisterInstance you have to manually do this.