Implement a unit of work using DI and without EF

I have an asp.net MVC application with a lock windsor working as DI and interceptpor.

Currently, Dapper is used for ORM mappring, Dapper is a simple object for an entity mapping provider that works with an ADO.Net connection, shared objects.

We already have a repository and business service level, now I want to implement a unit of work.

I know that EF has an automatic transactional capability, and SaveChanges () makes all changes to entities on entities.

Is there a way to have Unit of Work for a non-EF, layered application?

+4
source share
1 answer

I think you are looking for some kind of AOP. Have a look at this example http://www.sharpcrafters.com/solutions/transaction . You can wrap your method in a transaction using an attribute, or even better say that it will be used for all methods from your BAL as follows:

[assembly: TransactionScope( AttributeTargetTypes = "MyBALNamespace.*", AttributeTargetTypeAttributes = MulticastAttributes.Public, AttributeTargetMemberAttributes = MulticastAttributes.Public )] 

as stated here: http://www.sharpcrafters.com/solutions/logging

Update

When using TransactionScope , try to avoid unnecessarily escalating the transaction in MSDTC. If you open two connections (even in the same database) during TransactionScope , it will immediately go to DTC, which affect performance (and sometimes this can lead to unwanted exceptions when distributing the area) - this is SQLServer2008 behavior. You must use only one compound during ore operation, closing the first before opening the other.

+1
source

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


All Articles