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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:23:36+00:00 2026-06-11T19:23:36+00:00

Learning Entity Framework, Repositories and IOC, what I am trying to achieve is to

  • 0

Learning Entity Framework, Repositories and IOC, what I am trying to achieve is to implement the repository pattern in order to change my data source provider.

Please see this example from an open-source project:

namespace MyApp.Domain.Interfaces
{
    public interface IEFContext : IDisposable
    {
    }
}

EFContext (implementing IEFContext)

namespace MyApp.Data.Context
{
    public class EFContext : DbContext, IEFContext
    {
        public const string DBConnectionName = @"MyDBName";

        public EFContext() : base(DBConnectionName)      
        {
        }

        public DbSet<Member> Member { get; set; }
    }
}

IMemberRepository

namespace MyApp.Domain.Interfaces.Repositories
{
    public interface IMemberRepository
    {
        Member GetUser(string username);
    }
}

MemberRepository (implementing IMemberRepository)

namespace MyApp.Data.Repositories
{
    public class MemberRepository : IMemberRepository
    {
        private readonly EFContext _context;

        public MemberRepository(IEFContext context)
        {
            _context = context as EFContext;
        }

        // update
        public Member GetUser(string username)
        {
            return _context.Member.SingleOrDefault(name => name.UserName.ToUpper().Contains(username.ToUpper()));
        }
    }
}

and this is my application console using Unity

Console App

namespace MyApp.ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            var container = new UnityContainer();
            container.RegisterType<IEFContext, EFContext>();
            container.RegisterType<IMemberRepository, MemberRepository>();
            var repository = container.Resolve<MemberRepository>();
            var user = repository.GetUser("johnDoe");
            Console.WriteLine(user.Email);
            Console.ReadLine();
        }
    }
}

My question is the following:

if I decide to add a new context, such as:

namespace MyApp.Data.Context
{
    public class EFContext2 : DbContext, IEFContext
    {
        public const string DBConnectionName = @"MyNewDBName";

        public EFContext2() : base(DBConnectionName)      
        {
        }

         public DbSet<Member> Member { get; set; }
    }
}

I should have only to change the dependency like:

namespace MyApp.ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            ...
            container.RegisterType<IEFContext, EFContext2>();
            ...         
        }
    }
}

but in this example my MemberRepository is loosely-coupled with my first context. So if I have to change my Context I must also change my repository.

I would be please to have your point of view on this.
Kind regards

  • 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-11T19:23:37+00:00Added an answer on June 11, 2026 at 7:23 pm
    but in this example my MemberRepository is loosely-coupled with my first context. So if I have to change my Context I must also change my repository.
    

    Yes it is, but it’s a one line change from:

    private readonly EFContext _context;  
    

    to

    private readonly IEFContext _context;  
    

    And not casting on your constructor:

    public MemberRepository(IEFContext context)            
    {                
        _context = context;            
    }
    

    Now, you can inject any kind of concrete context that implements IEFContext.

    Just think about this: What you’re trying to do with dependency injection is, well, inject dependencies. But, how do you do that? Well, you do that by generalizating those dependencies. I mean, you want to be able to use different context, then you use an interface IEFContext instead of a concrete context. That’s why your constructor expects an interface.

    But, that’s the first part, know, the problem with your code is that when you say

    _context = context as EFContext;
    

    You’re downcasting the interface and saying: This interface is an EFContext. You’re loosing the generality. Know when you try to inject a, say, SQLContext, you’ll not be able since although it’s and IEFContext it isn’t an EFContext. That’s why you remove the as EFContext part and just let

    _context = context;
    

    Now as for the second part, being general and expecting and receiving an interface is just the first part. You now need to tell the compiler that you’re expecting a generic context IEFContext, but you need also tell him that no matter which context you receive you should be able to, say, GetUsers. That’s where your interface comes into play. You know declare the property public Member GetUser{} within your interface forcing and guaranteeing then that no matter which context arrive you’ll be able to get your users.

    I hope this to be a little simple to understand. Good luck.

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

Sidebar

Related Questions

I am learning Entity Framework 4, and I want to use the Repository pattern
I am learning repository pattern and was reading Repository Pattern with Entity Framework 4.1
I am learning ADO.NET entity framework. This is my first example. The datagrid seems
I am learning to develop windows forms application with Entity Framework using Model View
I am learning EF Code First from Programming Entity Framework Code First. The following
I am just learning to use the Entity framework and I wondered how I
I am learning in the visual web developer c#, entity framework, and webservices. I
I'm learning ASP.NET MVC 3 with Entity Framework Code First. I'm following a tutorial
I just recently started learning and using the ADO.NET Entity Framework and ran into
OK, so I am learning the Entity Framework and LINQ to connect 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.