如何将运行时字符串参数添加到依赖关系解析链中? [英] How do you add a runtime string parameter into a dependency resolution chain?

查看:81
本文介绍了如何将运行时字符串参数添加到依赖关系解析链中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为这种设置设置我选择的DI:

How could I setup my chosen DI for this kind of setup:

public abstract class BaseRepo
{
     public BaseRepo(string token)
     {
     }
}

public RepoA : BaseRepo, IRepoA
{
     // implementation of interface here    
}

public ViewModelA
{
     IRepoA _repo;
     public ViewModelA(IRepoA repo)
     {
       this._repo = repo;
     }

     public DoMethod()
     {
        this._repo.DoSomeStuff();
     }
}

在实际情况下,用户登录后就可以解析基类上的token参数.我本来只是想在登录后为DI配置接口,但是我不确定这样做是否正确.

In real scenario, the token parameter on the base class is resolved after the user has been logged in. I was thinking of just configuring the interfaces for DI after the login but I'm not sure if that a right thing do.

我看了一些工厂,但是我无法使其正常工作.

I looked at some Factories but I can't make it to work.

我选择的DI可能是AutoFac/Ninject,而该项目是Xamarin移动应用程序

My choice of DI probably goes to AutoFac/Ninject and the project is Xamarin mobile app

推荐答案

在实际情况下,解析了基类上的token参数 用户登录后.

In real scenario, the token parameter on the base class is resolved after the user has been logged in.

这表示令牌参数是运行时数据.防止将运行时数据注入到组件中.您的组件应该是无状态的.相反,应通过先前构造的组件对象图通过方法调用传递运行时数据.否则,将使配置和验证对象图变得更加复杂.

This means that the token parameter is runtime data. Prevent injecting runtime data into your components. Your components should be stateless. Instead, runtime data should be passed on through method calls through the previously constructed object graph of components. Failing to do so, will make it much more complicated to configure and verify your object graphs.

通常存在传递运行时数据的方法.您可以通过对象图通过方法之间的方法调用将其传递,或者您的组件调用返回正确值的方法.该令牌似乎是上下文信息,通常意味着您选择了后者:

There are typically to ways of passing runtime data. Either you pass it on through method calls from method to method through the object graph, or your components call a method that returns that correct value. This token seems like it is contextual information and that would typically mean you choose the latter option:

public interface ITokenProvider {
    string GetCurrentToken();
}

// Don't use base classes: base classes are a design smell!
public RepoA : IRepoA
{
     private readonly ITokenProvider tokenProvider;
     public RepoA(ITokenProvider tokenProvider) {
        this.tokenProvider = tokenProvider;
     }

     // IRepoA methods
     public A GetById(Guid id) {
         // Get token at runtime
         string token = this.tokenProvider.GetCurrentToken();

         // Use token here.
     }
}

在您的组成根中,您将必须为ITokenProvider.此实现的外观高度依赖于您希望如何存储此令牌,但这是一个可能的实现:

In your Composition Root, you will have to create an implementation for this ITokenProvider. How this implementation looks is highly dependent on how you wish to store this token, but here's a possible implementation:

public sealed class AspNetSessionTokenProvider : ITokenProvider {
    public string GetCurrentToken() {
        return (string)HttpContext.Current.Session["token"];
    }
}

这篇关于如何将运行时字符串参数添加到依赖关系解析链中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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