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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:25:03+00:00 2026-05-12T08:25:03+00:00

I have started using Ninject 2 (downloaded from Github yesterday including the MVC extension

  • 0

I have started using Ninject 2 (downloaded from Github yesterday including the MVC extension project) with a project based on the following technologies:

  • .Net 3.5 Sp1
  • ASP.NET MVC 1.0
  • LINQ to SQL

Nothing magical here – I have a few repository interfaces (named like IEntityRepository) that are implemented using LINQ to SQL in the runtime code (and using a hashtable in the unit test code). Each of these repositories need an instance of the DataContext from LINQ to SQL in order to talk to the database, so that is a constructor parameter on the concrete repository classes. The binding is set up like this:

Kernel.Bind<MyDataContext>().ToSelf().InRequestScope();

The reason for this is that I want to be able to share entities between different repositories if I should happen to need more of them, and with the LINQ to SQL datacontext unit of work philosophy, it seems to make sense to me to create one per HttpRequest.

I usually use the parameterless constructor for MyDataContext – I don’t see that as a risk because this is used for an internal project on a test system, so the “built-in” connection string in the datacontext is harmless. However, since Ninject 2 is “greedy” and wants the constructor with the MOST parameters, and I can’t really stick the [Inject] parameter into generated code in any meaningful way, I get an error whenever Ninject tries to create one of my controllers (which needs a repository, which needs the datacontext).

I have seen mention of IConstructorScorer and the ability to make an “inverted” one that would always use the constructor with the LEAST parameters, but then again, this would change how injection works for everything else – the default behavior is probably what I want for everything but the datacontext.

So – is there a nice, clean way to specify that this binding (and only this binding) should use a specific constructor? Can we do the same thing with providers as in Ninject 1, and perhaps supply our own “factory”? Or should I just give in and try to feed parameters into the datacontext that make sense?

  • 1 1 Answer
  • 1 View
  • 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-12T08:25:03+00:00Added an answer on May 12, 2026 at 8:25 am

    Figured it out – it is quite easily done by binding to a provider;

    Kernel.Bind<MyDataContext>().ToProvider<ContextProvider>().InRequestScope();
    

    Ninject will now call my ContextProvider whenever it needs to build one of those pesky DataContext objects. This is what my provider class looks like:

    public class ContextProvider : IProvider
    {
        #region IProvider Members
    
        public object Create(IContext context)
        {
            return new MyDataContext();
        }
    
        public Type Type
        {
            get { throw new NotImplementedException(); }
        }
    
        #endregion
    }
    

    Seems like I got away with it – it works fine. 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 226k
  • Answers 226k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Fast is not a general quality, the performance must be… May 13, 2026 at 1:12 am
  • Editorial Team
    Editorial Team added an answer Failing to return a value from a function that has… May 13, 2026 at 1:12 am
  • Editorial Team
    Editorial Team added an answer You need to redefine #eql? and the hash method. You… May 13, 2026 at 1:12 am

Related Questions

I just started working with dependency injection for the first time and I am
I'm new to IOC containers, and I'm getting started with NInject. What do you
I have started using Jython as it seems to be a excellent language, and
I have started using Linq to SQL in a (bit DDD like) system which

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.