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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:02:19+00:00 2026-06-04T10:02:19+00:00

I have a MVC3 project that uses the Entity Framework and Ninject v2.2, and

  • 0

I have a MVC3 project that uses the Entity Framework and Ninject v2.2, and follows the Unit of Work pattern with a Service Layer wrapping my repositories.

After looking at the code below, hopefully its apparent that Ninject is using constructor chaining to inject the correct classes. It currently works prefectly in my application, however I am at the point that I need to bind an instance of IDatabase to MyDatabase with a different scope such as InSingletonScope() or InNamedScope(), not InRequestScope(). I know that I can use the [Named(“MyDatabaseScope”)] Attribute to customize which IDatabase object is injected, however it seems that with my code structure, if I wanted to inject my SingletonScoped instance, I would have to recreate a new Abstract and Concrete Implementation of my Unit of Work, my Service and all my Repositories, that will then chain down.

Basically my application currently goes

Controller -> Unit of Work -> Database, (Repositories -> Database)

If I have to change my Database Binding, I will now have to create another chain in addition to the current one:

Controller -> New Unit of Work -> SingletonDatabase, (New Repositories-> SingletonDatabase)

This seems to completely defeat the DRY principal. Is there a way to, from the Controller Constructor, inform Ninject that when doing constructor chaining it should use my singleton (or named binding) rather than my request scope binding, without having to recreate all my classes with a Named attribute, or a new Interface?

Sorry for the long text, I wasnt sure if I could get the point across without my code snippets and my somewhat rambling explaination.

Ninject Module Load Function:

..snip..
Bind<IUserServices>().To<UserServices>();
Bind<IBaseServices>().To<BaseServices>();
Bind<IUserRepository>().To<UserRepository>();
Bind(typeof (IRepository<>)).To(typeof (RepositoryBase<>));
Bind<IUnitOfWork>().To<UnitOfWork>();
Bind<IDatabase>().To<MyDatabase>().InRequestScope();

//This is my problem:
//Bind<IDatabase>().To<MySingletonDatabase>().InSingletonScope();

Unit of Work Implementation Constructor:

public class UnitOfWork : IUnitOfWork
{
    private IDatabase _database;

    public UnitOfWork(IDatabase database,
                      IUserRepository userRepository,
                      IPeopleRepository peopleRepository,
        )
    {
        this._database = database;
        this.UserRepository = userRepository;
        this.PeopleRepository = peopleRepository;
    }

    protected IDatabase Database
    {
        get { return _database; }
    }

    ...snip...
}

User Service Layer Implementation Constructor:

public class UserServices : BaseServices, IUserServices
{
    private IUnitOfWork _uow;

    public UserServices(IUnitOfWork uow)
        : base(uow)
    {
        _uow = uow;
    }
    ...snip...
}

User Repository Constructor:

public class UserRepository : RepositoryBase<User>, IUserRepository
{
    public UserRepository(IDatabase database)
        : base(database)
    {
    }
    ...snip...
}

Controller Constructor:

public IUserServices _userServices { get; set; }

    public ActivityController(IUserServices userServices)
    {
        _userServices = userServices;
    }
}
  • 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-04T10:02:21+00:00Added an answer on June 4, 2026 at 10:02 am

    Using Ninject 3.0.0 you can use WhenAnyAncestrorNamed("Some name") But if you need to run asyncronous things you should thing about splitting your application into a web frontend and a server backend. This could make many things easier.

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

Sidebar

Related Questions

I have a MVC 3 project that uses Entity Framework. I can successfully update
I have an ASP.NET MVC3 project that uses a tab strip on certain pages.
I have an ASP.NET MVC 3 (using Entity Framework 4.2) application that uses transactions
I have a project that uses MVC 3 and Entity Franework. The site works
I have an ASP.Net MVC3 project that calls a WCF service to get data.
I have an ASP.NET MVC 3 application that uses the Ninject.MVC3 extension to setup
I have an MVC3 project using the Entity Framework model in which I've marked
I have some C# code in my ASP.NET MVC3 project that's throwing an exception
For a current MVC3 project I have a model that has multiple pages for
I have an MVC3 project that is using an EF4 project as its Domain.

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.