创建包装器时的 IOC 容器和基元
我正在尝试为我的 IOC 容器创建一个接口包装器,这样我就不必依赖于特定的容器。我的问题是,我的一些服务类采用了公司 ID,它是一个字符串。我想制作像
T Resolve
这样的通用接口方法,其中 T 是服务接口。
现在我在幕后使用 StructureMap 并知道具体构造函数是否接受 companyID,因此我将执行以下操作:
ObjectFactory.With("companyid").EqualTo("someCompanyID").GetInstance
我将这种调用包装在接口方法中: ICompanyService GetCompanyService(string companyID)
按照我现在的方式,应用程序必须初始化 StructureMaps 配置和传回服务的具体类,必须了解很多有关构造函数的信息。我希望这种情况不会发生,并使包装器变得通用。有没有一种好的方法,无需将companyID添加到界面上的每个方法中?
I am trying to create an interface wrapper for my IOC container so I do not have to have a dependency on a particular one. My problem is that some of the service classes I have take in a companyID which is a string. I want to make generic interface methods like
T Resolve<T>()
where T is the service interface.
Right now I use StructureMap behind the scenes and know if the concrete constructor takes in the companyID so I will do something like this:
ObjectFactory.With("companyid").EqualTo("someCompanyID").GetInstance<ICompanyService>();
I wrap this sort of call in an interface method:ICompanyService GetCompanyService(string companyID)
The way I have it now, the application has to initialize StructureMaps config and the concrete class, that passes back the services, has to know a lot about the constructors. I would like for that not to happen and to make the wrapper generic. Is there a nice way, without having to add companyID
to each method on the interface?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就我个人而言,我并不真正关心抽象出 MSUnity(我选择的 IOC 容器)。对我来说,这一步太远了。听起来您正在使用结构图特定功能,这将使抽象变得更加困难。
您是否了解 CommonServiceLocator 项目?主要有两种方法:
只要您坚持使用这些方法,您就可以轻松切换。以下是一些更多信息。
Personally I don't really care about abstracting out MSUnity (My IOC container of choice). For me it's one step too far. It sounds like you are using structuremap specific features, which will make the abstraction harder.
Are you aware of the CommonServiceLocator project?. That has main methods two methods:
Providing you stick to using these you can easily switch. Here's some more info.
MvcContrib中有一个DependencyResolver类。
另一方面,大多数时候我只是从我的应用程序项目中引用 IoC 容器。例如,我只需为 ctor 注入设置我的类,当我需要获取一个实例(在应用程序项目中)时,我只需向 IoC 容器请求即可。 IoC 容器可以担心填充 ctor args,但对象不知道 IoC 容器。这样我的应用程序项目是唯一需要引用 IoC 容器的项目。
There is a DependencyResolver class in MvcContrib.
On the other hand, most of the time I just ref the IoC container from my app project only. For instance, I simply setup my classes for ctor injection and when I need to grab an instance (in the app project) I simply ask the IoC container for it. The IoC container can worry about filling in the ctor args but the objects are unaware of the IoC container. This way my app project is the only project that needs to ref the IoC container.