I have been using Unity to create classes with constructor injection like this:
public class ProductsController : BaseController
{
public ProductsController(
IService<Account> accountService,
IService<Product> productService)
{
_account = accountService;
_product = productService;
}
Bootstrapper:
private static IUnityContainer BuildUnityContainer()
{
CloudStorageAccount storageAccount;
storageAccount = CloudStorageAccount.FromConfigurationSetting("DEV_DataConnectionString");
var container = new UnityContainer();
container.RegisterType< IService<Account>, AccountService >();
container.RegisterType< IService<Product>, ProductService >();
container.RegisterType<IAzureTable<Product>, AzureTable<Product>>(new InjectionConstructor(storageAccount, "TestProducts"));
return container;
}
Now I need to create an instance of a service instance "on the fly" from within my method. Something like this:
public void Update(string serviceClassName) {
var serviceClass = new container.Resolve<IService<Product>>();
But there are some things I don't understand.
- First of all do I need to create a new container? I assume I need to reference the container I already created but I can't find any reference on how to do that.
- Second here I have hardcoded Product but what I need is to be able to pass in the word "Product" as a parameter and then have it constructed.
How can I create my class ?
Update
Not tested yet but I believe the solution for naming of my class may require me to register my class as follows:
container.RegisterType< IService<Product>, ProductService >("productService");