I had a problem with how to register my common interface implemented in several classes in UnityContainer , and I understood using this code.
container.RegisterType(typeof (IRepository<Location>), typeof (LocationRepository));
container.RegisterType(typeof (IRepository<ProductDto>), typeof (ProductRepository));
container.RegisterType(typeof (IRepository<Customer>), typeof (CustomerRepository));
container.RegisterType(typeof (IRepository<CustomerAddress>), typeof (CustomerAddressRepository));
container.RegisterType(typeof (IRepository<CustomerTerms>), typeof (CustomerTermRepository));
container.RegisterType(typeof (IRepository<AmountDto>), typeof (ProductListPricingRepository));
container.RegisterType(typeof (IRepository<Contact>), typeof (ContactRepository));
It works as expected on the code, but out of my curiosity I also tried to experiment a little and register it without a specific type, for example, the code below, and it seems the same as the first one.
container.RegisterType(typeof (IRepository<>), typeof (LocationRepository));
container.RegisterType(typeof (IRepository<>), typeof (ProductRepository));
container.RegisterType(typeof (IRepository<>), typeof (CustomerRepository));
container.RegisterType(typeof (IRepository<>), typeof (CustomerAddressRepository));
container.RegisterType(typeof (IRepository<>), typeof (CustomerTermRepository));
container.RegisterType(typeof (IRepository<>), typeof (ProductListPricingRepository));
container.RegisterType(typeof (IRepository<>), typeof (ContactRepository));
Now my question is:
What is the difference between these two aspects besides code readability?
Is there any implication of them if I choose her?
What is the best practice if I have this code?
source
share