Ninject和静态类-如何? [英] Ninject and static classes - how to?

查看:56
本文介绍了Ninject和静态类-如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态类,我需要向其中注入一些实例.静态类可以具有静态构造函数,但它必须是无参数的.那么,我应该如何向其中注入一些东西?

我不想创建一个单例.我希望有一个静态类,它的一种方法在应注入的实例上运行.下面是一个我需要的东西的例子:

public static class AuthenticationHelper
{
    // Fields.
    private static object _lock = new object();
    private static readonly UserBusiness _userBusiness; // <-- this field needs to be injected.

    // Public properties.
    public static User CurrentUser
    {
        get
        {
            if (IsAuthenticated)
            {
                User user = (User)Context.Session[SessionKeys.CURRENT_USER];

                if (user == null)
                {
                    lock (_lock)
                    {
                        if (user == null)
                        {
                            user = _userBusiness.Find(CurrentUserId);
                            Context.Session[SessionKeys.CURRENT_USER] = user;
                        }
                    }
                }

                return user;
            }

            return null;
        }
    }
    public static int CurrentUserId { get; /* implementation omitted for brevity */ }
    public static bool IsAuthenticated { get; /* implementation omitted for brevity */ }
}

背景信息:这是一个MVC4应用程序,所以我正在使用ninject.mvc3插件.

PS .:我已经看到一些有关Ninject和静态方法的问题,但似乎都没有解决这样的问题.

解决方案

不要这样做.不要使用需要自己依赖的静态类.这使得测试更加困难,其他依赖于此AuthenticationHelper的类型将无法将其包含在其构造函数中,这意味着它们隐藏了它们依赖于它的事实.

相反,您只需做您一直会做的事情:使AuthenticationHelper为非静态,在其上实现一个IAuthenticationHelper接口,并通过其公共构造函数注入所有依赖项.

但是,如果您坚持要使该类保持静态(这又是一个非常糟糕的主意),请在其上创建一个静态Initialize(UserBusiness userBusiness)方法,然后在应用程序的启动路径中调用此方法.您不能让您的DI容器调用此静态方法.他们不允许这样做,因为1.这是个坏主意,而且2.这种静态方法只需要调用一次,因此让您的容器为您自动接线实际上并没有帮助.

I have a static class and I need to inject some instances into it. A static class can have a static constructor but it must be parameterless. So, how am I supposed to inject something into it?

I do not wish to create a singleton. I wish to have a static class, and one of its methods operate on an instance that should be injected. Below is an example of the sort of thing I need:

public static class AuthenticationHelper
{
    // Fields.
    private static object _lock = new object();
    private static readonly UserBusiness _userBusiness; // <-- this field needs to be injected.

    // Public properties.
    public static User CurrentUser
    {
        get
        {
            if (IsAuthenticated)
            {
                User user = (User)Context.Session[SessionKeys.CURRENT_USER];

                if (user == null)
                {
                    lock (_lock)
                    {
                        if (user == null)
                        {
                            user = _userBusiness.Find(CurrentUserId);
                            Context.Session[SessionKeys.CURRENT_USER] = user;
                        }
                    }
                }

                return user;
            }

            return null;
        }
    }
    public static int CurrentUserId { get; /* implementation omitted for brevity */ }
    public static bool IsAuthenticated { get; /* implementation omitted for brevity */ }
}

Background info: this is an MVC4 application, so I'm using ninject.mvc3 plugin.

PS.: I've seen some questions concerning Ninject and static methods, but none of them seemed to address an issue like this.

解决方案

Don't do it. Don't use a static class that needs dependencies of its own. This makes testing harder and other types that depend on this AuthenticationHelper won't be able to include it in their constructor which means they hide the fact that they depend on it.

Instead just do what you would always do: make AuthenticationHelper non-static, implement an IAuthenticationHelper interface on it and inject all dependencies through its public constructor.

But if you insist into keeping that class static (which again is a really bad idea), create a static Initialize(UserBusiness userBusiness) method on it, and call this method in the start-up path of your application. You can't let your DI container call this static method. They don't allow because 1. it's a bad idea, and 2. such static method only has to be called once, so letting your container auto-wire this for you doesn't really help.

这篇关于Ninject和静态类-如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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