达尔。数据约束建模最佳实践

发布于 2024-09-15 15:00:14 字数 782 浏览 1 评论 0原文

存储在具有数据约束的关系数据库中的数据(例如最大字符串属性长度)。客户端使用数据访问库 (DAL) 以 ORM 方式管理数据(存储库 + 数据域类)

您个人会在哪里实现约束? 例如:

数据域类:

class Person
{
 private string _name;
 public string Name 
 {
   get { return _name; }
   set { _name = StringHelper.Truncate(value, 50) }
 }
 ...
}

或者可能是存储库:

PersonRepository {
  public void CreatePerson(Person p) {
    p.Name = StringHelper.Truncate(p.Name, 50);
    ... 
    DataContext.Insert(..);
  }
}

或者您可能应该使用分配给数据域类属性的属性,这些属性将通过自动字符串字段截断的反射在存储库方法中进行处理。

class Person {
   [StringConstraint(MaxLength = 50)]
   public string Name { get; set; }
} 

PersonRepository::CreatePerson(p) {
  EntityHelper.ApplyConstraints(p);
  ...
}

或者可能是别的什么?

先感谢您!

Data stored in relation database with data constraints (for example maximum string property length). Clients use Data Access Library (DAL) to manage the data in ORM manner (repositories + data domain classes)

Where would you personally implement constraints?
For example:

Data domain classes:

class Person
{
 private string _name;
 public string Name 
 {
   get { return _name; }
   set { _name = StringHelper.Truncate(value, 50) }
 }
 ...
}

Or may be repository:

PersonRepository {
  public void CreatePerson(Person p) {
    p.Name = StringHelper.Truncate(p.Name, 50);
    ... 
    DataContext.Insert(..);
  }
}

Or may be you shall use attributes assigned to data domain classes properties that would be processed in repository methods via reflection for automated string fields truncation.

class Person {
   [StringConstraint(MaxLength = 50)]
   public string Name { get; set; }
} 

PersonRepository::CreatePerson(p) {
  EntityHelper.ApplyConstraints(p);
  ...
}

Or may be something else?

Thank you in advance!

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

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

发布评论

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

评论(3

天冷不及心凉 2024-09-22 15:00:14

我会在业务逻辑层中使用与数据库相关类不同的业务逻辑类来完成此操作。我不会冒泡 SQL 错误,甚至不会在数据库中强制执行它。在我看来,数据库应该只关心数据的形状和一致性。与实际数据本身无关。

您关于 50 个字符的规则是业务规则,而不是逻辑、关系或“结构”规则。它可以很容易地是 51 个字符或 49 个字符。您任意选择 50 个字符,这很好。这就是业务层对象的用途。执行这些规则。

任何对您数据的业务访问都应该通过业务层、通过服务、直接引用或您选择的其他方法公开。

对数据库的唯一“直接”访问应该再次是关心数据的形状和一致性的事情,而不是实际数据本身。诸如备份、复制、集群、负载平衡、审计等。

因此,ORM 类只是 Person 的业务对象和实际数据库中 Person 的存储之间的粘合剂。数据库应该只关心数据库的整体形状和结构以及实际数据存储的底层基础设施和机制。业务对象应该确定对象的“性质”并定义它的真正含义。一个人至少应该有一个名字,他们的年龄不能超过 110 岁,他们的身高不能超过 7 英尺,等等。

无论如何,这就是我的哲学/经验法则:-)

I would do it in the business logic layer, with a business logic class that is different from the database related classes. I wouldn't bubble up the sql error or even enforce it in the database. In my opinion the database should only be concerned with the shape and consistency of the data. Not with the actual data itself.

Your rule about 50 characters is a business rule, not a logical, relational or "structural" rule. It could just as easily be 51 characters or 49. You arbitrarily picked 50, which is fine. That is what the business layer objects are there for. To enforce those rules.

Any business access to your data, should again be through the business layer, exposed via services, direct reference or some other method you choose.

The only "direct" access to your database should again be things which care about the shape and consistency of your data, not the actual data itself. Things like backup, replication, clustering, load balancing, auditing etc.

So the ORM classes are just the glue between a business object of Person and the storage of Person in an actual database. The database should only care about the overall shape and structure of the database and the underlying infrastructure and mechanics of the actual data storage. The business object should determine the "nature" of the object and define what it truly is. That a Person should always have at least a firstname, that their age cannot be greater than 110 years, their height cannot be more than 7 feet, etc. etc.

Thats my philosophy/rule of thumb anyway :-)

梦里泪两行 2024-09-22 15:00:14

约束是验证的一部分。验证对象是业务层的责任,但在 UI 中使用相同的验证逻辑也很方便。共享此类逻辑的方法是使用一些 API,用验证属性标记数据对象。您可以在 BL 和 UI 中运行相同的验证。 DataAnnotations 提供了此功能,也可以通过 Validation 应用程序块来实现。

编辑:这并不意味着您不会在数据库中放置约束。这仅意味着您应该能够尽快检测到约束违规,以将往返保存到数据库。

Constraints are part of validation. It is responsibility of Business layer to validate objects but it is also convenient to use same validation logic in UI. The way to share such logic is to use some API which marks your data objects with validation attributes. You can than run the same validation in BL and UI. DataAnnotations offers this functionality and it can be also achieved with Validation application block.

Edit: It doesn't mean that you will not place constraints in DB. This only mean that you should be able to detect constraint violation as soon as possible to save the round trip to database.

吲‖鸣 2024-09-22 15:00:14

在数据库中是我的偏好。
如果有人尝试插入太长的字符串,则会返回 SQL 错误。由应用程序通过向用户显示有用的消息来处理该错误。

它也是集中式的,以防有人有备用访问权限 - SSMS 或其他应用程序代码。

In the database is my preference.
If someone attempts to insert a string that's too long, a SQL error will be returned. It's up to the application to handle that error by displaying a useful msg to the user.

It's also centralized in case someone has alternate access - SSMS, or other application code.

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