DTO传递给我的ViewModels构造函数映射属性 [英] Passing DTO to my ViewModels constructor to map properties

查看:287
本文介绍了DTO传递给我的ViewModels构造函数映射属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的解决方案,我有两个项目。

In my solution I have two projects.

项目1(核心)
使用小巧精致的映射到SQL DTO

Project 1 (Core) Mapping SQL to DTO using Dapper

2项目(WebUI中 - ASP.NET MVC 4)
这里我用一个ViewModel按次。

Project 2 (WebUI - ASP.NET MVC 4) Here I use a ViewModel per View.

一个控制器的例子

  [HttpGet]
    public ActionResult Edit(int id)
    {
        // Get my ProductDto in Core
        var product = Using<ProductService>().Single(id);
        var vm = new ProductFormModel(product);

        return View(vm);
    }

一个ViewModel的例子

Examples of a ViewModel

public class ProductFormModel : BaseViewModel, ICreateProductCommand
    {
        public int ProductId { get; set; }
        public int ProductGroupId { get; set; }
        public string ArtNo { get; set; }
        public bool IsDefault { get; set; }
        public string Description { get; set; }
        public string Specification { get; set; }
        public string Unit { get; set; }
        public string Account { get; set; }
        public decimal NetPrice { get; set; }

        public ProductFormModel(int productGroupId)
        {
            this.ProductGroupId = productGroupId;
        }

        public ProductFormModel(ProductDto dto)
        {
            this.ProductId = dto.ProductId;
            this.ProductGroupId = dto.ProductGroupId;
            this.ArtNo = dto.ArtNo;
            this.IsDefault = dto.IsDefault;
            this.Description = dto.Description;
            this.Specification = dto.Specification;
            this.Unit = dto.Unit;
            this.Account = dto.Account;
            this.NetPrice = dto.NetPrice;
        }

        public ProductFormModel()
        {
        }
    }

说明:
我将在这个项目(核心)使用服务类让我的DTO在我的控制器。
然后创建我的视图模型并通过DTO在视图模型构造。
我也可以使用此视图来添加一个新的产品,因为我的视图模型可以采取空构造。

Explanation: I'll get my DTOs in my controller using a service class in the project (Core). Then i create my ViewModel and pass the DTO to the constructor in ViewModel. I can also use this view to add a new Product because my ViewModel can take a empty constructor.

有没有人有这方面的经验。我不知道如果我以这种方式作为项目的增大将在未来的问题?

Does anyone have experience of this. I wonder if I am in this way will have problems in the future as the project gets bigger?

我知道这有没有关系小巧玲珑。但我还是想一个好办法来解释我的解决方案。

I know this has nothing to do with Dapper. But I would still like a good way to explain my solution.

推荐答案

我想你会用你目前的做法被罚款。更重要的是,开始像这样和重构如果您开始遇到与你的对象映射code(而不是想得太多关于它的事前)的问题。

I think you will be fine using your current approach. More importantly, start out like this and refactor if you start to encounter problems related to your object mapping code (instead of thinking too much about it beforehand).

组织映射逻辑的另一种方式,我有时用是采用扩展方法。这样,映射code保持从视图模型本身是分开的。是这样的:

Another way to organize mapping logic that I use sometimes is to employ extension methods. That way, the mapping code is kept separate from the view model itself. Something like:

public static class ProductMappingExtensions
{
    public static ProductFormModel ToViewModel(this ProductDto dto)
    {
        // Mapping code goes here
    }
}

// Usage:

var viewModel = dto.ToViewModel();

另一种方法是使用的映射框架的如 AutoMapper - 这是一个不错的选择特别是如果你的映射逻辑很简单(大量的1:性能之间的映射1)。

Yet another approach would be to use a mapping framework like AutoMapper - this is a good fit in particular if your mapping logic is simple (lots of 1:1 mappings between properties).

但同样,开始简单重构,当你需要

这篇关于DTO传递给我的ViewModels构造函数映射属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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