我是否应该使用可选的构造函数映射域对象到视图模型

我是否应该使用可选的构造函数映射域对象到视图模型

本文介绍了我是否应该使用可选的构造函数映射域对象到视图模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够通过newing一个视图模型,并在促进域模型作为一个参数(如低于code)使映射域模型视图模型。我的动机是为了利用重映射code保留,并提供一个简单的方法来映射(不使用automapper还)。一位朋友说,视图模型不应该知道这件事的被传递到可选的构造函数中的支付领域模型什么。你觉得呢?

 公共类LineItemsViewModel
{
    公共LineItemsViewModel()
    {
    }    公共LineItemsViewModel(IPayment支付)
    {
        LineItemColumnHeaders = payment.MerchantContext.Profile.Ui preferences.LineItemColumnHeaders;
        了LineItem = LineItemDomainToViewModelMapper.MapToViewModel(payment.LineItems);
        ConvenienceFeeAmount = payment.ConvenienceFee.Fee;
        小计= payment.PaymentAmount;
        总= payment.PaymentAmount + payment.ConvenienceFee.Fee;
    }    公共IEnumerable的<&字典LT; INT,串>> {了LineItem获得;组; }
    公共字典< INT,串> LineItemColumnHeaders {搞定;组; }
    公共小数小计{搞定;组; }
    公共小数ConvenienceFeeAmount {搞定;组; }
    公共小数总{搞定;组; }
}


解决方案

您的朋友是对的。意见应该是愚蠢的,不知道你的域模型东西。

您是否尝试过使用您的业务/领域实体/模型映射到你的DTO /的ViewModels?

由于注释的更多细节:

把测绘code在你的ViewModels违反了关注分离,固体单一责任主体,MVC模式和领域驱动设计原则。查看有一个责任,获取数据到屏幕上,多数民众赞成它。恕我直言,没有太多的争论。它仅仅是一个坏主意,违反了很多软件开发核心主体。

I'd like to be able to map a domain model to a view model by newing up a view model and passing in the contributing domain model as a parameter (like the code below). My motivation is to keep from re-using mapping code AND to provide a simple way to map (not using automapper yet). A friend says the view model should not know anything about the "payment" domain model that's being passed into the optional constructor. What do you think?

public class LineItemsViewModel
{
    public LineItemsViewModel()
    {
    }

    public LineItemsViewModel(IPayment payment)
    {
        LineItemColumnHeaders = payment.MerchantContext.Profile.UiPreferences.LineItemColumnHeaders;
        LineItems = LineItemDomainToViewModelMapper.MapToViewModel(payment.LineItems);
        ConvenienceFeeAmount = payment.ConvenienceFee.Fee;
        SubTotal = payment.PaymentAmount;
        Total = payment.PaymentAmount + payment.ConvenienceFee.Fee;
    }

    public IEnumerable<Dictionary<int, string>> LineItems { get; set; }
    public Dictionary<int, string> LineItemColumnHeaders { get; set; }
    public decimal SubTotal { get; set; }
    public decimal ConvenienceFeeAmount { get; set; }
    public decimal Total { get; set; }
}
解决方案

Your friend is right. Views should be dumb and not know anything about your domain model.

Have you tried using Automapper to map your business/domain entities/models to your dto/viewmodels?

More details because of comment:

Putting mapping code in your viewmodels violates the separation of concern, the SOLID single responsibility principal, the MVC pattern, and domain driven design principals. Views have one responsibility, get data to the screen, thats it. IMHO there isn't much to argue about. Its simply a bad idea that violates a lot of core software development principals.

这篇关于我是否应该使用可选的构造函数映射域对象到视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:32