I have set up a bootstrapper as follows:
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
container.RegisterType<IService<Account>, AccountService>();
container.RegisterControllers();
return container;
}
My controller looks like this:
public AccountsController(IService<Account> accountService) {
_account = accountService;
}
When called it correctly sets up _account from the accountService which looks like this:
public class AccountService : BaseService, IService<Account>
{
public AccountService() { base.Initialize();
_accountRepository = StorageHelper.GetTable<Account>();
_productRepository = StorageHelper.GetTable<Product>();
}
You can see here that the AccountService depends on setting up two repositories. In Unity is there some way that I can also specify this dependence so I don’t have to code in StorageHelper.GetTable();
Update:
I added the following code after suggestion below:
public AccountService(
IAzureTable<Account> accountRepository,
IAzureTable<Product> productRepository) {
//base.Initialize();
_accountRepository = accountRepository;
_productRepository = productRepository;
}
Here’s the original code:
public static IAzureTable<T> GetTable<T>() where T : TableServiceEntity
{
return new AzureTable<T>(GetStorageAccount(), TableNames[typeof(T)]);
}
Then tried to set up these types in the bootstrapper as follows:
container.RegisterType<IAzureTable<Account>, AzureTable<Account>(GetStorageAccount(), "TestAccounts")>();
container.RegisterType<IAzureTable<Product>, AzureTable<Product>(GetStorageAccount(), "TestProducts")>();
I guess there’s something I don’t understand as this produced a lot of syntax errors. Am I doing it wrong here?
Yes, treat those two repositories as constructor arguments and Unity will wire them up for your as specified in your bootstrapper:
In your bootstrapper you should be able to specify where exactly it will get those arguments from using the InjectionConstructor (a good example here) (assuming they are different types, if they are different types you can just wire them up and Unity will sort them out).
Edit for new info
When you register your generic types with Unity, wrap them in a TypeOf statement, since that seems to be how you get around generics with Unity (source: MSDN)