I have a business layer, with a Order class and Item class. Suponse that I want to add a new line to the order. How the business logic has not known about the data access layer, I will use a service layer, with a method that is AddItemToOrder, something like this:
public Order AddItemToOrder(Order paramOrder, Item paramItem)
{
//Get order from database with EF Core
//Add item to order
//Save changes in database.
}
Well, I some small prject, I have mixed business logic and data access logic in this way, having a method in my Order class, in the busisiness layer:
public order AddItem(Item paramItem)
{
using (MyDbContext = new DbContext(options))
{
string myStrSql = "select * from Order where IDOrder = {0} with(UPDLOCK)";
Order myOrderDb = myDbContext.Order.FromRawSql(myStrSql, 1).Include(x => x.Items).FirstOrDefault();
//Some validations, for example number of items <= 20
//if validations are correct
myOrderDb.Items.Add(paramItem);
SaveChanges();
}
}
In this case, I can use pessimistic concurency because I am using EF directly in my business logic, but I think that it is not very correct.
the other option, in the service, I get the order first, but I can't use pessimistic concurrency becuase the dbContext is disponse after I get the data.
So I am wondering if it is possible to use pessimistic concurrency if I separate business logic layer and data access layer.
Perhaps another option it would be use optimistic concurrency, but I would like to know if it would be possible to use pessimistic concurrence, and how it would be done.
Thanks.