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

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

问题描述

我正在忙着阅读和享受 Mark Seemann 撰写的《.Net 中的依赖注入》.

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.

我的问题与第 49 页第 2 章中的两个 Product 类有关.一个在域层,一个在数据访问层.说明数据访问层中的Product类是由Linq to Entity向导创建的.

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 to SQL,我可以用 Ling to 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;
}

但是我觉得这是混合问题,它实际上将我的域层紧密耦合到 Linq to SQL 数据访问层.你同意吗?

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?

假设我为域和数据访问层创建了两个客户"类.假设 City 是必填字段.保存时需要勾选此规则.这应该在领域层或数据访问层完成,还是两者兼而有之?

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?

谢谢,达琳

推荐答案

当然,这将您的域层耦合到 DAL.更糟糕的是,您的域层实体将与数据库中的表具有相同的结构.如果这些表是关系表,那么这将不是领域模型的最佳表示.

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.

我们所做的是让Linq-to-SQL 实体存在于DAL 中,然后我们在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.

我会说,如果需要 City 作为业务规则,那么这就是业务逻辑并且属于业务逻辑层中的业务规则.有一些验证包可以帮助解决这个问题.

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.

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

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