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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:26:43+00:00 2026-05-14T02:26:43+00:00

I am wondering if anyone has any good tutorials(or maybe even a library that

  • 0

I am wondering if anyone has any good tutorials(or maybe even a library that is already made and well documented) on making a generic repository.

I am using currently linq to sql but it might change so I don’t know if you can make a generic repository that would take little to no changes if I would say switch to entity framework.

Thanks

I think I should also add why I want a generic repository. The reason is in my database I have like corporate tables(users who’s subscriptions are paid by someone else) and individual tables(people who find my site through google or whatever and pay for their own subscription)

But I will have 2 very similar tables. For instance I have 2 settings tables one for corporate users and one for the individuals.

Now since they are 2 different tables I need 2 different insert methods as I am inserting it into 2 different tables and at this current time only one field is different(that is the PK).

So now I need all these duplicate methods and I don’t want that. Maybe what I have in my database is could be considered as a design flaw(and maybe it is) but one of the reasons behind this was if needed I can break up my database into 2 different databases very easy and I am not going to change my design anytime soon.

  • 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-14T02:26:44+00:00Added an answer on May 14, 2026 at 2:26 am

    Here is my answer to another question of the same type. Hope it helps:

    Advantage of creating a generic repository vs. specific repository for each object?

    Edit:

    It sounds like you want to treat two concrete types as one logical type. To do that, first define the logical type:

    public interface ISubscription
    {
        // ...
    }
    

    Then, define the concrete types as part of your data model (interfaces would be implemented in another partial class):

    [Table("CorporateSubscription")]
    public partial class CorporateSubscription : ISubscription
    {
    
    }
    
    [Table("IndividualSubscription")]
    public partial class IndividualSubscription : ISubscription
    {
    
    }
    

    Next, define the repository which operates on the logical type:

    public interface ISubscriptionRepository
    {
        CorporateSubscription GetCorporate(string key);
    
        IndividualSubscription GetIndividual(int userId);
    
        IEnumerable<ISubscription> ListAll();
    
        IEnumerable<CorporateSubscription> ListCorporate();
    
        IEnumerable<IndividualSubscription> ListIndividual();
    
        void Insert(ISubscription subscription);
    }
    

    Finally, implement the interface by using both tables:

    public class SubscriptionRepository : ISubscriptionRepository
    {
        private readonly YourDataContext _dataContext;
    
        public SubscriptionRepository(YourDataContext dataContext)
        {
            _dataContext = dataContext;
        }
    
        #region ISubscriptionRepository
    
        public CorporateSubscription GetCorporate(string key)
        {
            return _dataContext.CorporateSubscriptions.Where(c => c.Key == key).FirstOrDefault();
        }
    
        public IndividualSubscription GetIndividual(int userId)
        {
            return _dataContext.IndividualSubscriptions.Where(i => i.UserId == userId).FirstOrDefault();
        }
    
        public IEnumerable<ISubscription> ListAll()
        {
            return ListCorporate()
                .Cast<ISubscription>()
                .Concat(ListIndividual().Cast<ISubscription>());
        }
    
        public IEnumerable<CorporateSubscription> ListCorporate()
        {
            return _dataContext.CorporateSubscriptions;
        }
    
        public IEnumerable<IndividualSubscription> ListIndividual()
        {
            return _dataContext.IndividualSubscriptions;
        }
    
        public void Insert(ISubscription subscription)
        {
            if(subscription is CorporateSubscription)
            {
                _dataContext.CorporateSubscriptions.InsertOnCommit((CorporateSubscription) subscription);
            }
            else if(subscription is IndividualSubscription)
            {
                _dataContext.IndividualSubscriptions.InsertOnCommit((IndividualSubscription) subscription);
            }
            else
            {
                // Forgive me, Liskov
                throw new ArgumentException(
                    "Only corporate and individual subscriptions are supported",
                    "subscription");
            }
        }
        #endregion
    }
    

    Here is an example of an insert. Don’t get too wrapped up in the presenter class; I just needed a situation in which subscriptions would be created based on a flag:

    public class CreateSubscriptionPresenter
    {
        private readonly ICreateSubscriptionView _view;
        private readonly ISubscriptionRepository _subscriptions;
    
        public CreateSubscriptionPresenter(
            ICreateSubscriptionView view,
            ISubscriptionRepository subscriptions)
        {
            _view = view;
            _subscriptions = subscriptions;
        }
    
        public void Submit()
        {
            ISubscription subscription;
    
            if(_view.IsCorporate)
            {
                subscription = new CorporateSubscription();
            }
            else
            {
                subscription = new IndividualSubscription();
            }
    
            subscription.Notes = _view.Notes;
    
            _subscriptions.Insert(subscription);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.