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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:25:26+00:00 2026-05-27T06:25:26+00:00

I have an MVC3 solution where I have 2 repositories & 2 service layers.

  • 0

I have an MVC3 solution where I have 2 repositories & 2 service layers. I also have separate every layer: controller <–> service <–> repository

Previously, in each of my repositories I had a separate data context. I mean I created a new object instance in each repository. I knew this is not the good way.

So I refactored my solution in order to share my data context between my repositories. Below is the skeleton of my solution. I would like your opinion.

public class EntityFrameworkDbContext: DbContext, IUnitOfWork
{
    public DbSet<Project>           Projects            { get; set; }
    public DbSet<Technology>        Technologies        { get; set; }

    public void Save()
    {
        SaveChanges();
    }
}

public interface IUnitOfWork
{
    void Save();
}

The nInject bindings

ninjectKernel.Bind<IUnitOfWork>().To<EntityFrameworkDbContext>().InRequestScope();
ninjectKernel.Bind<IMainRepository>().To<MainRepository>();
ninjectKernel.Bind<IAdminRepository>().To<AdminRepository>();
ninjectKernel.Bind<IMainService>().To<MainService>();
ninjectKernel.Bind<IAdminService>().To<AdminService>();

Controller

public class AdminController : Controller
{
    private IMainService m_MainService;
    private IAdminService m_AdminService;

    public AdminController(IMainService mainService, IAdminService adminService)
    {
        m_MainService = mainService;
        m_AdminService = adminService;
    }
    ...
}

Service

public class MainService : IMainService
{
    private IMainRepository m_Repository;

    public MainService(IMainRepository repository)
    {
        m_Repository = repository;
    }
    ...
}

Repository

public class AdminRepository : IAdminRepository 
{
    private EntityFrameworkDbContext m_Context;

    public AdminRepository(IUnitOfWork unitOfWork)
    {
        if (unitOfWork == null)
            throw new ArgumentNullException("unitOfWork");

        m_Context = unitOfWork as EntityFrameworkDbContext;
    }
    ...
}

As you can see I implement the Unit of Work pattern but I don’t know if this is a good approach.

Thanks.

  • 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-27T06:25:27+00:00Added an answer on May 27, 2026 at 6:25 am

    Used a similar approach with MVC3 and Linq2SQL, so I guess this should work for Entity Framework too. Just make sure that you tell the fact to other developers, that by default all queries in the same request use the same EF DbContext if not told otherwise.

    One remark: You are effectively casting your IUnitOfWork to a EntityFrameworkDbContext type in your Repository, and I guess you couldn’t just replace it with another implementation. Is it a good idea to publish to the callers, that you expect IUnitOfWork (that has 1 method), but under the hood your code works only, if you pass an EntityFrameworkDbContext? And I would guess code would fail, if the parameter is not an actual EntityFrameworkDbContext?

    If you use that actual EF datacontext, you can pass in an actual EntityFrameworkDbContext instance to all callers using Ninject like this:

    //this makes sure all contexts use the same DbContext
    ninjectKernel.Bind<EntityFrameworkDbContext>().To<EntityFrameworkDbContext>().InRequestScope();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have business layer and UI layer in separate projects within a same solution.
I have a ASP.NET MVC3 solution with 2 projects it in. Let's call it
I have two projects in my solution... a domain project and MVC3 web project
I have MVC3 project & Valums file uploader . The code is following <script
I have an MVC3 app with a WCF service reference. Of course when im
I have .net mvc3 web site. And i must to implement top menu. Each
How to register Controller in MVC3? I have 2 solutions: MvcApplication (MVC 3 Web
I've been fiddling with some ASP.NET MVC3 solution structures and I have settled on
Here goes: I have 2 MVC3 projects in the solution, where one is for
Using: MVC3, Vs2010, and a DbProject In a Solution I have an MVC3 project

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.