POCO,DTO,DLL和贫血域模型 [英] POCO's, DTO's, DLL's and Anaemic Domain Models

查看:218
本文介绍了POCO,DTO,DLL和贫血域模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 POCO和DTO之间的差异(看来POCO的行为是行为(方法?)),并发现Martin Fowler关于贫血域模型的这篇文章



由于缺乏理解,我认为我已经创建了这些贫血域模型之一。



我的应用程序我的业务域实体在'dto'dll中定义。他们有很多属性与吸气者和设置者,没有太多的其他。我的业务逻辑代码(填充,计算)在另一个bllDLL中,我的数据访问代码是一个daldll。



所以通常我会这样创建一个dto:

 code> dto.BusinessObject bo = new dto.BusinessObject(...)

并传递它到bll层如下:

  bll.BusinessObject.Populate(bo); 

这又反过来执行一些逻辑,并将其传递给dal层,如下所示:

  dal.BusinessObject.Populate(bo); 

从我的理解,让我的dto进入POCO我需要做出业务逻辑和行为(方法)部分对象。所以,而不是上面的代码更像:

  poco.BusinessObject bo = new poco.BusinessObject(...)
bo.Populate();

ie。我正在调用对象上的方法,而不是将对象传递给该方法。



我的问题是 - 我该怎么做,仍然保留最佳实践分层的关注(单独的dll的等等)。不调用对象上的方法意味着方法必须在对象中定义?



请帮助我的困惑。

解决方案

通常,你不想在你的域对象中引入持久性,因为它不属于这种商业模式(飞机不构造自己,它会飞行乘客/货物从一个位置到另一个)。您应该使用存储库模式,一个 ORM框架或一些其他数据访问模式来管理对象的状态的持久存储和恢复。 p>

在这样做的时候,贫血域模型的出现是:

  IAirplaneService service = ...; 
飞机飞机= ...;
service.FlyAirplaneToAirport(plane,IAD);

在这种情况下,飞机状态的管理(无论是飞行,在哪里,什么是出发时间/机场,什么是到达时间/机场,什么是飞行计划等)被委托给飞机外部的一个... AirplaneService实例。



POCO的实现方式是以这种方式设计你的界面:

 飞机飞机= 
plane.FlyToAirport(IAD);

这是更可见的,因为开发者知道在哪里寻找飞机飞行(只是告诉飞机去做吧)。它还允许您确保内部仅管理状态 。然后,您可以将当前位置的内容像只读方式一样,并确保它只在一个地方更改。随着贫血域对象,由于状态是外部设置的,因此随着域名规模的增加,发现状态改变的地步越来越困难。


I was looking at the differences between POCO and DTO (It appears that POCO's are dto's with behaviour (methods?))and came across this article by Martin Fowler on the anaemic domain model.

Through lack of understanding, I think I have created one of these anaemic domain models.

In one of my applications I have my business domain entities defined in a 'dto' dll. They have a lot of properties with getter's and setter's and not much else. My business logic code (populate, calculate) is in another 'bll' dll, and my data access code is in a 'dal' dll. 'Best practice' I thought.

So typically I create a dto like so:

dto.BusinessObject bo = new dto.BusinessObject(...)

and pass it to the bll layer like so:

bll.BusinessObject.Populate(bo);

which in turn, performs some logic and passes it to the dal layer like so:

dal.BusinessObject.Populate(bo);

From my understanding, to make my dto's into POCO's I need to make the business logic and behaviour (methods) part of the object. So instead of the code above it is more like:

poco.BusinessObject bo = new poco.BusinessObject(...)
bo.Populate();

ie. I am calling the method on the object rather than passing the object to the method.

My question is - how can I do this and still retain the 'best practice' layering of concerns (separate dll's etc...). Doesn't calling the method on the object mean that the method must be defined in the object?

Please help my confusion.

解决方案

Typically, you don't want to introduce persistence into your domain objects, since it is not part of that business model (an airplane does not construct itself, it flies passengers/cargo from one location to another). You should use the repository pattern, an ORM framework, or some other data access pattern to manage the persistent storage and retreival of an object's state.

Where the anemic domain model comes in to play is when you're doing things like this:

IAirplaneService service = ...;
Airplane plane = ...;
service.FlyAirplaneToAirport(plane, "IAD");

In this case, the management of the airplane's state (whether it's flying, where it's at, what's the departure time/airport, what's the arrival time/airport, what's the flight plan, etc) is delegated to something external to the plane... the AirplaneService instance.

A POCO way of implementing this would be to design your interface this way:

Airplane plane = ...;
plane.FlyToAirport("IAD");

This is more discoverable, since developers know where to look to make an airplane fly (just tell the airplane to do it). It also allows you to ensure that state is only managed internally. You can then make things like current location read-only, and ensure that it's only changed in one place. With an anemic domain object, since state is set externally, discovering where state is changed becomes increasingly difficult as the scale of your domain increases.

这篇关于POCO,DTO,DLL和贫血域模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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