WCF 和领域模型 - 不需要贫血吗?

发布于 2024-10-25 04:45:27 字数 1123 浏览 0 评论 0原文

以下是反对 Martin Fowler 提出的 贫血域模型 的论点(请阅读 链接)。

现在,根据此描述,人们会期望业务对象不仅具有 getter 和 setter,还具有行为,如下所示。

    public class Student
{
    private List<Course>_courses = new List<Course>();
    public string Name{get; set;}
    public ReadOnlyCollection<Course> Courses {
        get{ return _courses.AsReadOnly();}
    }
    public void Add(Course course)
    {
        if (course != null && _courses.Count <= 3)
        {
            _courses.Add(course);
        }
    }
    public bool Remove(Course course)
    {
        bool removed = false;
        if (course != null && _courses.Count <= 3)
        {
            removed = _courses.Remove(course);
        }
        return removed;
    }
}

但是像上面描述的 Student 这样的对象无法通过 WCF 服务调用正确公开(课程仅通过只读属性公开)。这意味着我需要有一个返回列表的课程 getter 和 setter,

所以贫血域模型不适合 WCF,并且仅当客户端可以实际使用代码时才适合适当的域模型(Asp.net 在服务器端或在服务器端时)使用 Silverlight 等时的客户端业务实体)。

Here is the argument against an Anemic Domain Model as presented by Martin Fowler (read link).

Now based on this description, one would expect the business object to not just have getters and setters, but also behavior, such as what I show below.

    public class Student
{
    private List<Course>_courses = new List<Course>();
    public string Name{get; set;}
    public ReadOnlyCollection<Course> Courses {
        get{ return _courses.AsReadOnly();}
    }
    public void Add(Course course)
    {
        if (course != null && _courses.Count <= 3)
        {
            _courses.Add(course);
        }
    }
    public bool Remove(Course course)
    {
        bool removed = false;
        if (course != null && _courses.Count <= 3)
        {
            removed = _courses.Remove(course);
        }
        return removed;
    }
}

But an object like the Student as described above cannot be properly exposed via a WCF service call (Courses is exposed only via a readonly property). Which would mean that I need to have a Courses getter and setter which returns a List

So isnt the Anemic domain model appropriate for WCF and a proper domain model appropriate only when the client can actually use the code (Asp.net when server side or in the client side business entities when using Silverlight, etc).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

合约呢 2024-11-01 04:45:27

<块引用>

贫血领域模型不适合 WCF

在这种情况下,您所说的贫血数据模型,我称之为数据传输对象。

领域模型捕获行为和驱动行为的数据。通常通过远程端点按原样公开域模型通常会遭受过多的耦合并遇到实际问题。

通常,数据传输对象 (DTO) (http://martinfowler.com/eaaCatalog/dataTransferObject.html) 是解决设计紧张的好方法。

最终将有代码遍历您的域模型属性并将数据复制到适当的 DTO 以返回到 WCF 服务的调用者。

So isnt the Anemic domain model appropriate for WCF

In this context, what you call Anemic Data Model, I'd call Data Transfer Object.

Domain model captures behavior and the data that drives the behavior. Often exposing a domain model as-is over a remote endpoint often suffers from too much coupling and runs into practical problems.

Often Data Transfer Objects (DTO) (http://martinfowler.com/eaaCatalog/dataTransferObject.html) is a good way of solving that design tension.

There will end up being code that walks your domain model properties and copies the data over to the appropriate DTO for return to the caller of your WCF service.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文