我可以将 MVC 2 DataAnnotation 属性添加到现有属性吗?

发布于 2024-08-14 01:30:38 字数 154 浏览 5 评论 0原文

我使用生成的类作为模型,并且希望将 DataAnnotation 属性添加到其某些属性中。由于它是生成的代码,我不想直接添加注释。还有其他方法可以将它们附加到财产上吗?

我考虑过使模型成为一个接口,并使用分部类来获取生成的类来订阅它。假设可行的话,是否有一个不太复杂的解决方案?

I'm using a generated class as a model, and I wish to add DataAnnotation attributes to some of its properties. As it's a generated code, I don't want to add the annotations directly. Is there another way to attach them to a property?

I'd considered making the model an interface, and using a partial class to get the generated class to subscribe to it. Is there a less elaborate solution, assuming that would even work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温馨耳语 2024-08-21 01:30:38

是的,有。您必须创建与原始模型具有相同属性的元数据类,并使用 MetadataType 属性将其连接到您的模型:

[MetadataType(typeof(MyModelMetadata))]
public partial class OriginalMyModel
{
}

public class MyModelMetadata
{
    [Required]
    public string MyProperty;  

    // ...
}

在示例中,ebove OriginalModel 是您正确的模型类,而 MyModelMetadata 是仅用于注释属性的类。 MyModelMetadata 应该具有与您的模型相同的属性。

Yes there is. You have to create metadata class that will have the same properties that your original model, and connect it to your model with MetadataType attribute:

[MetadataType(typeof(MyModelMetadata))]
public partial class OriginalMyModel
{
}

public class MyModelMetadata
{
    [Required]
    public string MyProperty;  

    // ...
}

In the example ebove OriginalModel is your proper model class, and MyModelMetadata is a class used only for annotating properties. MyModelMetadata should have the same properties that your model has.

您可以在类上使用 MetadataType 属性:

http: //msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute.aspx

在实践中,我经常看到元数据与生成的模型不同步,这可能会导致一些头痛。您可能想研究替代验证机制而不是数据注释。

我一直在使用 Fluent Validation,它非常容易上手并开始使用。 Fluent Validation 2.0(仍处于测试版)中甚至还有一个 Fluent Validation 到 xVal 集成部分,您可以将其引入到您的项目中进行客户端验证。

Fluent Validation 允许您在单独的类中定义验证。您需要做的就是向生成的类添加一个属性,告诉它要使用哪个验证器,这可以通过部分类来完成。

或者,您可以创建特定于视图的模型,这些模型映射到包含数据注释的域模型。在这种情况下,请使用 AutoMapper 之类的东西来简化来回映射。然后,如果您的领域模型发生变化,与元数据方法相比,您会遇到编译时错误。

You can use the MetadataType attribute on your class:

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute.aspx

In practice, I've seen the metadata get out of sync with a generated model pretty frequently, though, which can lead to some headaches. You may want to look into an alternate validation mechanism instead of data annotations.

I've been using Fluent Validation, which is very easy to pick up and start using. There is even a Fluent Validation to xVal integration piece in Fluent Validation 2.0 (still in beta) that you can bring into your project for client-side validation.

Fluent Validation allows you to define your validation in a separate class. All you would need to do is add an attribute to your generated class telling it what validator to use, which could be accomplished through partial classes.

Alternatively, you could create view-specific models that are mapped to from your domain model that contain your data annotations. In that case, simplify the back-and-forth mapping using something like AutoMapper. Then, if your domain model changes, you get compile-time errors versus the metadata approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文