Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
How to Get Logged User in Trigger? #23
I want to save the information about the user that inserted and/or modified the entry. This is being a pain, since I need to use the static constructor to get this information, and ASP.NET Core uses a lot of IoC/DI patterns that make the task almost impossible.
The idea was to use a class like this to get the user:
But I think including this as an
AFAIK, now this use is considered "an anti-pattern", so I had to implement this service to get the user from a
Here is a sample using
It would be nice having this feature handy at
I agree that it would be a good feature in
I'm thinking the best way to do this is have the entry object contain a reference to the DI container being used. That approach would require the code to register the DI container instance with the library. But, there is a mismatch because
So I'm thinking a breaking API change is required so it doesn't end up with a mismatched design.
Not sure if it's still relevant or not, but since I know my DbContext will always be used in a request, and I want to track the user used to update each object, I just inject IHttpContextAccessor like do.
To inject a service, either use the
Triggers<Entity, Context>.GlobalInserting.Add<IHttpContextAccessor>(entry => Console.WriteLine(entry.Service.HttpContext.User));
You can also inject multiple services easily with a value tuple...
Triggers<Entity, Context>.GlobalInserting.Add<(IHttpContextAccessor Hca, OtherService Os)>(entry => Console.WriteLine(entry.Service.Hca.HttpContext.User + " " + entry.Service.Os.Foo));
You can also inject instance of