biproberkay / unitofwork-3orm-practice

UnitOfWork practice with three(3) ORM
MIT License
0 stars 0 forks source link

Topic in Agenda: practicing Unit Of Work with three ORM #1

Open biproberkay opened 1 year ago

biproberkay commented 1 year ago

Practicing Unit Of Work with three ORM


implement the unit of work pattern with three different object-relational mappers (ORMs)

:

Entity Framework,

see code ```csharp public class UnitOfWork : IUnitOfWork { private readonly DbContext _context; public UnitOfWork(DbContext context) { _context = context; } public void BeginTransaction() { _context.Database.BeginTransaction(); } public void Commit() { _context.Database.CommitTransaction(); } public void Rollback() { _context.Database.RollbackTransaction(); } public IRepository GetRepository() where T : class { return new Repository(_context); } public void Dispose() { _context.Dispose(); } } ```

NHibernate,

see code ```csharp public class UnitOfWork : IUnitOfWork { private readonly ISession _session; private ITransaction _transaction; public UnitOfWork(ISession session) { _session = session; } public void BeginTransaction() { _transaction = _session.BeginTransaction(); } public void Commit() { _transaction.Commit(); } public void Rollback() { _transaction.Rollback(); } public IRepository GetRepository() where T : class { return new Repository(_session); } public void Dispose() { _session.Dispose(); } } ```

and

Dapper.

see code ```csharp public class UnitOfWork : IUnitOfWork { private readonly IDbConnection _connection; private IDbTransaction _transaction; public UnitOfWork(IDbConnection connection) { _connection = connection; } public void BeginTransaction() { _transaction = _connection.BeginTransaction(); } public void Commit() { _transaction.Commit(); } public void Rollback() { _transaction.Rollback(); } public IRepository GetRepository() where T : class { return new Repository(_connection, _transaction); } public void Dispose() { _connection.Dispose(); } } ```

In these examples, the UnitOfWork class serves as the implementation of the unit of work pattern, and it manages the lifecycle of the ORM-specific objects, such as the DbContext, ISession, or IDbConnection, as well as the transaction. The Repository class serves as the implementation of the repository pattern, and it provides a simple interface for querying and modifying data using the ORM-specific objects.

By implementing the unit of work pattern in this way, you can use the same business logic and presentation layer code with any of these ORMs, while still taking advantage of the specific


Türkçesi için tıklayınız! # Gündem Konu Başlığı: 3 ORM ile Unit Of Work pratiği
biproberkay commented 1 year ago

i guess i can write one interface for all these tree class

By creating an interface for the UnitOfWork class, you can define a consistent set of methods and properties that can be used across all three ORMs. This will allow you to write business logic and presentation layer code that is agnostic to the specific ORM that is being used.

the interface for the UnitOfWork class might look like:

see code ```csharp public interface IUnitOfWork : IDisposable { void BeginTransaction(); void Commit(); void Rollback(); IRepository GetRepository() where T : class; } ```

With this interface in place, you can create a concrete implementation of the UnitOfWork class for each ORM, as shown in the previous examples, and then use dependency injection to inject the appropriate IUnitOfWork implementation into your business logic or presentation layer classes. This will allow you to switch between ORMs simply by changing the implementation of the IUnitOfWork interface that is being injected, without having to modify any of the business logic or presentation layer code.