What is the difference in registering a common interface with a specific type in a common interface without a type?

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?

+4
source share
1 answer

. , , .

, :

interface IRepository<T>{}

class Repository<T>:IRepository<T>{}

IoC :

container.RegisterType(typeof (IRepository<>), typeof (Repository<>));

container.RegisterType(typeof (IRepository<User>), typeof (Repository<User>));
container.RegisterType(typeof (IRepository<Product>), typeof (Repository<Product>));
container.RegisterType(typeof (IRepository<Customer>), typeof (Repository<Customer>));
container.RegisterType(typeof (IRepository<CustomerAddress>), typeof (Repository<CustomerAddress>));
// and so on

,

+1

Source: https://habr.com/ru/post/1599848/


All Articles