I have a question about how to proceed with the following situation:
I have a DDD project where I wanted somehow in the infrastructure layer to save the user data and be able to keep it throughout the session so I can use this data within the infrastructure itself, to make validations of which company this user is.
I created in UnitOfWork that is in the Repository layer that uses my context and maps to the other layers (APP,SERVICE) a User object.
public class UnitOfWork : IUnitOfWork
{
private readonly CRMVarejoContexto _db;
public User _user { get; set;}
public UnitOfWork(CRMVarejoContexto db)
{
_db = db;
}
public UnitOfWork()
{
_db = new CRMVarejoContexto();
}
And in the App layer I created a method passing a User Guid, reaching the UnitOfWork (Repository) and using the following query:
public void LoadLoggedUser(Guid UserId)
{
_user = _db.CatchLoggedUser(UserId);
}
Which in the Context performs a direct query and returns the user:
public User CatchLoggedUser(Guid UserId)
{
var rawQuery = Database.SqlQuery<User>("SELECT * FROM dbo.IDENTITYUSER WHERE UserId = '" + UserId + "';");
var task = rawQuery.FirstOrDefault();
return task;
}
My idea was to set this object throughout the session so I could always instantiate this property in UnitOfWork and pull the user's data. What's the best way to do this?