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 6835167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:12:00+00:00 2026-05-26T23:12:00+00:00

I’m building a new project off the service repository pattern detailed here . It

  • 0

I’m building a new project off the service repository pattern detailed here. It seems to work well in the most basic of examples. In more complex scenarios is it acceptable to mix the objects in the service \ repository layers?. For example say there is a User repository and service and I want to be able to create an audit for the creation of a user, I would think this would go in the service layer.

If I follow the article the service automatically creates the user repository object in the constructor. Adding a audit would mean adding audit CRUD methods to the user repository? Does that make sense to do that?

    public UserService(IValidationDictionary validationDictionary, IUserRrepository repository)
    {
        _validatonDictionary = validationDictionary;
        _repository = repository;
    }
  • 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-26T23:12:00+00:00Added an answer on May 26, 2026 at 11:12 pm

    in my experience you dont need repositories for each entity type. Just create one repository for the whole model, and then use linq queries over it. EF already provides implementation of that repository, you can create a custom interface like shown below and implement it over that repository ..

      public interface IDataContext
        {
            void Add<T>(T entity) where T : BaseEntity;
            void Delete<T>(T entity) where T : BaseEntity;
            IQueryable<T> Find<T>(Expression<Func<T, bool>> where) where T : BaseEntity;
            int SaveChanges()
        }
    

    where your base entity is your base class for all repositories.

    most of the linq you would write would be pretty straighforward, but for the complicated ones, just write Utility classes

    in our implementation the class derived from DbContext implements this interface, and all the auditing is done through the Save Method using the ChangeTracker

    A sample implementation of EF 4.2 is below …

     public class MyContext : DbContext, IDataContext
     {
        static MyContext ()
        {
            Database.SetInitializer<MyContext >(null);
        }
    
        public T GetById<T>(int id) where T : BaseEntity
        {
            return this.Set<T>().SingleOrDefault(i => i.Id == id);
        }
    
        public void Add<T>(T entity) where T : BaseEntity
        {
            this.Set<T>().Add(entity);
        }
    
        public void Delete<T>(T entity) where T : BaseEntity
        {
            this.Set<T>().Remove(entity);
        }
    
        public IQueryable<T> Find<T>(System.Linq.Expressions.Expression<Func<T, bool>> where) where T : BaseEntity
        {
            return this.Set<T>().Where(where);
        }
    
        public override int SaveChanges()
        {
            this.SetAuditValues();
            return base.SaveChanges();
        }
    
       private void SetAuditValues()
        {
            var addedEntries = this.ChangeTracker.Entries().Where(e => e.State ==  System.Data.EntityState.Added);
            var currentUser = this.GetCurrentUser();
            foreach (var addedEntry in addedEntries)
            {
                var entity = addedEntry.Entity as BaseEntity;
                if (entity != null)
                {
                    entity.CreateDateTime = DateTime.Now;
                    entity.CreateUser = currentUser;
                    entity.ModDateTime = DateTime.Now;
                    entity.ModUser = currentUser;
                }
    
            }
    
            var modifiedEntries = this.ChangeTracker.Entries().Where(e => e.State == System.Data.EntityState.Modified);
    
            foreach (var modEntry in modifiedEntries)
            {
                var entity = modEntry.Entity as BaseEntity;
                if (entity != null)
                {
                    entity.ModDateTime = DateTime.Now;
                    entity.ModUser = currentUser;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.