数据容器与对象与行为相结合
我正在为工作流层次结构编写一个存储解决方案。
为了简化图片,我有两种类型的对象:Workflow 和 WorkflowStep。
尽管 WorkflowStep 按层次结构位于 Workflow 之下,但 Workflow 不会聚合 WorkflowStep,因为我们将这些对象仅视为数据容器。
因此,这给我留下了以下类:
public class Workflow : Node {
public string UID;
public string parentID;
}
public class WorkflowStep : Node {
public string UID;
public string parentID;
}
public class WorkflowEngine {
public void Store(Workflow wf) {
}
public void Store(WorkflowStep wfs) {
}
}
不在 Workflow 中聚合 WorkflowStep 的原因(即使逻辑上适合)是这些对象纯粹被视为数据容器,它们可能会在以后发生更改,并且我们希望保留存储这些对象与对象本身解耦。
当然,另一种选择是这样做:
public class Workflow : Node {
public List<WorkflowStep> steps;
public string UID;
public string parentUID;
public void Store() { }
}
public class WorkflowStep : Node {
public string UID;
public string parentID;
public void Store() { }
}
这两种方法的优点和缺点是什么?有没有任何文献讨论这两种设计?
I am writing a storage solution for a workflow hierarchy.
To simplify the picture I have 2 types of objects, a Workflow and a WorkflowStep.
Even though WorkflowStep comes hierarchically under the Workflow, the Workflow does not aggregate WorkflowStep because we view these objects as just data containers.
So this leaves me with the following classes:
public class Workflow : Node {
public string UID;
public string parentID;
}
public class WorkflowStep : Node {
public string UID;
public string parentID;
}
public class WorkflowEngine {
public void Store(Workflow wf) {
}
public void Store(WorkflowStep wfs) {
}
}
The reasoning for not aggregating WorkflowStep inside Workflow (even though logically that fits) is that these objects are purely viewed as data containers and they may be subject to changes later on and we want to keep the storage of these objects decoupled from the objects themselves.
The other alternative of course would be to do something like this:
public class Workflow : Node {
public List<WorkflowStep> steps;
public string UID;
public string parentUID;
public void Store() { }
}
public class WorkflowStep : Node {
public string UID;
public string parentID;
public void Store() { }
}
What are the pros and cons of either approach? Is there any literature that talks about both the designs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管
Workflow
和WorkflowStep
都是数据容器,但将它们与分层措施分开并不能解决解耦问题。将
WorkflowStep
保留在Workflow
的层次结构上更符合逻辑,并且为了实现解耦,在这种情况下必须引入IoC
。IoC
的美妙之处在于,更改WorkflowStep
的定义(它是Workflow
类中的列表)将是透明的,您只需考虑在IoC
容器上注册您的类型。让我为您提供一个使用
Ninject
IoC
容器框架的示例。定义接口并相应地实现数据容器:
现在,Ninject 模块是:
这是将接口与具体类绑定的唯一位置。而在世界其他地方,您只需请求一个已定义接口的实例即可。
要解析您的类型,您需要创建一个 Ninject
Kernel
,它是一个IKernel
类型,并且通过加载您定义的 StandardKernel 的具体实现>模块。这就像,
现在,您所要做的就是解决您想要的接口,例如:
这里的美妙之处在于,您无需担心具体的实现,并且它在您的系统中非常紧密地耦合。它只是您将要处理的接口,剩下的就是您的 IoC 容器实现的担忧。
由于您更担心
WorkflowStep
的实现被更改并且不与Workflow
耦合。我想,这就是 IoC 发挥作用的地方。请注意,您可以使用任何
IoC
容器框架,例如Unity、Spring.NET、StructureMap > 等等。我使用 Ninject 因为我对它很满意。Even though
Workflow
andWorkflowStep
are both data containers but keeping these aside from hierarchical measures doesn't solve your decoupling issue.It is more logical to keep
WorkflowStep
on hierarchy ofWorkflow
and to get along with decoupling you must introduceIoC
in this case.The Beauty of
IoC
is that changing the definitions ofWorkflowStep
which is a list inWorkflow
class will be just transparent where you will only be considering to register your types on yourIoC
container.Let me put you on an example with
Ninject
IoC
container framework.Define interfaces and implement your data containers accordingly:
And now, the Ninject module be:
This is the single place where you bind your interfaces with concrete classes. And rest of the world, you just ask for an instance of defined interface.
To resolve your type, you need to create a Ninject
Kernel
which is anIKernel
type and a concrete implementation ofStandardKernel
by loading your defined module.Which is something like,
Now, all you have to do is resolve your desired interface, like:
The beauty here is, you don't need to worry about your concrete implementation and which is very tightly coupled within your system. Its just the interface you will be dealing with and rest are the worries of your
IoC
container implementation.As you are more worried about the implementation of
WorkflowStep
to be changed and not coupling withWorkflow
. I guess, this is whereIoC
comes to play.Please be noted that, you can use any
IoC
container framework like Unity, Spring.NET, StructureMap and etc. I used Ninject because I am comfortable with it.