Ninject-使用运行时值设置依赖项构造函数 [英] Ninject - Setting dependencies constructor with run-time value

查看:245
本文介绍了Ninject-使用运行时值设置依赖项构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对DI / Ninject相当陌生,似乎无法弄清楚我需要做什么。我有这样的服务:

 公共类ArchiveService:IArchiveService 
{
私有字符串sAuditUser =用户;

public ArchiveService(字符串AuditUser)
{
sAuditUser = AuditUser;
}
...
}

我目前实例化服务,如下所示:

  string sUser = HttpContext.Session [ UID]。ToString(); 
ArchiveService svcArchive =新的ArchiveService(Session [ UID]。ToString());

当用户成功登录帐户页面时,将设置会话值。我该如何设置服务依赖关系,以便它将正确获得该值?

解决方案

通常,您应该尝试防止尝试注入原始值插入服务,除非:




  • 它们是配置值,-和-

  • 只需将它们注入单个服务中。



如果其中任何一个为假,则不应注入直接原始。



在您的情况下,您正在处理上下文信息,应该对其进行抽象:

 公共接口IUserContext 
{
string Name {get; }
}

有了这种抽象,所有需要了解以下内容的服务代表操作执行的当前用户可以依赖于此抽象。

 公共类ArchiveService :IArchiveService 
{
private IUserContext userContext;

public ArchiveService(IUserContext userContext)
{
this.userContext = userContext;
}
}

现在唯一要做的就是创建您注册的 IUserContext 实现。这种实现的外观完全取决于您正在运行的应用程序的类型(ASP.NET,Win Forms,WCF等),但是在您的特定情况下,这将是如何实现的:

 公共类AspNetUserContext:IUserContext 
{
公共字符串名称
{
get
{
返回HttpContext.Current.Session [ UID]。ToString();
}
}
}

这就是您的注册方式

  Bind< IUserContext>()。To< AspNetUserContext>()。InSingletonScope(); 

为单个字符串创建接口似乎很愚蠢,但具有一些明显的优点: / p>


  • 新的抽象更清楚地传达了该值是什么。 字符串可以是任何东西。可能是路径,连接字符串等。

  • 抽象概念集中了如何将用户信息集中到一个地方的知识。

  • 由于新的抽象是明确的,因此在容器中注册它变得容易得多。该配置将更易于维护。

  • 该解决方案更不会出错,因为我们集中了提供用户信息的方式。


I'm fairly new to DI/Ninject and I can't seem to figure out what I need to do. I have a service like this:

public class ArchiveService : IArchiveService
{
    private string sAuditUser = "USER";

    public ArchiveService(string AuditUser)
    {
        sAuditUser = AuditUser;
    }
    ...
}

I'm currently instantiated the service like this:

string sUser = HttpContext.Session["UID"].ToString();
ArchiveService svcArchive = new ArchiveService(Session["UID"].ToString());

The session value gets set when a user successfully logs in on the account page. How can I setup the service dependency so it will get that value properly?

解决方案

In general you should try to prevent trying to inject primitive values into services unless:

  • They are configuration values, -and-
  • They only have to be injected into a single service.

If either of those are false, you should not inject the primitive directly.

In your case you're dealing with context information and that deserves its own abstraction:

public interface IUserContext
{
    string Name { get; }
}

With this abstraction in place, all services that need to know anything about the current user on who's behalf the operation is executing, can depend on this abstraction.

public class ArchiveService : IArchiveService
{
    private IUserContext userContext;

    public ArchiveService(IUserContext userContext)
    {
        this.userContext = userContext;
    }
}

The only thing you have to do now is to create an IUserContext implementation that you register. How this implementation looks like totally depends on the type of application you are running (ASP.NET, Win Forms, WCF, etc), but in your particular case, this is how it would be implemented:

public class AspNetUserContext : IUserContext
{
    public string Name
    { 
       get
       {
           return HttpContext.Current.Session["UID"].ToString();
       }
    }
}

And this is how you register it:

Bind<IUserContext>().To<AspNetUserContext>().InSingletonScope();

It might seem silly to create an interface for a single string, but it has some clear advantages:

  • The new abstraction communicates much clearer what that value is. A string could be anything. It could be a path, a connection string, etc.
  • The abstraction centralized the knowledge of how to get this user information to a single place.
  • Since the new abstraction is unambiguous, it becomes much easier to register this in the container. The configuration will be much more maintainable.
  • The solution is much less error prone, because we centralized the way user information is provided.

这篇关于Ninject-使用运行时值设置依赖项构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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