What is the best way to complete automated transactions using Asp.Net MVC?

I feel awkward writing code in my entire MVC application.

using(var tx = new TransactionScope()){
   blah...
   tx.Complete()
}

I would like to make this DRYer somehow.

I thought of several different options for this.

  • An action filter to declaratively designate certain actions as transactional.
  • Override OnActionExecuting in the base class of the controller and make all transactions transactions in one snapshot.

Is there one of these ideas? Any prey I should pay attention to? The second option seems to be a good way to get a lot of dead ends.

Oh yes, I also use StructureMap and the factory user controller to inject fingerprints into my controllers if anyone knows of some tricks for injecting transactions this way.

+3
2

" " . - , , . :

public interface IUnitOfWork
{
    void Start();
    void Commit();
    void RollBack();
}

UnitOfWork ORM sql. . . try-catch catch.

try
{
    unitOfWork.Commit();
} 
catch
{
     unitOfWork.RollBack();
     throw;
}

:

  • : , .
  • : .
  • : .

:

  • application_begin endrequest global.asax
  • HttpModule

StructureMap InstanceScope . StructureMap.

+2

, , NHibernate ISession ISessionProvider, . - AspNetMvcSessionProvider, ISession ISessionProvider.OpenSession(), ISession -.

OnActionExecuted ISessionProvider Commit RollBack , , - , , -.

, , - , . .

+1

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


All Articles