Aljo_M
Aljo_M
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
okay thank you :)
93 replies
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
😂
93 replies
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
so i created a scoped service and kept the middleware. in the middleware i run the scoped service to create transaction and then set the session object into context like this:
context.Items["MongoSession"] = session;
context.Items["MongoSession"] = session;
this is just to play around. what do you think about it?
93 replies
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
okay thanks
93 replies
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
sure
93 replies
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
i get it
93 replies
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
yes okay
93 replies
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
yes ofcourse
93 replies
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
yes i understand that
93 replies
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
i saw some example, and didnt want to use start transaction in all my methods
93 replies
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
i was asking what is better to do instead of that, in case that is not common practice?
93 replies
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
okay, i can make it transient
93 replies
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
why is UserService scoped?
93 replies
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
what would you do?
93 replies
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
but okay UserService is scoped
93 replies
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
i am a complete beginner, sorry
93 replies
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
i actually have dbClient and all dbManagers registered as singletons
93 replies
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
how to implement it here?
93 replies
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
this is in my UserService.cs
93 replies
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
let me just give you example:
public async Task<BaseResponse> RemoveUserAsync(string userId)
{
var user = await _userManager.GetByIdAsync(userId);
if (user == null)
throw new UserNotFoundByIdException(userId);

var removeFromCompanyResult = await
_companyManager.RemoveUserFromAllCompaniesAsync(user.UserName);
if (!removeFromCompanyResult.IsAcknowledged || removeFromCompanyResult.ModifiedCount > 1)
throw new DatabaseErrorException($"Failed to remove user from companies that have it. User was not removed, run it again.", removeFromCompanyResult);

var deleteUserResult = await _userManager.RemoveOneAsync(userId);
if (!deleteUserResult.IsAcknowledged || deleteUserResult.DeletedCount != 1)
throw new DatabaseErrorException($"Failed to remove user.", deleteUserResult);

return BaseResponse.Succeeded("User deleted successfully");
}
public async Task<BaseResponse> RemoveUserAsync(string userId)
{
var user = await _userManager.GetByIdAsync(userId);
if (user == null)
throw new UserNotFoundByIdException(userId);

var removeFromCompanyResult = await
_companyManager.RemoveUserFromAllCompaniesAsync(user.UserName);
if (!removeFromCompanyResult.IsAcknowledged || removeFromCompanyResult.ModifiedCount > 1)
throw new DatabaseErrorException($"Failed to remove user from companies that have it. User was not removed, run it again.", removeFromCompanyResult);

var deleteUserResult = await _userManager.RemoveOneAsync(userId);
if (!deleteUserResult.IsAcknowledged || deleteUserResult.DeletedCount != 1)
throw new DatabaseErrorException($"Failed to remove user.", deleteUserResult);

return BaseResponse.Succeeded("User deleted successfully");
}
93 replies