数据传输对象,业务对象,域对象还是其他什么东西? [英] Data Transfer Object, Business Object, Domain Object or something else?

查看:133
本文介绍了数据传输对象,业务对象,域对象还是其他什么东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据库中,我有表:Notes和表评论。在我的解决方案我有3个项目:DAL,BLL和Web

我需要显示包含注释的用户注释并没有设置为垃圾邮件,所以我在DAL项目已经创建了一个类:

 公共类NotesWithComments
{
    公众注意注意{搞定;组; }
    公共IEnumerable的<意见>评论{搞定;组; }
}

我用上面的类中的每个项目:DAL,BLL和Web。这是一流的数据传输对象,业务对象,域对象还是什么?

在一个仓库类我有一个查询:

 公共类NotesRepository
{
    DatabaseContext背景;    公共NotesRepository(DatabaseContext上下文)
    {
         this.context =背景;
    }
    公众的IQueryable< NotesWithComments> GetNotesWithNoSpamComments()
    {
        IQueryable的< NotesWithComments> notesWithNoSpamComments = context.Notes.Include(X => x.Comments).OrderByDescending(X => x.CreateDate)
            。选择(X =>新建NotesWithComments
            {
                注意= X,
                评论= x.Comments.Where(Y => y.IsSpam ==假).OrderBy(Y => y.CreateDate)
            });        返回notesWithNoSpamComments;
    }
}

在BLL项目中,我使用的方法从仓储类:

 公共类NotesService
{
    私人只读IUnitOfWork _unitOfWork;
    公共NotesService(IUnitOfWork的UnitOfWork)
    {
        _unitOfWork =的UnitOfWork;
    }
    公共IEnumerable的< NotesWithComments> GetNotesWithComments()
    {
        IQueryable的< NotesWithComments>结果= _unitOfWork.NotesRepository.GetNotesWithNoSpamComments();        返回结果;
    }
}

和在Web项目中我使用的方法形成服务类:

 公众的ActionResult指数()
{
    清单< NotesWithComments>结果= _notesService.GetNotesWithComments();    返回视图(结果);
}


解决方案

由于它没有任何公开的行为(属性或getter / setter方法​​不符合条件),也再次封装结构(,什么也不做,但属性或getter / setter方法暴露的基础数据不符合),它是没有目标可言。

没有,如果你使用的语言称之为一个对象或没有关系。这仅仅是一个数据结构(这是完全正常的,如果你只希望将数据从一个地方移动,就像一个数据库,到另一个,就像一个UI)。

或者,报价<一个href=\"http://qconlondon.com/dl/qcon-london-2010/slides/DanNorth_SimplicityTheWayOfTheUnusualArchitect.pdf\"相对=nofollow>丹北:


  

数据传输对象是一个矛盾。


In database I have table: Notes and table Comments. In my solution I have 3 projects: DAL, BLL and Web.

I need to show a user notes with comments which aren't set as spam so I have created in DAL project that class:

public class NotesWithComments
{
    public Notes Note { get; set; }
    public IEnumerable<Comments> Comments { get; set; }
}

I use above class in each project: DAL, BLL and Web. Is this class Data Transfer Object, Business Object, Domain Object or what?

In a repository class I have that query:

public class NotesRepository
{
    DatabaseContext context;

    public NotesRepository(DatabaseContext context)
    {
         this.context = context;
    }


    public IQueryable<NotesWithComments> GetNotesWithNoSpamComments()
    {
        IQueryable<NotesWithComments> notesWithNoSpamComments = context.Notes.Include(x => x.Comments).OrderByDescending(x => x.CreateDate)
            .Select(x => new NotesWithComments
            {
                Note = x,
                Comments = x.Comments.Where(y => y.IsSpam == false).OrderBy(y => y.CreateDate)
            });

        return notesWithNoSpamComments;
    }
}

In BLL project I use the method from the repository class:

public class NotesService
{
    private readonly IUnitOfWork _unitOfWork;


    public NotesService(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }


    public IEnumerable<NotesWithComments> GetNotesWithComments()
    {
        IQueryable<NotesWithComments> result = _unitOfWork.NotesRepository.GetNotesWithNoSpamComments();

        return result;
    }
}

And in Web project I use the method form the service class:

public ActionResult Index()
{
    List<NotesWithComments> result = _notesService.GetNotesWithComments();

    return View(result);
}

解决方案

Since it neither exposes any behavior (properties or getters/setters don't qualify) nor encapsulates its structure (again, properties or getters/setters that do nothing but expose the underlying data don't qualify) it is no object at all.

No matter if the language you use calls it an object or not. It is just a data structure (which is perfectly fine if you only want to move data from one place, like a database, to another, like a UI.)

Or, to quote Dan North:

Data Transfer Object is an oxymoron

这篇关于数据传输对象,业务对象,域对象还是其他什么东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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