NHibernate ITransaction and the clean domain model

I am trying to write my domain model as non-violent as possible . The only thing I am doing right now is to note every property and method virtual, since NHibernate requires that for lazy loading.

In my model assembly I define some repository interfaces:

public interface IRepository<TEntity> where TEntity : EntityBase {
    TEntity Get(int id);
    /* ... */
}
public interface IProductRepository : IRepository<Product> { ... }

Then I have a data assembly . This one will refer to NHibernate, he knows about its existence. This is the assembly that implements these repository interfaces:

public abstract class Repository<TEntity> : IRepository<TEntity> {
    public TEntity Get(ind id) { ... }
    /* ... */
}
public class ProductRepository : Repository<Product>, IProductRepository {
    /* ... */
}

etc.

. BeginTransaction IRepository. , NHibernate.ITransaction, , NHibernate .

?

void BeginTransaction(), a void Commit() a void RollBack() , ITransaction ?

ITransaction, , ?

!

+3
2

Sharp Architecture, , , . , IRepository DbContext, ( ). ( , NHibernate). .

, S # arp , .

+2

IMO -, , , , , .

, NH, "" ( ), "BeginTransaction" :

, , :

  public void RegisterCustomer(Customer customer)
    {
        try
        {
            using(var transaction = _session.BeginTransaction())
            {
                _customerRepository.Save(customer);
                _customerSurveyRepository.Save(customerSurvey);
                // DO What ever else you want...
                transaction.Commit();
            }
        }
        catch (Exception exn)
        {
            throw new AMException(FAILED_REGISTRATION, exn);
        }
     }

, SessionFactory ...

0

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


All Articles