I have a method which constructs dependencies using Unity. Currently, all of the calls do pretty much the same thing: Registers 2 repository object and 2 'App Part' objects. My issue is, there's a lot of them and the method has become massive. I've generalised the calls but here's an example:
container.RegisterType<IRepositoryOneDataAccess, RepositoryOneDataAccess>();
container.RegisterType<IRepositoryTwoDataAccess, RepositoryTwoDataAccess>();
container.RegisterType<IAppPart, FirstAppPart>("FirstAppPart.TestName");
container.RegisterType<IAppPart, SecondAppPart>("SecondAppPart.TestName");
I though that this could be split out to reduce the code in the method which registers the types with the container.
Firstly I moved the bottom 2 calls to another method since they both register to the same interface, I came up with this method:
private void RegisterParts<TPartOne, TPartTwo>(string firstAppPartName, string secondAppPartName) where TPartOne : IAppPart where TPartTwo : IAppPart
{
container.RegisterType<IAppPart, TPartOne>(firstAppPartName);
container.RegisterType<IAppPart, TPartTwo>(secondAppPartName);
}
This seems to work but I'm not sure this is the best way to do it. I would also like to move the other calls out into another method preferably the same as this one I've created but I'm not sure how to do it or even if it's possible.
Is there a better way of refactoring this?
IRepository
type names somehow related to the "First" and "Second"IAppPart
s ? At first sight those names sound really odd - it's hard to help not knowing exactly what they are supposed to do though.