c# - StructureMap 在请求实例时向构造函数提供字符串参数

标签 c# dependency-injection inversion-of-control instantiation structuremap

在 Autofac 中,您好我能够做到这一点:

public class Service1_Client
{
    private const string ServiceName = "Service1";
    Func<string, RabitMqClient> _clientFunc;

    public Service1_Client(Func<string, RabitMqClient> clientFunc) 
    { 
         _clientFunc = clientFunc;
    }

    public IList<object> GetData()
    {
        return _clientFunc(this.ServiceName).Get<IList<object>>();               
    }
}

public class Service2_Client
{
    private const string ServiceName = "Service2";
    Func<string, RabitMqClient> _clientFunc;

    public Service2_Client(Func<string, RabitMqClient> clientFunc) 
    { 
         _clientFunc = clientFunc;
    }

    public IList<object> GetData()
    {
        return _clientFunc(this.ServiceName).Get<IList<object>>();               
    }
}

public class RabitMqClient
{
    private IConnectionFactory _connectionFactory;
    private string _baseServiceName;
    public RabitMqClient(IConnectionFactory connectionFactory, string baseServiceName)
    {
        _connectionFactory = connectionFactory;
        _baseServiceName = baseServiceName;
    }

    public TResult Get<TResult>()
    {
        string topicName = this.GetTopicName();
        ///Connect to the channel that uses the topic and ask for data...
    }

    private string GetTopicNAme()
    {
        /// this should be an algoritam for getting the topic from the speccified baseServiceName (I have omitted the details)
        return this._baseServiceName;
    }     
}

现在解释: 基本上我有一个依赖于 2 个参数的类 (RabitMqClient):

  1. 连接工厂(IConnectionFactory)
  2. 基础服务名称(字符串)

这些参数用于连接到 RabbitMq 服务器。 现在我在RabbitMq中使用Topics来实现服务的多样化,为了得到正确的Topic,使用了字符串参数baseServiceName。 预计调用方(Service1_Client、Service2_Client)提供字符串参数 baseServiceName。

在 Autofac 中我可以像这样注册这个类:

builder.Register((context, parameters) =>
{
    var param = parameters.First() as Autofac.TypedParameter;
    return new HcRabbitMqClient(
        context.Resolve<IConnectionFactory>(),param.Value.ToString());
}).AsSelf().InstancePerDependency();

这告诉 Autofac,当请求这个类时,让 Autofac 解析第一个参数,但调用者将在调用站点提供第二个(字符串)参数。然后,此字符串参数将放入参数值中。 this的用法见上面代码中的(Service1_Client,Service2_Client)GetData()方法;

如何在 StructureMap 中执行此操作?

最佳答案

如何扩展工厂模式?

public interface IRabitMqClientFactory
{
    IRabitMqClientFactory Create(string baseServiceName);
}

public class RabitMqClientFactory : IRabitMqClientFactory
{
    public RabitMqClientFactory(IConnectionFactory connectionFactory){
        _connectionFactory = connectionFactory;
    }

    public IRabitMqClientFactory Create(string baseServiceName)
    {
        _baseServiceName = baseServiceName;
    }
}

然后就简单了

public class Service1_Client
{
    private const string ServiceName = "Service1";
    private RabbitMqClient _rabbitMqClient;

    public Service1_Client(IRabitMqClientFactory rabitMqClientFactory) 
    { 
         _rabbitMqClient = rabitMqClientFactory.Create(ServiceName);
    }

    public IList<object> GetData()
    {
        return _rabbitMqClient.Get<IList<object>>();               
    }
}

它也比在 IoC 中包含复杂逻辑更具可读性。

关于c# - StructureMap 在请求实例时向构造函数提供字符串参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40163035/

相关文章:

c# - ref 或 out 参数必须是可赋值变量?

c# - .NET 异常处理有多繁重?

java - 当一个类只调用另一个类有很多方法的一个方法时,如何降低耦合?

java - 如何从 HttpServlet 访问同一个 bean 实例?

testing - 小巧玲珑。 IoC、测试和 Agatha

c# - 获取 checkedCheckList (Form1) 的 GetItemCheckState (在 Form2)

c# - 为 Windows 窗体 C# 设置日期时间选择器的最小日期

java - 将多个接口(interface)实现注入(inject)到类构造函数中

c# - 没有实现 UoW 或存储库的 EF 是否正确?

Delphi依赖注入(inject): Framework vs Delegating Constructor