How to find the user logged in to the Infra layer? (Using DDD with C# MVC EF)

Asked

Viewed 116 times

0

I’m developing a feature for storing the history of screen record modifications. Example: I have a register of people, in this register of people, at each action performed (Insert, Edit, Delete) I save in database via JSON the data of the entity at that time. This will give me all the history of modifications made in the course of time. So far it is flowing well, however I need to record the user who performed the operation, in this case in the Infra layer I do not have the user logged in but only in the UI bed.

I am using EF (as ORM) and Identity(for access control), Identity is in a project isolated in the below.

One solution would be to always transport the logged in user in the viewmodel to the domain, but it is impossible to do this... being that it runs away from any good practice, that is, I would leave in the hand of the programmer the control to always be passing the logged in user.

Another solution would be to create an audit filter in Filters. But right now I can’t always control all the data, because my operations don’t always transport viewmodel to service layer or domain and it’s complex to control whether the domain has accepted the validations of business rules. I discarded the model.

I saw output in override the EF Savechanges in the Infra layer, added a method to perform the log record. But in the Infra layer I do not have access to recover the logged in user.

My question is this: How can I recover the User logged in to the Below? And how do you deal with log history changes in your applications?

1 answer

0

You can include in the constructor of the class responsible for doing the work with the ORM (or without ORM as well) the parameter with the logged-in user ID, and in the dependency injection mapping layer you instantiate to the UI layer and call a method that returns the logged-in user, and passes in the user ID parameter in the dependency injection mapping itself, so no one needs to worry about always passing such parameter, you will have it as parameter whenever triggered some method of the class responsible for making any changes in the bank.

  • Interesting solution, I will implement and comment again here the resolution. For now, Thank you!

  • The solution did not work because of the circular reference. My UI has Ioc reference. I need to find another solution for the case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.