两个应用程序之间共享的对象模型的数据注释

发布于 2024-09-27 06:50:16 字数 253 浏览 1 评论 0原文

我有一个包含我的对象模型的类库。我希望每个对象都具有数据注释,以将验证规则放入我的模型中,以便可以在 2 个应用程序之间共享验证。一个是 MVC 2 应用程序,另一个是 Windows 窗体应用程序。

我需要能够使用数据注释从代码手动验证对象模型,但不使用 xVal。当我将对象模型库切换到 4.0 客户端配置文件时,它无法再使用 xVal 组件进行构建。域对象类库将随 Windows 应用程序一起分发,因此我想利用 4.0 客户端配置文件。

有什么想法吗?

I have a class library that contains my object model. I'd like each object to have data annotations to place validation rules into my model so that validation can be shared across 2 apps. One is an MVC 2 app and the other is a Windows forms app.

I need to be able to validate the object model manually from code using the data annotations, but without using xVal. When I switch the object model library to the 4.0 client profile it can no longer build with the xVal components. The domain object class library will be distributed with the windows app, so I wanted to utilize the 4.0 client profile.

Any ideas?

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

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

发布评论

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

评论(1

耳钉梦 2024-10-04 06:50:16

好吧,我将提供更多实质内容的答案,以防对其他人有所帮助。

对于我们的验证,我们使用一个简单的 Validate 方法,如下所示:

public void Validate(T entity)
{
    var context = new ValidationContext(entity, null, null);
    var results = new List<ValidationResult>();

    bool valid = Validator.TryValidateObject(entity, context, results, true);

    if (!valid)
        ; // do something fancy with the results here, perhaps
}

如果您不想对结果。

Well, I'll provide an answer with a little more substance in case it might be of help to someone else.

For our validation, we use a simple Validate method like this:

public void Validate(T entity)
{
    var context = new ValidationContext(entity, null, null);
    var results = new List<ValidationResult>();

    bool valid = Validator.TryValidateObject(entity, context, results, true);

    if (!valid)
        ; // do something fancy with the results here, perhaps
}

You can also skip the TryValidateObject and go right to ValidateObject if you don't want to do anything fancy with the results.

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