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

  • Home
  • SEARCH
  • 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 4000122
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:44:33+00:00 2026-05-20T07:44:33+00:00

I am reading up upon the Repository usage. Sometimes I see that a repository

  • 0

I am reading up upon the Repository usage. Sometimes I see that a repository is a property of an entity. I was wondering what the pro’s and cons are.

public interface IRepository<T>
{
  T GetById(int id);
  void Update(T);
}

public class FooRepository : IRepository<Foo>
{
  public Foo GetById(int i)
  { /* code ..*/ }

  void Update(Foo)
  { /*code..*/ }
}

public class Foo
{
   public IRepository Repository {get;set;}

   public void Update()
   {
     Repository.Update(this);
   }
}

Why use this apporach? Doesn’t it makes more sense to use the repository and the entity object seperated? So that the entity object doesn’t know about any repositories?

EDIT:

But what if you have one main object and different sub-objects:

public class MainObject
{
  public int Id {get;set;}

  public List<ISubject> SubObjects {get;}
}

public interface ISubObject
{
}

public class SubObjectA : ISubObject
{
  public string SomeProperty {get;set;}
  public int Id {get;set;}

  public int MainObjectId {get;set;}
}

public class SubObjectB : ISubObject
{
  public string AnotherProperty{get;set;}
  public int Id {get;set;}

  public int MainObjectId {get;set;}
}

So SubObjectA and SubObjectB are different types but implement the ISubject interface. The main object has a list of these subobjects. Each sub-object has it’s own repository. How would you load the sub-objects?

  • 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-20T07:44:33+00:00Added an answer on May 20, 2026 at 7:44 am

    Some entity implementations require access to the context in which they are created, to implement lazy loading for example. Consider the following:

    class MyEntity
    {
        public virtual IEnumerable<string> Tags { get; private set; }
    }
    
    interface IMyEntityRepository
    {
        ...
    }
    

    You can return sub-classes of MyEntity from the an MyEntity implementation, that require a reference to the repository that created it.

    internal class MyLazyEntity : MyEntity
    {
        public MyLazyEntity(MyLazyEntityRepository repository)
        {
            this.Repository = repository;
        }
    
        public override IEnumerable<string> Tags
        {
            get
            {
                return this.Repository.LoadTagsForEntityFromXml(this.Id);
            }
        }
    }
    
    class MyLazyEntityRepository : IMyEntityRepository { }
    

    I would say, that keeping a reference to the source repository in a class derived from an entity is fine, on the other hand, the base entity class should not have knowledge about the repository.

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

Sidebar

Related Questions

Reading about the lack of persistence ignorance in Entity Framework I often stumble upon
Upon reading the Wikipedia article on bar codes , I learned that there is
In reading High performance MySQL from O'Reilly I've stumbled upon the following Another common
While reading proggit today, I came upon this comment in a post about how
While reading the OWASP site, I stumbled upon pages www.owasp.org/index.php/Category:Countermeasure and www.owasp.org/index.php/Category:Control . My
So I was reading up on indexes and their implementation, and I stumbled upon
One stumbles upon this phrase when reading about design patterns. But I don't understand
I'm reading over this query, and I came upon a line where I don't
I started reading upon html5 and I am trying to work on a project
I am learning about all things SQL, and upon reading about indexes I naturally

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.