[CSHARP-2455] TransactionScope like implementation for IClientSessionHandle Created: 13/Dec/18  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: API, Session Management
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Ivan Artemov Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to CSHARP-2021 Implement Drivers Sessions API Closed
Backwards Compatibility: Fully Compatible

 Description   

Current SessionApi is simple, but not fit to developer requirements.

When i need use session across many repositories - it need to pass IClientSessionHandle to each repository method. But repository interface is domain contract and we need some ambient implementation.

This api must be optional without any incomatibility to other driver api

Example:

 

public interface IUserRepository
{
   Task<User> Create(IClientSessionHandle sessionHandle, User user);
}
public interface IRolesRepository
{
   Task AssignRoles(IClientSessionHandle sessionHandle, string login, List<Roles> roles);
 }

 

 It is not usefull and clear

We want to see something like this:

 

public interface IUserRepository
{
   Task<User> Create(User user); //clear
}
public interface IRolesRepository
{
    Task AssignRoles(string login, List<Roles> roles); //clear
 }
public class AccountService : IAccountService
{
      private readonly IUserRepository _userRepository;
      private readonly IRolesRepository _rolesRepository;
 
       private readonly ISessionFactory _sessionFactory;
 
        public AccountService(IUserRepository userRepository, IRolesRepository rolesRepository, ISessionFactory sessionFactory)
       {
     _userRepository = userRepository;
        _rolesRepository = rolesRepository;
 
     _sessionFactory = sessionFactory;
 }
 
 public async Task CreateUser(UserModel user)
 {
       using (_sessionFactory.Create()) //session factory can contain our abstraction
       {
         //operation auto join to current session like TransactionScope in ADO.NET
         await _userRepository.Create(user);
         await _rolesRepository.AssignRoles(user.login, roles);
        }
  }
}

 



 Comments   
Comment by Ayesh Silva [ 03/Sep/21 ]

Try this example

https://alexalvess.medium.com/getting-started-with-net-core-api-mongodb-and-transactions-c7a021684d01

Comment by zeng pu [ 11/Mar/20 ]

I also follow this,this is very important function

Comment by shubham saini [ 05/Jan/20 ]

The above approach does not seem to work with the latest driver. We need to pass in the session explicitly with each insert, update, delete operation. It makes a compelling case for creating a ResourceManager that can be enlisted under Transaction Manager.

Comment by Ivan Artemov [ 17/Dec/18 ]

using (var session = _sessionFactory.Create())      
{       
   session.BeginTransaction();
         
   try
   {
        await _userRepository.Create(user);          
        await _rolesRepository.AssignRoles(user.login, roles); 
         
        session.Commit();
   }
   catch
   {
       session.Rollback();
   }  
}   

Generated at Wed Feb 07 21:42:36 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.