映射属性验证从域实体到DTO [英] Mapping Validation Attributes From Domain Entity to DTO

查看:204
本文介绍了映射属性验证从域实体到DTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标准的领域层实体:

I have a standard Domain Layer entity:

public class Product
{
    public int Id { get; set; }

    public string Name { get; set; }

    public decimal Price { get; set;}
}

具有某种验证特性的应用:

which has some kind of validation attributes applied:

public class Product
{
    public int Id { get; set; }

    [NotEmpty, NotShorterThan10Characters, NotLongerThan100Characters]
    public string Name { get; set; }

    [NotLessThan0]
    public decimal Price { get; set;}
}

正如你所看到的,我已经决定了这些属性完全。这验证框架(NHibernate的验证,DataAnnotations,ValidationApplicationBlock,青山验证等)在使用这里并不重要。

As you can see, I have made up these attributes completely. Which validation framework (NHibernate Validator, DataAnnotations, ValidationApplicationBlock, Castle Validator, etc) in use here is not important.

在我的客户层,我也有一个标准的安装,我不使用域实体本身,而是它们映射到我的视图层使用的ViewModels(又名DTO):

In my client layer, I also have a standard setup where I don't use the Domain entities themselves, but instead map them to ViewModels (aka DTO) which my view layer uses:

public class ProductViewModel
{
    public int Id { get; set; }

    public string Name { get; set; }

    public decimal Price { get; set;}
}

那么,让我们说,我希望我的客户机/视图能够执行一些基本的属性级别的验证。

Let's then say that I want my client/view to be able to perform some basic property-level validations.

我知道我能做到这一点的唯一方法是重复的视图模型对象中的验证定义:

The only way I see I can do this is to repeat the validation definitions in the ViewModel object:

public class ProductViewModel
{
    public int Id { get; set; }

    // validation attributes copied from Domain entity
    [NotEmpty, NotShorterThan10Characters, NotLongerThan100Characters]
    public string Name { get; set; }

    // validation attributes copied from Domain entity
    [NotLessThan0]
    public decimal Price { get; set;}
}

这显然是不能令人满意的,因为我现在已经在视图模型(DTO)层重复的业务逻辑(属性级别的验证)。

This is clearly not satisfactory, as I have now repeated business logic (property-level validation) in the ViewModel (DTO) layer.

那么,什么可以做?

假设我使用像AutoMapper自动化工具,我的域名映射实体对我的ViewModel DTO的,是不是也很酷以某种方式转移验​​证逻辑映射属性视图模型呢?

Assuming that I use an automation tool like AutoMapper to map my Domain entities to my ViewModel DTOs, wouldn't it also be cool to somehow transfer the validation logic for the mapped properties to the ViewModel as well?

的问题是:

1),这是一个好主意?

1) Is this a good idea?

2)如果是的话,能不能做到?如果不是,有什么办法,如果有的话?

2) If so, can it be done? If not, what are the alternatives, if any?

感谢您事先的任何输入!

Thank you in advance for any input!

推荐答案

如果你正在使用的东西支持DataAnnotations,你应该能够使用元数据类包含您的验证属性:

If you're using something supporting DataAnnotations, you should be able to use a metadata class to contain your validation attributes:

public class ProductMetadata 
{
    [NotEmpty, NotShorterThan10Characters, NotLongerThan100Characters]
    public string Name { get; set; }

    [NotLessThan0]
    public decimal Price { get; set;}
}

和在MetadataTypeAttribute添加两个域实体放大器; DTO:

and add it in the MetadataTypeAttribute on both the domain entity & DTO:

[MetadataType(typeof(ProductMetadata))]
public class Product

[MetadataType(typeof(ProductMetadata))]
public class ProductViewModel

这将无法制定出与所有验证盒子 - 你可能需要延长您选择的验证框架来实现类似的方法。

This won't work out of the box with all validators - you may need to extend your validation framework of choice to implement a similar approach.

这篇关于映射属性验证从域实体到DTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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