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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:00:42+00:00 2026-06-10T21:00:42+00:00

I am using Ninject in my MVC application and am trying to implement a

  • 0

I am using Ninject in my MVC application and am trying to implement a Unit of Work repository pattern.

Specifically, I have defined a generic IUnitOfWork interface, I then want to use an instance of this interface in the constructor for some other (Business Logic classes). Here is the start of my Controller…

public class CompanyController : Controller
{
    private readonly IUnitOfWork UnitOfWork;
    private readonly ICompanyService CompanyDb;
    private readonly IEmployeeService EmployeeDb;

    public CompanyController(IUnitOfWork unitOfWork)
        : base()
    {
        this.UnitOfWork = unitOfWork;
        // This following construction is the problem...
        this.CompanyDb = new ICompanyService(this.UnitOfWork);
        this.EmpoloyeeDb = new IEmployeeService(this.UnitOfWork);
    }

    // Rest of Controller implementation
}

The Controller constructor works correctly, but I would like to find a clean way of constructing the various Business Logic classes so that they may reference the repositories within the UnitOfWork object.

The problem is that I can’t instantiate a new Business Logic object from the interface, i.e. the following code fails:

this.CompanyDb = new ICompanyService(this.UnitOfWork);

As there is nothing to ‘wire’ the ICompanyService interface to a concrete implementation. What I would like it to do would be to use DI (Ninject) to create the instance of ICompanyService where the physical class’s constructor would look like this:

public class CompanyService : ICompanyService {

    private readonly UnitOfWork db;

    public CompanyService(IUnitOfWork unitOfWork) {
        this.db = unitOfWork as UnitOfWork;
    }
}

FINAL SOLUTION
Further to the suggestions from Joshscorp and Dbaseman (thank you) I structured my code as follows:

public class CompanyService : ICompanyService {

    private readonly UnitOfWork db;

    public CompanyService(IUnitOfWork unitOfWork) {
        this.db = unitOfWork as UnitOfWork;
    }

    // Rest of implementation
}


public class CodeFirstController : Controller {

    private readonly IUnitOfWork UnitOfWork;
    private readonly ICompanyService CompanyDb;
    private readonly IEmployeeService EmployeeDb;


    public CodeFirstController(
        IUnitOfWork unitOfWork,
        ICompanyService companyDb,
        IEmployeeService employeeDb
    ) : base() {
        this.UnitOfWork = unitOfWork;
        this.CompanyDb = companyDb;
        this.EmployeeDb = employeeDb;
    }

    // Rest of implementation
}

The Ninject module is as follows:

public class CodeFirstModule : NinjectModule {
    public override void Load() {
        string connectionString = ConfigurationManager
            .ConnectionStrings["CompanyConnection"]
            .ConnectionString;


        Bind<CodeFirst.DAL.IUnitOfWork>()
           .To<CodeFirst.DAL.EntityFramework.UnitOfWork>()
           .InRequestScope()
           .WithConstructorArgument("connectionString", connectionString);

        Bind<CodeFirst.DAL.ICompanyService>()
           .To<CodeFirst.DAL.EntityFramework.CompanyService>()
           .InRequestScope();

        Bind<CodeFirst.DAL.IEmployeeService>()
           .To<CodeFirst.DAL.EntityFramework.EmployeeService>()
           .InRequestScope();
   }
}

The ‘InRequestScope()’ method on the IUnitOfWork binding ensures that the same instance of the UnitOfWork class is used in the constructors of the dependent classes CompanyService and EmployeeService.

This achieves exactly what I was looking for – a single instance of the IUnitOfWork object referenced by the dependent services classes.

  • 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-10T21:00:44+00:00Added an answer on June 10, 2026 at 9:00 pm

    First of all, you cannot new up an Interface, only concrete classes.

    new ICompanyService will never work.

    Secondly, if you are usually MVC, I supposed you are using the DependencyResolver to resolve your objects, which is an anti-pattern according to some. Regardless of what you use to resolve the dependencies, you should always have a single point of entry to the application, i.e. per request in Global.asax etc… and a bootstrapper of some sort to load up the dependency object graphs.

    In your example, what I would change is the constructor, taking in ICompanyService and IEmployeeService as well,

    public class CompanyController : Controller
    {
        private readonly IUnitOfWork unitOfWork;
        private readonly ICompanyService companyDb;
        private readonly IEmployeeService rmployeeDb;
    
        public CompanyController(IUnitOfWork unitOfWork,
            ICompanyService companyDb, 
            IEmployeeService  employeeDb)
        {
            this.unitOfWork = unitOfWork;
            this.companyDb = companyDb;
            this.empoloyeeDb = employeeDb;
       }
    
        // Rest of Controller implementation
    }
    

    This is of cause assuming you registered the concrete implementations of ICompanyService and IEmployeeService with Ninject or any IoC container in the entry point of your app (not familiar with Ninject and don’t recommend it)

    You might wonder, how then did the IUnitOfWork instance get injected into the ICompanyService concrete object’s constructor? The same way it got injected to the CompanyController‘s constructor!

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

Sidebar

Related Questions

I have an ASP.NET MVC 4 Application that I want to implement Unit of
I have successfully setup a simple mvc application that lists teams. I'm using Ninject
I have an ASP.NET MVC 3 application and am using Ninject for injecting dependencies
I have an MVC application using Ninject to connect to a single database. Now
I am using Ninject in my ASP MVC 3 project, I have modified the
I was trying Ninject in a Asp.net Mvc application and I was wondering what
I am using ASP.NET MVC 3 + Ninject in my application which works great
Hi I'm using ninject with an MVC app. I'm sure I have it setup
I am starting a project in ASP.NET MVC using Ninject to implement dependency injection.
I am trying to use the repository pattern, ninject for DI with fluent nhibernate.

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.