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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:47:43+00:00 2026-06-14T04:47:43+00:00

I am working on asp.net mvc 4. I am trying to implement dependeny injection

  • 0

I am working on asp.net mvc 4. I am trying to implement dependeny injection using ninject and repository pattern with code first model.

IMessageRepository

public interface IMessageRepository
{
void IsMessageRead();
}

MessageRepository

 public class MessageRepository:IMessageRepository
 {
 string id="";
 public MessageRepository(string idparam)
 {
   id=idparam;
 }
 EFMsgContext context=new EFMsgContext(id);
 public void IsMessageRead()
 {
  ...
 }
 }

EFMsgContext

public class EFMsgContext:Dbcontext
{
public EFMsgContext(string id):base("MyContext")
{
 //based on id i will construct a connection string from my database.
}
....
}

my controller

public class MYController:Controller
{
private IMessageRepository repo;
public MYController(IMessageRepository repoparam)
{
repo=repoparam;
}
public ActionResult Index()
{
  repo.IsMessageRead();
}
}

and i am using Ninject for dependency injection i bind my interface with concrete class like,

ninjectKernel.Bind<IMessageRepository>().To<MessageRepository>();

here how could i pass constructor parameters to the binding and is there any way to write the logic for buidling connection string inside the ninjectcontrollerfactory. please guide me.

  • 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-14T04:47:45+00:00Added an answer on June 14, 2026 at 4:47 am

    You have to use the WithConstructorArgument extension method

    ninjectKernel.Bind<IMessageRepository>().To<MessageRepository>().WithConstructorArgument("idparam", value);
    

    You can even pass a Func as a second argument.

    ninjectKernel.Bind<IMessageRepository>().To<MessageRepository>()
                 .WithConstructorArgument("idparam", (c) => {
                       // read from somewhere and return
                  });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on asp.net mvc. I am trying to implement Code-First Entity Model.
I'm working in ASP.NET MVC 1.0 using MvcContrib FluentHtml version 1.0. The following code
I'm trying to understand the Repository Pattern , while developing an ASP.NET MVC application
I am trying to implement optional client side validation using ASP.NET MVC 4, unobtrusive
I'm working on my first ASP.NET MVC 3 application and I'm trying to show
Currently I'm trying to understand dependency injection better and I'm using asp.net MVC to
I am working with asp.net mvc 4 and entity framework code-first approach. I have
I am trying to get ASP.Net MVC 4 working on IIS6 and am running
I'm trying to get edit-and-continue working with Visual Studio 2008 with an ASP.Net MVC
i've started working with ASP.NET MVC and using Entity Framework, concretely with SQLite db,

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.