域逻辑VS数据验证 [英] Domain logic vs data validation

查看:201
本文介绍了域逻辑VS数据验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我忙读书,和享受,依赖注入净马克塞曼。

I am busy reading, and enjoying, Dependency Injection in .Net by Mark Seemann.

这是相当困难的我解释确切的范围内,所以请,如果你熟悉的书这个问题只有麻烦了。

It is quite difficult for me to explain the exact context, so please only bother with this question if you are familiar with the book.

我的问题在第2章第49与两个产品类别做有一个领域层和一个数据访问层英寸据解释说,数据访问层中的产品类由LINQ到实体向导创建。

My question has to do with the two Product classes in chapter 2 pg 49. There is one in the Domain layer and one in the data access layer. It is explained that Product class in the data access layer was created by the Linq to Entity wizard.

我使用LINQ合作,SQL,我可以装饰我的灵模型类SQL属性,这样我就不必有第二类。例如。

I am working with Linq to SQL, and I could adorn my model class with Ling to SQL attributes, so that I don't have to have a second class. E.g.

[Table(Name="Customers")]
public class Customer
{
  [Column(IsPrimaryKey=true)]
  public string CustomerID;
  [Column]
  public string City;
}

不过我觉得这是混合的关注,它会到SQL数据访问层效果紧密结合我的域名层到Linq的。你是否同意这种说法?

However I feel this is mixing concerns and it will in effect tightly couple my domain layer to the Linq to SQL data access layer. Do you agree with this?

假设我创建两个客户类,域和数据访问层。比方说城市是一个必填字段。保存时,这条规则需要进行检查。如果这一领域层和数据访问层,或两者做?

Let's assume I create two 'Customer' classes, for the domain and data access layer. Let's say City is a required field. When saving, this rule needs to be checked. Should this be done in the domain layer or the data access layer, or both?

谢谢,Daryn

推荐答案

当然,夫妻那您的域名层到DAL。更糟糕的是,您的域名层实体将具有相同的结构,在你的数据库中的表。如果这些表都是关系,那么这将不会是一个域模型最好再presentation。

Absolutely, that couples your domain layer to the DAL. Even worse, your domain layer entities will have the same structure as the tables in your DB. If those tables are relational, then that won't be the best representation of a domain model.

我们要做的就是让在DAL存在LINQ到SQL实体,然后我们在DAL映射类转换L2S实体领域的实体,反之亦然。这没关系,因为DAL真的是你的ORM,而且它的工作部分是做这个映射。

What we do is let the Linq-to-SQL entities exist in the DAL, and then we have mapping classes in the DAL convert the L2S entities to domain entities, and vice versa. And that's okay, because the DAL really is your ORM, and part of it's job is to do this mapping.

我要说的是,如果需要市,为业务规则,那么这就是商业逻辑,属于在业务逻辑层业务规则。有验证包在那里可以与这个问题有帮助。

I would say that if City is required, as a business rule, then that's business logic and belongs as a business rule in the business logic layer. There are validation packages out there that can help with this issue.

这篇关于域逻辑VS数据验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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