I have a pretty generic repository that does basic CRUD for many of my business entities.
The entities enherit form a generic object that has a few fields I maintain for all objects.
eg. ModifiedBy, CreatedBy, CreatedDate, ModifiedDate.
These fields ModifiedBy and CreatedBy will always be set before any update/save.
My questions is:
Is there any way to gain access to the Identity object from my MVC web application in my repositories?
I was hoping to set the modifiedby to the identity user for any update in one shot??
Best Regard,
Rod
Well, you could access
HttpContext.Current.User.Identitydirectly in your repositories, but I wouldn’t recommend it as it will make your repositories HttpContext-dependent.Alternatives:
IIdentityparameter in your Create/Update methods, e.g.void Update(T entity, IIdentity user)Decouple the logic of “getting the current user”, e.g.:
Or even simpler:
Use an IoC container to wire things up.