具有继承接口的Unity解析对象 [英] Unity Resolving objects with inherited interfaces

查看:110
本文介绍了具有继承接口的Unity解析对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下两个类.

Consider the two classes below.

public abstract class BaseService<tmodel> : IService<tmodel> where TModel : IModel

public class AccountCodeService : BaseService<accountcode> ,IAccountCodeService
 
BaseService implements IService<tmodel>.



由于AccountCodeSevice继承自BaseService,因此它实现了IService< tmodel>接口以及IAccountCodeService

我现在在Unity中注册AccountCodeService:



Since AccountCodeSevice inherits from BaseService, it implements the IService<tmodel> interface as well as IAccountCodeService

I now register AccountCodeService in Unity:

container.RegisterType<iaccountcodeservice,>();



然后解决:



Then resolve:

_accountCodeService = container.Resolve<iaccountcodeservice>();



问题:
_accountCodeService不公开IService< tmodel>中的元素.

我试过了:



The Problem:
_accountCodeService does not expose the elements in IService<tmodel>.

I tried:

container.RegisterType<iaccountcodeservice,>().RegisterType<iservice><accountcode>,AccountCodeService>();

>

然后解决:



Then resolve:

_accountCodeService = container.Resolve<iaccountcodeservice>();



相同的问题:
_accountCodeService不公开IService< tmodel>中的元素.

为了理智起见,我尝试了以下操作:



Same issue:
_accountCodeService does not expose the elements in IService<tmodel>.

For sanity sake I tried:

var o = container.Resolve<iservice><accountcode>>();


验证o是否公开IService< tmodel>中的元素.

我如何获得Unity来以暴露IService< tmodel>中的元素的方式解析类.和IAccountCodeService?

注意:我发现如果我复制了IService< tmodel> IAccountCodeService中的属性"属性,则应用会先进行编译,然后再将IService< tmodel>元素被公开并且可以被调用.但是,我不想实现这种方式.


Verified that o exposes the the elements in IService<tmodel>.

How do I get Unity to Resolve the class in a way that it exposes the elements from IService<tmodel> and IAccountCodeService?

Note: I found that if I duplicated the IService<tmodel> properties in IAccountCodeService, the app would compile and then the IService<tmodel> elements were exposed and were able to be called. I do not want to implement this way though.

推荐答案

简而言之,Unity容器解析解决了接口问题.因此,如果您想要一个解析的引用,该引用同时实现IService< T>和IAccountCodeService,您需要一个同时实现这两者的接口.在这种情况下,似乎IAccountCodeService应该是该接口,因此请尝试接口IAccountCodeService扩展IService< AccountCode> (并且显然要让AccountCodeService实现它).

如果您的命名不正确,并且您实际上是在追随更一般的情况,那么可以总结一下:
In short, Unity container resolution resolves interfaces. So if you want a resolved reference which implements both IService<T> and IAccountCodeService, you need an interface which implements both. In this case it appears that IAccountCodeService should be that interface, so try interface IAccountCodeService extends IService<AccountCode> (and, obviously, have AccountCodeService implement it).

If your naming is bad, and you''re actually after the more general case which can be summarised:
interface IService<T> { ... }
interface ISomethingElse { ... }
class Example implements ISomethingElse, IService<DataClass> { ... }

container.RegisterType<???, Example>();
var example = container.Resolve<???>(); // What can I put here?


...那么您需要引入一个新的界面,您可以使用该界面进行注册:


... then you need to introduce a new interface which you can use to register:

interface IExample extends IService<DataClass>, ISomethingElse {}
class Example implements IExample { ... }


...然后您可以使用该界面进行注册和解析:


... and then you can use that interface for registration and resolution:

container.RegisterType<IExample, Example>();
var example = container.Resolve<IExample>();



另外,如果您只是出于服务单例原因而使用Unity,则可以直接注册具体类型,也可以仅使用更简单的单例模式,而完全不用担心Unity.在我看来,依赖注入被大大高估了.



Alternatively, if you are simply using Unity for service singleton reasons, it''s possible to register concrete types directly, or just use a simpler singleton pattern and not bother with Unity at all. Dependency injection is greatly over-rated in my opinion.


这篇关于具有继承接口的Unity解析对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆