在实体框架属性域逻辑 [英] Domain logic in a Entity framework Attribute

查看:98
本文介绍了在实体框架属性域逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC项目与域和数据层(只包含EF和迁移)。

I've an ASP.NET MVC project with a domain and a data layer (contains just EF and migrations).

现在,我要验证客户端的VATNUMBER场我有疑惑在哪里把它特定的公式。

Now, I have to validate a Client's VATNumber field with a specific formula I'm having doubts where to place it.

我想用控制器的ModelState中,以检查是否字段是有效的,使用EF DataAnnotation ValidationAttribute,所以我没有验证它在控制器的节能方法。但我有冲突不得不把业务逻辑在数据层。

I would like to use the Controller's ModelState to check if the field is valid, using a EF DataAnnotation ValidationAttribute, so I don't have to validate it in the Controller's Saving Method. But I'm conflicted having to put business logic in the data layer.

是否有某种替代ValidationAttribute的,所以它可以是注入?或者,我只是看着这个错误?

Is there some kind of alternative to ValidationAttribute, so it can be "injected"? Or I'm just looking at this wrongly?

推荐答案

属性验证,是不是实体框架的一部分。
如果你想从presentation层splite数据层,可以使用的ViewModels并将它们绑定与Automapper datamodels。在这种情况下,您将域验证的视图模型和数据模型数据层的验证。
在十分复杂的情况下,可以有三种模式:视图模型,和的DomainModel数据模型,但常见的情况视图模型和数据模型是足够的。

Validation attributes is not part of Entity Framework. If you want splite data layer from presentation layer, you can use Viewmodels and bind them on datamodels with Automapper. In this case you place domain validation on viewmodel and validation of datalayer on datamodel. In very complicated cases you may have three models: viewmodel, domainmodel and datamodel, but in common cases viewmodel and datamodel is enough.

这篇关于在实体框架属性域逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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