使用IoC容器时如何管理接口隔离? [英] How to manage interface segregation when using an IoC container?

查看:87
本文介绍了使用IoC容器时如何管理接口隔离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
StructureMap单例用法(实现两个接口的类)

我目前正在设计一个小型系统,目前正在使用structureMap作为IoC.我刚刚才知道接口隔离的意义...我现在想知道.

如果我有一个特定的业务对象,它将实现说三个接口...我应该如何在代码的配置和初始化中处理这个问题?

假设在一个简单的场景中,对于名为EmployeeServiceObject的服务层类,我有两个接口. IGenericEntity和IEmployeeServiceObject.

GenericEntity将为该类提供CRUD功能,而IEmployeeServiceObject将为业务查询/操作提供结果集.

如果使用Facade/Service Layer方法,则必须使用EmployeeServiceObject类,并实际上使用两个接口中的功能...应如何处理?

最初,我认为正确的事情是设置IoC的配置,以将IEmployeeServiceObject映射到EmployeeServiceObject,向工厂询问对象,然后在需要使用CRUD功能时将其强制转换为IGenericEntity,但是我不太确定.这似乎也不对,因为我永远不会正式声明具体的类实际上是在实现ioc容器配置中未设置的接口.

我当然知道创建相同具体类的两个实例,但要求使用不同的接口……听起来更糟.

应如何处理?

解决方案

我没有找到如何以重复方式结束我的问题...但是我确切地找到了如何解决另一个stackoverflow问题中所需的事情.

Mark在Windsor中提到的F​​orward方法,它也存在于StructureMap中……看来这是做事的正确方法.

StructureMap单例用法(实现两个接口的类)

Possible Duplicate:
StructureMap singleton usage (A class implementing two interface)

I'm currently designing a small system and i'm currently using structureMap as IoC. I just recently got the point of interface segregation...and I'm wondering now.

If I have a certain business object, that will implement say, three interfaces... how should I handle this in the configuration and instatiation of code?

Assuming I have two interfaces in a simple scenario, for a service layer class called EmployeeServiceObject. IGenericEntity and IEmployeeServiceObject.

GenericEntity will provide the CRUD capabilities for the class, and IEmployeeServiceObject will provide resultsets for business queries/operations.

If on a Facade/Service Layer method, I have to use the EmployeeServiceObject class and actually use functionality from both interfaces...how should this be handled?

Initially I thought that the correct thing was to setup the configuration of the IoC to map IEmployeeServiceObject to EmployeeServiceObject, ask the factory for the object, and just cast it to IGenericEntity when i needed to use the CRUD functionality, but i'm not quite sure. It also does not seem right because I'd never be formally stating that the concrete class is actually implementing the interface that was not setup in the ioc container configuration.

and I definitely know that creating two instances of the same concrete class but asking for a different interface...sounds even worse.

How should this be handled?

解决方案

I didnt find how to close my question as duplicate...but I found exactly how to do what I needed in another stackoverflow question.

What Mark mentioned as the Forward method in Windsor, it also exists in StructureMap...and it appears it would be the right way to do things.

StructureMap singleton usage (A class implementing two interface)

这篇关于使用IoC容器时如何管理接口隔离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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