Ninject 从内核获取实例

发布于 2024-10-05 10:43:55 字数 2794 浏览 2 评论 0原文

我是 Ninject 的新手,也是 stackoverflow 的新手。

我将它与 ninject.web.mvc 扩展一起使用,我能够像这样正确初始化它:

public class MvcApplication : NinjectHttpApplication 
{
    protected override void OnApplicationStarted()
    {
        RegisterRoutes(RouteTable.Routes);
    }

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

这是我的类 assemlylocator,它扫描 bin 文件夹中的所有程序集,搜索程序集中的所有 Ninject 模块。

public static class AssemblyLocator
{ 
    private static readonly ReadOnlyCollection AllAssemblies = null; 
    private static readonly ReadOnlyCollection BinFolderAssemblies = null;

    static AssemblyLocator() 
    { 
        AllAssemblies = new ReadOnlyCollection<Assembly>( 
            BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToList()); 

        IList<Assembly> binFolderAssemblies = new List<Assembly>(); 

        string binFolder = HttpRuntime.AppDomainAppPath + "bin\\"; 
        IList<string> dllFiles = Directory.GetFiles(binFolder, "*.dll", 

        SearchOption.TopDirectoryOnly).ToList(); 

        foreach (string dllFile in dllFiles) 
        { 
            AssemblyName assemblyName = AssemblyName.GetAssemblyName(dllFile); 
            Assembly locatedAssembly = AllAssemblies.FirstOrDefault(a => 
            AssemblyName.ReferenceMatchesDefinition(a.GetName(), assemblyName)); 

            if (locatedAssembly != null) 
            { 
                binFolderAssemblies.Add(locatedAssembly); 
            } 
        }

        BinFolderAssemblies = new ReadOnlyCollection<Assembly> (binFolderAssemblies); 
    }

    public static ReadOnlyCollection<Assembly> GetAssemblies() 
    { 
        return AllAssemblies; 
    }

    public static ReadOnlyCollection<Assembly> GetBinFolderAssemblies() 
    { 
        return BinFolderAssemblies; 
    } 
} 

我的控制器中的一切都工作正常:

public class ReteController : Controller 
{ // // GET: /Rete/ 

    private readonly IReteService _service;

    public ReteController(IReteService _service)
    {
        if (_service == null)
        {
            throw new ArgumentNullException("IReteService");
        }
        this._service = _service;
    }

    public ActionResult Index()
    {
        return View(_service.getReti());
    }

在此之前,几乎所有内容都很容易学习,现在我的问题是,如果我需要创建在 NinjectModule From Ninject 中绑定的对象的新实例,我不知道如何访问内核从听。

//this is jus a ex.

public ActionResult NewRete() { 
    IRete xItem = Kernel.get();
    xItem.name= "hope";
    return View(xItem);
}

问题是我无法从我的控制器找到内核。我也需要将它注入到构造函数中?

我希望有人能帮助我。非常感谢你们这些天给我的大力帮助。

I am new to Ninject and I am new to stackoverflow too.

I am using it with the ninject.web.mvc extension, I was able to initialize it correctly like this:

public class MvcApplication : NinjectHttpApplication 
{
    protected override void OnApplicationStarted()
    {
        RegisterRoutes(RouteTable.Routes);
    }

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

And here is my class assemlylocator that scans all the assemblies in the bin folder, searching for all the Ninject modules in the assembly.

public static class AssemblyLocator
{ 
    private static readonly ReadOnlyCollection AllAssemblies = null; 
    private static readonly ReadOnlyCollection BinFolderAssemblies = null;

    static AssemblyLocator() 
    { 
        AllAssemblies = new ReadOnlyCollection<Assembly>( 
            BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToList()); 

        IList<Assembly> binFolderAssemblies = new List<Assembly>(); 

        string binFolder = HttpRuntime.AppDomainAppPath + "bin\\"; 
        IList<string> dllFiles = Directory.GetFiles(binFolder, "*.dll", 

        SearchOption.TopDirectoryOnly).ToList(); 

        foreach (string dllFile in dllFiles) 
        { 
            AssemblyName assemblyName = AssemblyName.GetAssemblyName(dllFile); 
            Assembly locatedAssembly = AllAssemblies.FirstOrDefault(a => 
            AssemblyName.ReferenceMatchesDefinition(a.GetName(), assemblyName)); 

            if (locatedAssembly != null) 
            { 
                binFolderAssemblies.Add(locatedAssembly); 
            } 
        }

        BinFolderAssemblies = new ReadOnlyCollection<Assembly> (binFolderAssemblies); 
    }

    public static ReadOnlyCollection<Assembly> GetAssemblies() 
    { 
        return AllAssemblies; 
    }

    public static ReadOnlyCollection<Assembly> GetBinFolderAssemblies() 
    { 
        return BinFolderAssemblies; 
    } 
} 

Everything works fine in my controller:

public class ReteController : Controller 
{ // // GET: /Rete/ 

    private readonly IReteService _service;

    public ReteController(IReteService _service)
    {
        if (_service == null)
        {
            throw new ArgumentNullException("IReteService");
        }
        this._service = _service;
    }

    public ActionResult Index()
    {
        return View(_service.getReti());
    }

Until here almost everything was easy to learn, now my problem is that if I need to create a new instance of an object that was bind in the NinjectModule From Ninject I don't know how to acces the kernel from heare.

//this is jus a ex.

public ActionResult NewRete() { 
    IRete xItem = Kernel.get();
    xItem.name= "hope";
    return View(xItem);
}

the problem is that i am not able to find the kernel from my controller. I need to inject it too in the constructor??

I hope somebody can help me. Thank a lot for the big help that you guys give me all the days.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断念 2024-10-12 10:43:55

请参阅如何我是否将 Ninject 与 ActionResults 一起使用,同时使控制器与 IoC 框架无关?

这对你来说基本上是一样的。创建一个工厂 a 并将其注入到您的控制器中。

See How do I use Ninject with ActionResults while making the controller IoC-framework-agnostic?

It's basically the same for you. Create a factory aand inject it to your controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文