Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
618 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - MVC3, Ninject and Ninject.MVC3 problem

I just start using Ninject with MVC3 so here is my problem: - I installed Ninject 2.2.1.4 and Ninject.MVC3 2.2.2.0 from Nuget - In my WebUI (MVC3 project):

Global.asax.cs

public class MvcApplication : NinjectHttpApplication
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "home", action = "index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

    protected override void OnApplicationStarted()
    {
        base.OnApplicationStarted();

        AreaRegistration.RegisterAllAreas();
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

    protected override IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Load(Assembly.GetExecutingAssembly());
        return kernel;
    }


}
  • In my Domain (class project), i have my LinQ to SQL datacontext, i want to load the context with the connection string from my Web.Config in my WebUI, so i have to pass the constructor parameter, i also have some services in my Domain project

    public class LotteryDataService
    {
        LinQ.WebDataContext _context;
    
        public LotteryDataService(LinQ.WebDataContext context)
        {
            _context = context;
        }
    
        public IEnumerable<LinQ.LotteryData> Get()
        {
            return _context.LotteryDatas.Take(10);
        }
    }
    

How to bind the datacontext with Ninject with constructor parameter (here is connection string)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is how you pass a constructor parameter. Ninject will resolve the constructor that matches the specified constructor arguments.

public class DataModule : NinjectModule
{
    public override void Load()
    {
        string connectionString = "...";
        Bind<WebDataContext>().ToSelf()
            .WithConstructorArgument("connection", connectionString);
    }
}

The first argument to .WithConstructorArgument() should be the name of the constructor parameter. This is fileOrServerOrConnection in the base class, but connection in derived class.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...