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

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

问题描述

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

In my solution I have two projects.

项目 1(核心)使用 Dapper 将 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.然后我创建我的 ViewModel 并将 DTO 传递给 ViewModel 中的构造函数.我还可以使用此视图添加新产品,因为我的 ViewModel 可以采用空构造函数.

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?

我知道这与 Dapper 无关.但我仍然想要一个很好的方式来解释我的解决方案.

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

推荐答案

我认为您使用当前的方法会很好.更重要的是,如果您开始遇到与对象映射代码相关的问题(而不是事先考虑太多),请以这样的方式开始并重构.

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).

我有时使用的另一种组织映射逻辑的方法是使用扩展方法.这样,映射代码就与视图模型本身分开了.类似的东西:

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天全站免登陆