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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:28:09+00:00 2026-05-22T01:28:09+00:00

I have successfully setup a simple mvc application that lists teams. I’m using Ninject

  • 0

I have successfully setup a simple mvc application that lists teams. I’m using Ninject to inject the appropriate repository depending on the controller (thanks to stack overflow ;). All looks good, except that the repository code looks exactly the same. And I know that’s wrong. So my TeamRepository has two classes (for now).

public class SwimTeamRepository : ITeamRepository<SwimTeam> 
{
    private readonly Table<SwimTeam> _teamTable;
    public SwimTeamRepository(string connectionString)
    {
        _teamTable = (new DataContext(connectionString).GetTable<SwimTeam>());
    }

    public IQueryable<SwimTeam> Team
    {
        get { return _teamTable; }
    }
}

public class SoccerTeamRepository : ITeamRepository<SoccerTeam>
{
    private readonly Table<SoccerTeam> _teamTable;
    public SoccerTeamRepository(string connectionString)
    {
        _teamTable = (new DataContext(connectionString).GetTable<SoccerTeam>());
    }

    public IQueryable<SoccerTeam> Team
    {
        get { return _teamTable; }
    }
}

They look exactly the same except for the Class and Table name, so clearly I need to re-factor this. What would be the best approach here? Singleton? Factory Method?

Thanks in advance!

  • 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-22T01:28:10+00:00Added an answer on May 22, 2026 at 1:28 am

    You could use generics:

    public interface ITeamRepository<T>
    {
    }
    
    public class TeamRepository<TTeam> : ITeamRepository<TTeam>
        where TTeam : Team
    {
        private readonly Table<TTeam> _teamTable;
        public TeamRepository(string connectionString)
        {
            _teamTable = (new DataContext(connectionString).GetTable<TTeam>());
        }
    
        public IQueryable<TTeam> Team
        {
            get { return _teamTable; }
        }
    }
    
    public class Team
    {
    }
    
    public class SwimTeam : Team
    {
    }
    

    Then use it like so…

    public void MyMethod()
    {
        var repository = new TeamRepository<SwimTeam>();
    }
    

    …and set up your IoC container w/ Ninject like so…

    public class MyModule : NinjectModule
    {
        public override void Load()
        {
            Bind<ITeamRepository<SwimTeam>>
                .To<TeamRepository<SwimTeam>>();
        }
    }
    
    public void MyMethod()
    {    
        var repository = kernel.Get<ITeamRepository<SwimTeam>>();
    }
    

    If you want to get REAL generic and have a single repository for ALL of your mapped classes, you can do something like this:

    public interface IRepository
    {
        IQueryable<T> Get<T>() where T : class, new();
    }
    
    public class Repository : IRepository, IDisposable
    {
        private DataContext _dataContext;
        public Repository(string connectionString)
        {
            _dataContext = new DataContext(connectionString);
        }
    
        public IQueryable<T> Get<T>()
            where T : class, new()
        {
            return _dataContext.GetTable<T>().AsQueryable();
        }
    
        public void Dispose()
        {
            if (_dataContext != null)
            {
                _dataContext.Dispose();
                _dataContext = null;
            }
        }
    }
    

    …which you could call like so (after setting up your Ninject container)…

    using (var repository = kernel.Get<IRepository>())
    {
        var swimTeam = repository.Get<SwimTeam>();
    }
    

    Since Ninject takes care of the life-cycle management of your objects, you don’t HAVE to wrap the repository in a using statement. In fact, you don’t want to use a using statement there at all if you plan to use the repository more than once within the scope of its lifetime. Ninject will automatically dispose of it when it’s life-cycle ends.

    Here’s a good article by Rob Conery on using this kind of technique to reduce the friction of using different ORMs.

    EDIT by keeg:

    I Think

    public class TeamRepository<TTeam> : ITeamRepository<TTeam> where TTeam : Team {}
    

    Should be

    public class TeamRepository<TTeam> : ITeamRepository<TTeam> where TTeam : class {}
    

    Please correct if I’m wrong.

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

Sidebar

Related Questions

I have a .NET 3.5 Setup Package Project which installs my application successfully. The
just simple question, recently i have setup a web server to build project using
I'm trying to setup ELMAH to log errors for our application. I have successfully
Have anyone successfully managed to setup a combined Java/C++ project for Eclipse? What I
I have successfully implemented interop beftween Win32 application and managed .Net dll as described
I have successfully upgraded an MFC application which was compiled with an old version
I have created a windows application setup program, it needs to have a text
I have setup a basic application which uses the ActiveDirectoryMembershipProvider to talk to our
I built a rather simple application in Python 3.1 using PyQt4. Being done, I
I have a very simple javascript class that does an ajax call to a

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.