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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:52:37+00:00 2026-06-14T16:52:37+00:00

I am using Entity framework 5 and using repository pattern. Say I got these

  • 0

I am using Entity framework 5 and using repository pattern. Say I got these entities Customer, Files, Images, Tasks, Invoice, User.

Each entity (apart from Customer) has a foreign key of Customer. When a user logs in I store the customerid in session (aps.net mvc). What I want is any CRUD taken on all entities to be limited to the customer who’s user is logged in. e.g I can’t afford to delete a Task belonging to customer 1 to be deleted by user who is from customer 2.

Is adding an argument of customerid for each method of repositories the best way to achieve this or are there any better/clever ways of doing it?

  • 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-06-14T16:52:37+00:00Added an answer on June 14, 2026 at 4:52 pm

    Tricky to give a definitive answer but you could make it a bit more extensible by implementing higer order functions, like this:

    public interface IRepository<T>
    {
        public T GetBy(Expression<Func<T, bool>> query)
    }
    
    
    public class FileRepository : IRepository<File>
    {
        public File GetBy(Expression<Func<T, bool>> query)
        {
            using(var context = new FilesContext())
            {
                return context.Files.Where(query).FirstOrDefault();
            }
        }
    
    }
    
    public class SomeController
    {
        private IRepository<File> _repo;
    
        public SomeController(IRepository<File> repo)
        {
            _repo = repo;
        }
    
       public ActionResult Index()
       {
           var model = _repo.GetBy(f => f.CustomerId == Session.Whatever.CustomerId);
    
           return View(model);
       }
    
    }
    

    This way you can vary the search query when required, rather than tie yourself in to using a hardcoded customer id property. For example, if you wanted to get the File object by the FileID, not the CustomerID, then:

    var model = _repo.GetBy(f => f.FileId == someId);
    

    and that’s the only part of the code that needs to change.

    Some really good info on Higher Order functions and functional programming in C# here: http://www.codeproject.com/Articles/375166/Functional-programming-in-Csharp

    Edit:

    You might be able to isolate the “Always use the customer ID when hitting DB” into a repository of it’s own, using a decorator style pattern, thus: (massive disclaimer – I haven’t tested this, but something along these lines should work)

    public class SpecialFileRepo : IRepository<File>
    {
        private readonly IRepository<File> _baseRepo;
    
        public SpecialFileRepo(IRepository<File> baseRepo)
        {
            _baseRepo = baseRepo;
        }
    
        public SpecialFileRepo() : this(new FileRepository())
        {
    
        }
        public File GetBy(Expression<Func<File, bool>> query)
        {
            var parameters = query.Parameters;
            var newParam = Expression.Parameter(typeof (File), "f");
    
            var additionalQuery = Expression.AndAlso(query.Body,
                                                     Expression.Equal(
                                                         Expression.PropertyOrField(newParam, "CustomerId"),
                                                         Expression.Constant(HttpContext.Current.Session["customerId"])));
    
            var newQuery = query.Update(additionalQuery, parameters);
    
    
            return _baseRepo.GetBy(newQuery);
        }
    }
    

    Then anything that’s talking to a repository, as far as it’s concerned, it’s just a base repository, but this class is sitting in between and always grafting the “customerid = sessionwhatever” expression onto what finally gets passed to the database. And of course, anything that only cares about using the base repository, can still do so.

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

Sidebar

Related Questions

I am using Entity Framework, POCO entities and repository pattern for data access. I
I'm getting into using the Repository Pattern for data access with the Entity Framework
I am using Entity Framework v4 and the repository pattern found in the answer
I'm using entity framework with POCOs and the repository pattern and am wondering if
I'm using Entity Framework 4.3 with a unit-ofwork/repository pattern. In this instance, the unit
I am using: Asp.net MVC3 Ninject Repository pattern with Entity Framework 4.0 On an
I'm using the Repository and Unit Of Work Pattern with Entity Framework 5 I
I am using Entity Framework with the generic repository pattern. I have used the
I am using Entity Framework 4.1 and the repository pattern . I am trying
I'm trying to implement the repository pattern using entity framework code first rc 1.

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.