坚持无知的好处是什么? [英] What are the benefits of Persistence Ignorance?

查看:96
本文介绍了坚持无知的好处是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是DDD + TDD世界中的新手。但是我从事编程已经有将近9年了。

I am a newbie in the DDD+TDD World. But I have been in programming for almost 9 years.

有人可以解释一下持久性ignornace的好处吗?典型的nHibernate应用程序只是将类和数据库之间的依赖关系推送到映射文件。

Can someone please explain me the benefits of persistance ignornace ? Typical nHibernate application just pushes the dependency between class and database to mapping files.

如果我更改了类文件或数据库,则必须更改映射文件。那么,这不只是通过增加一个抽象层来推动依赖关系吗?在我看来,到目前为止,我认为这不是革命性的。但是我不确定是否丢失了某些东西。

If I change Class files or Database, I have to change the mapping files. So isn't it just pushing the dependency by adding one more abstraction layer? In my opinion so far I don't think it's anything revolutionary. But I am not sure if I am missing something.

最后如何测试映射文件?

Finally How can I test the mapping files ? There are chances bugs will appear from mapping files, how can I test them?

推荐答案

让我举一个例子来解释这个问题。假设您正在使用经典SQL方法实现应用程序。您打开记录集,更改数据并提交。

Let me explain this with an example. Lets assume your are implementing an application using a classical SQL approach. You open recordsets, change data and commit it.

伪代码:

trx = connection.CreateTransaction();
query = connection.CreateQuery("Select * from Employee where id = empid");
resultset = query.Run();
resultset.SetValue("Address_Street", "Bahnhofstrasse");
resultset.SetValue("Address_City", "Zürich");
trx.Commit();

使用NHibernate看起来像这样:

With NHibernate it would look something like this:

emp = session.Get<Employee>(empid);

// persistence ignorant 'logic'
emp.Address.Street = "Bahnhofstrasse";
emp.Address.City = "Zürich";

session.Commit();

持久性无知意味着业务逻辑本身并不了解持久性。换句话说,持久性与逻辑是分离的。

Persistence Ignorance means that the business logic itself doesn't know about persistence. Or in other words, persistence is separated from logic. This makes it much more reusable.

将逻辑移至可重复使用的方法:

Move 'logic' to a reusable method:

void MoveToZuerichBahnhofstrasse(Employee emp)
{
  // doesn't have anything to do with persistence
  emp.Address.Street = "Bahnhofstrasse";
  emp.Address.City = "Zürich";
}

尝试使用结果集编写这种方法,您就会知道持久性无知是什么。

Try to write such a method using resultsets and you know what persistence ignorance is.

如果您不确定,请看看单元测试有多简单,因为与持久性相关的东西没有任何依赖关系:

If your are not convinced, see how simple a unit test would be, because there aren't any dependencies to persistence related stuff:

Employee emp = new Employee();
MovingService.MoveToZuerichBahnhofstreasse(emp);
Assert.AreEqual("Bahnhofstrasse", emp.Address.Street);
Assert.AreEqual("Zürich", emp.Address.City);






DDD有所不同。在那里,您首先建立域模型(类模型),然后根据它创建数据库设计。使用NH,这非常简单,因为-由于持久性的无知-您可以在拥有(权威的)数据库模型之前编写和进行模型和逻辑的单元测试。


DDD is something different. There you build up your domain model first (class model) and create the database design according to it. With NH this is very simple, because - thanks to persistence ignorance - you can write and unit test the model and logic before having a (definitive) database model.

测试:我们正在通过创建实体的实例,将其存储到数据库,将其取回并进行比较来测试映射。这是通过大量反射自动完成的。但是您不必走太远。尝试存储实体时,大多数典型错误都会出现。

Testing: We are testing mappings by creating an instance of the entity, storing it to the database, getting it back and compare it. This is done automatically with lots of reflection. But you don't need to go so far. most of the typical errors show up when trying to store an entity.

您可以对查询执行相同的操作。复杂的查询值得测试。如果查询完全被编译,这将是最有趣的。您甚至不需要任何数据。

You could do the same with queries. Complex queries deserve a test. It's most interesting if the query gets compiled at all. You don't even need any data for this.

对于数据库集成测试,我们使用的是 Sqlite 。这非常快。 NH使用SchemaExport实时生成内存数据库(每次测试之前)。

For database integration tests, we are using Sqlite. This is pretty fast. NH produces the in-memory database on the fly using SchemaExport (before each test).

这篇关于坚持无知的好处是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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