Active Record
Active Record Pattern gained in their time of great popularity on the wave of fashion for Ruby on Rails. In a nutshell, the essence of the pattern is as follows:
- Record: each object is, in fact, write a wrapper in the database.
- Active: this object has methods to work with: he could save himself and update. To extract used static methods of the same class.
From the perspective of the school about the PLO, all right - let the object itself serves, because it shows how thrust themselves into the database!
Active Record Pattern extremely easy to use, and he earned the love nationwide. To start with, the old-fashioned, with the creation of a database, and then prescribes the connection string, press the button, and we have finished the classes, which we can now object-oriented twist tricks.
It is clear that, in terms of “advanced” design, there are at least three deadly sins. First, the class-essence can too. This, in part, is not so urgent in view of the fact that the entire history of communications with the base sits in a base class or generate partial. But the purity of design still spoils. The second sin - the fact that the structure is forced to repeat the grade structure of the base. As a result, in the domain layer vytvoryat different countries have to focus. And the third - a consequence of the first two - Persistence Awareness (contrast Persistence Ignorance). In addition, the static type FindByXxx methods do not allow the use of mock testing facility.
Because merits can be described as simple: we do not need any ISession or DataContext, there are only objects of-fact, with whom we work, let them understand how they work with the database.
As a result, this pattern is ideal for those who are going to stop writing SQL code mixed with code, and wants to quickly and without extra effort zamutit PLO. It also works well for applications in the style of CRUD, when we want to make just some interesting interface for reading or modification of our beloved base. In general, for camp “base on top.”
The most famous analogue to. Net - framework Castle ActiveRecord, which is simply a wrapper over NHibernate. Another worth mentioning previous incarnation SubSonic, which was designed from the outset with a view to the Web, and was dotnetovskim similar to RoR-a. However, users of these frameworks over time began to strain at the deficiencies described above, and frameworks have to meet them. For example, in Castle ActiveRecord classes appeared ActiveRecordMediator, which assume the job with the base, and generally similar to the Repository.
Unit Of Work
Rather than entrust the objects themselves flounder in contact with the base, we can arrange for this special class. For example, in this NHibernate ISession, but DLinq - DataContext. Objects-fact in one way or another “is recorded in this object, and then we velim him to retain all accrued to the date of the change (usually in a single transaction).
If we use the UoW, it does not mean that we have Persistence Ignorance. For example, there are certain requirements to object-entities: for example, they must be Redefine the (virtual) properties, or they shall be heirs of a certain base class. But the fact that all communication with the base trusted professional, and the objects do not breathe on their base, clean the handle, it’s definitely a step forward.

