ASP.Net MVC2中的Dependencey Injection [英] Dependencey Injection in ASP.Net MVC2

查看:85
本文介绍了ASP.Net MVC2中的Dependencey Injection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我正在关注Steven Sanderson的Pro MVC2书,并对使用Ninject有疑问。

以下是代码

< pre lang =c#> public class NinjectControllerFactory:DefaultControllerFactory

{
// Ninjectkernet是可以提供对象实例的东西
private IKernel kernel = new StandardKernel( new SportsStoreServices());


protected 覆盖 IController GetControllerInstance(System.Web.Routing .RequestContext requestContext,Type controllerType)
{
return (IController)kernel.Get(controllerType);
}

私人 SportsStoreServices:NinjectModule
{
public string QString = null ;
public 覆盖 void 加载()
{

Bind< IProductsRepository>()。To< SqlProductsRepository>()
.WithConstructorArgument( connectionString,ConfigurationManager.ConnectionStrings [ AppDb]的ConnectionString)。


}
}

}





我想要制作的地方



  private   class  SportsStoreServices:NinjectModule 
{

}



但它显示错误,我可以没有私人,公共课程

所以我应该做什么请告知



感谢All

解决方案

保留语法和理解编译错误消息。使用简单的逻辑。您是否能理解,与私人类型的成员不同,私人顶级课程是完全荒谬的?



如果某个类型的某个成员是私有的,则它是私有的这种背景。这意味着如果声明类,除了代码之外的任何地方都无法访问它。什么是私人顶级课程的背景? 这意味着无法从任何地方访问课程。



我该怎么做的问题也几乎没有任何意义。这取决于你想要达到的目标。你应该做任何你想做的事情,但不要违反任何事情,特别是常识。



-SA


Hi All
I am following Steven Sanderson's Pro MVC2 book and have a question about using Ninject.
Following is the Code

public class NinjectControllerFactory : DefaultControllerFactory

{
    //A Ninject "kernet" is the thing that can supply object instances
    private IKernel kernel = new StandardKernel(new SportsStoreServices());


    protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
    {
        return (IController)kernel.Get(controllerType);
    }

    private class SportsStoreServices : NinjectModule
    {
        public string QString = null;
        public override void Load()
        {

            Bind<IProductsRepository>().To<SqlProductsRepository>()
                .WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["AppDb"].ConnectionString);


        }
    }

}



Where I am trying to make

private class SportsStoreServices : NinjectModule
{

}


But it is showing error that I can't make Private, Public Class explecitely
So what should I do Please Advise

Thanks to All

解决方案

Set aside syntax and understanding of compilation error message. Use just simple logic. Can you understand that, unlike a private type member, a private top-level class is total absurd?

If some member of a type is private, it is private in the context of this type. It means that it cannot be accessed from anywhere except the code if the declaring class. And what would be a context of a private top-level class? It would mean that the class is not accessible from anywhere.

The question "what should I do" also makes little to no sense. It depends on what you want achieve. You should do whatever you want, but not violating anything, especially common sense.

—SA


这篇关于ASP.Net MVC2中的Dependencey Injection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆