把DataAnnotation哥们类另一个程序 [英] Putting DataAnnotation buddy class in another assembly

查看:121
本文介绍了把DataAnnotation哥们类另一个程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为项目的模式即可。我所有的实体是由产生该项目的 EF code 第一位。

 公共部分类人
{
    ...
}

我不想碰我的生成的类,所以我可以创建一个部分类并添加 MetadataType 属性吧。

  [MetadataType(typeof运算(Person_Metadata))]
公共部分类人
{
}

和这里是我的哥们类

  [绑定(不包括=是PersonID)]
公共类Person_Metadata
{
    [显示器(产品名称:=名字)]
    公共字符串名字{获得;组; }
    [显示器(产品名称:=姓)]
    公共字符串名字{获得;组; }
}

问题是,我想我哥们类移动到另一个程序集

示范项目具有的没有提到它所以 [MetadataType(typeof运算(Person_Metadata))] 给出了一个错误,因为它没有提及 Person_Metadata 类。

我可以使用 FluentValidation 作为验证的一部分(和它的伟大工程),但对于其他元数据,如:<?code>显示属性

我也发现了这个问题:<一href=\"http://stackoverflow.com/questions/5810851/adding-dataannotation-to-class-when-using-fluentvalidation\">Adding DataAnnotation上课使用FluentValidation时了解管理与FluentValidation引擎元数据,但看起来像一个很长的路要走,我用数据注解preFER属性


解决方案

  

问题是,我想我的哥们类移动到另一个程序集。


这是不可能的。部分类的工作只能在同一组件的边界。


  

我可以使用FluentValidation进行验证的一部分(和它的伟大工程),
  但对于其他元数据,如:?显示属性


没什么,他们只是可以留在您的视图模式即可。不是你的域模型。您的视图模型是你专门设计,以满足你的意见的要求类。这是对那些你会使用 [DisplayFormat] 以及类似的属性视图模型。

I have a project called Model. All of my entities are in that project generated by EF Code first.

Public Partial Class Person
{
    ...
}

I don't want to touch my generated classes so I can create a partial class and add MetadataType attribute to it.

[MetadataType(typeof(Person_Metadata))]
public partial class Person
{
}

And here is my buddy class.

[Bind(Exclude="PersonID")]
public class Person_Metadata
{
    [Display(Name:="First name")]
    public string FirstName { get; set; }
    [Display(Name:="Last name")]
    public string LastName { get; set; }
}

The problem is, I want to move my buddy class to another assembly.

Model project has no reference to it so [MetadataType(typeof(Person_Metadata))] gives an error because it has no reference to Person_Metadata class.

I can use FluentValidation for validation part (and it works great), but what about other Metadata like: Display Attribute ?

I have also found this question: Adding DataAnnotation to class when using FluentValidation about managing MetaData with FluentValidation engine, but that looks like a long way to go and I prefer using data annotation attributes

解决方案

The problem is, I want to move my buddy class to another assembly.

That's impossible. Partial classes work only within the boundaries of the same assembly.

I can use FluentValidation for validation part (and it works great), but what about other Metadata like: Display Attribute ?

Nothing, they just could stay on your view model. Not on your domain model. Your view models are classes that you specifically design to meet the requirements of your views. It's on those view models that you would use the [DisplayFormat] and similar attributes.

这篇关于把DataAnnotation哥们类另一个程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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