Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 826225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:22:55+00:00 2026-05-15T03:22:55+00:00

I have a pretty generic repository that does basic CRUD for many of my

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:22:56+00:00Added an answer on May 15, 2026 at 3:22 am

    Well, you could access HttpContext.Current.User.Identity directly in your repositories, but I wouldn’t recommend it as it will make your repositories HttpContext-dependent.

    Alternatives:

    • Add an explicit IIdentity parameter in your Create/Update methods, e.g. void Update(T entity, IIdentity user)
    • Decouple the logic of “getting the current user”, e.g.:

      class GenericRepository<T>: IRepository<T> {
          private readonly ICurrentUserFetcher currentUserFetcher;
          public GenericRepository<T>(ICurrentUserFetcher currentUserFetcher) {
              this.currentUserFetcher = currentUserFetcher;
          }
          public void Update(T entity) {
              var currentUser = currentUserFetcher.Get();
              ...
          }
      }
      interface ICurrentUserFetcher {
          IIdentity Get();
      }
      class WebCurrentUserFetcher: ICurrentUserFetcher {
          public IIdentity Get() {return HttpContext.Current.User.Identity;}
      }
      

      Or even simpler:

      class GenericRepository<T>: IRepository<T> {
          private readonly Func<IIdentity> currentUserFetcher;
          public GenericRepository<T>(Func<IIdentity> currentUserFetcher) {
              this.currentUserFetcher = currentUserFetcher;
          }
          public void Update(T entity) {
              var currentUser = currentUserFetcher();
              ...
          }
      }
      var repo = new GenericRepository<Person>(() => HttpContext.Current.User.Identity);
      

      Use an IoC container to wire things up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty generic Article model, with m2m relation to Tag model. I
I have pretty big background of .net, and I've decided that i want to
I might have pretty basic question about regex. I have the following regex, which
I have a basic slider operating on a site. I have pretty hi-res images
I have a pretty complicated function which does a couple of AJAX posts on
I have a form that performs calculations live as the user enters data. However,
I am pretty bad at stating my problem clearly. Sorry. Basically, I have many
I have a lot of unit tests that pretty much tests the same behavior.
I have some data which I originally stored in a generic Javascript object, with
When working on a repository i generaly try to keep the method pretty generic,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.