Data Transfer Object, Business Object, Domain Object or something else?

倾然丶 夕夏残阳落幕 提交于 2019-12-07 18:58:17

问题


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);
}

回答1:


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




回答2:


Is this class Data Transfer Object, Business Object, Domain Object or what?

A DTO is typically a class that is mainly used for transferring data between layers or some type of boundaries..typically just an object with no behavior.

I have always referred to Domain Objects as something that maps directly to a database table. So In your example, your domain models would be Notes, and Comments.

I would consider your NotesWithComments object a dto, or possibly a view model (as you're using it as your asp.net mvc model for the view).

The practice I would normally use here is use your NotesWithComments as a dto (transfer data, no behavior, easily serializable, very clean ect), and create another class to act as your view model.

In the beginning these classes would probably be very similar, possibly the same..but if you make changes over time, or your view needs to display different things, you would just change your view model, and populate it from other dtos, or tranform your data however you need to. You could also then get rid of the properties on your view model that your view doesn't need.. (unless your view magically maps directly to every property on your current dto). It's a bit more work up front but if you're working on a big long living project I think you'd be happy you did it later on.

So you would populate your domain models using EF in your data layer, you would then use your dto and transfer that data to the Biz layer, do w/e you need there, then use your dto (could be the same one) to transfer your data to your presentation layer (mvc), and populate your view model from the dtos you receive.

Anyway that's my take on it.



来源:https://stackoverflow.com/questions/24470122/data-transfer-object-business-object-domain-object-or-something-else

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!