As abstract NHibernate, to avoid hard addiction and facilitate testing

Is it possible to use the use of O / RM, for example NHibernate or Entity Framework, and abstract it so that it can be replaced if there is a situation that O / RM cannot handle.

It seems tempting to create a service with short service methods, within which the session is created, the session is used to get / unload entities and then used to save all dirty objects.

I would consider a repository template so that a service operation requests a repository for entities and an O / RM session is embedded in the repository. But what do you do with storing related objects, and also that the T entity flush method changes immediately. It seems simple and generally unsatisfactory.

What I am referring to right now is one O / RM shell class that provides an interface with generic methods like "StartSession", "EndSession", "AbandonSession", "GetById (object id)", etc.

At the very least, this can lead to OR / M being faked during testing, which is another big problem.

I think I'm saying that I don’t want to closely interconnect the business logic and the O / RM data access code, because switching to another O / RM can replace most of this code.

What do people do in the real world?

+3
source share
3 answers

My strong opinion would be to do some tests on smaller projects and select a toolkit to use the front, and then defer the solution until it creates an abstract shell that you can change “someday”. In my experience, that someday will never come, and the complexity that you introduce is not worth it.

nHibernate , - . ORM - . , ( ) , , , , .

, , , , , ORM. -, nHibernate... , nHibernate.

, , , . , , , , , .

, ,

+8

C/U/D . , , . Linq , . , :

interface IRepository<T> : IQueryable<T>
{
   void Add(T entity);
   void Remove(T entity);
   T Get(Guid id);
}

interface IUnitOfWork : IDisposable
{
   void RollBack();
   void Commit();
}

// I don't do this every time, the generic repository is enough for almost everything.
[Example]
interface IOrderRepository : IRepository<Order>
{
   IList<Order> GetOrdersForUser(User user);
}

// This interface is only used in the repository implementation
interface INHiberanteUnitOfWork : IUnitOfWork
{
  ISession Session { get; }
}

, , . NHibernate + Unit of work, .

, , , . fluentnhibernate .

+1

- POCOs, BoostMap - OR/. OR/M, , iQueryable , : -)

0

Source: https://habr.com/ru/post/1736386/


All Articles