简体   繁体   English

将带有已定义类型的通用接口注册到没有类型的通用接口有什么区别?

[英]What is the difference in registering generic interface with defined type into generic interface without type?

I have a problem on how to register my generic interface implemented into multiple class in my UnityContainer and I figured it out using this code. 我有一个问题,如何在我的UnityContainer中注册我的通用接口实现到多个类,我用这个代码想出来了。

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));

Above code is working as expected but out of my curiosity I also tried to experiment a little bit and register it without a defined type like code below and it seems working 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 are: 现在我的问题是:

  1. What is/are the difference between the two aside from readability of the code? 除了代码的可读性之外,两者之间有什么区别?

  2. Is their any implication if I choose one of it? 如果我选择其中一个,这是否有任何影响?

  3. What is the best practice if I have this code? 如果我有这个代码,最佳做法是什么?

Second method named open generic registration. 第二种方法名为开放通用注册。 This method useful when your concrete classes also generic then you don't need to register a type for each classes. 当您的具体类也是通用的时,此方法很有用,因此您不需要为每个类注册一个类型。

For example if you have: 例如,如果你有:

interface IRepository<T>{}

and

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

simply in IoC you could write one simple line: 只需在IoC中你就可以编写一个简单的行:

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

instead of 代替

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

As you can see without open generic registration we need register separately for each item 如您所见,如果没有开放式通用注册,我们需要为每个项目单独注册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM