【问题标题】:How use Ioc structuremap for quartz?石英如何使用 Ioc 结构图?
【发布时间】:2016-06-28 07:46:25
【问题描述】:

我已尝试使用当前示例,但出现错误: https://gist.github.com/mhenrixon/772643

ForSingletonOf<IScheduler>().Use(ctx =>
        {
            var scheduler = ctx.GetInstance<ISchedulerFactory>().GetScheduler();
            scheduler.JobFactory = ctx.GetInstance<IJobFactory>();
            return scheduler;
        });

错误:“带有语句体的 lambda 表达式无法转换为表达式树”。

实际上我需要将 IoC 用于 IJobFactory

的解决方案

【问题讨论】:

    标签: c# inversion-of-control structuremap quartz.net ijobfactory


    【解决方案1】:

    我使用这样的东西:

            ForSingletonOf<IScheduler>().Use("A description",ctx =>
            {
                var scheduler = ctx.GetInstance<ISchedulerFactory>().GetScheduler();
                scheduler.JobFactory = ctx.GetInstance<IJobFactory>();
                return scheduler;
            });
    

    【讨论】:

      【解决方案2】:

      在这种情况下,您似乎只能在 => (“goes to”) 运算符右侧使用简单表达式(没有“语句体” - 意思是花括号) - 可能需要转换 lambda 表达式到表达式树。

      您可以将所有这些都包装在工厂模式中(在那些已经存在的工厂接口之上...factorception):

      public interface ICustomSchedulerFactory
      {
          Scheduler Create();
      }
      
      public class CustomSchedulerFactory: ICustomSchedulerFactory
      {
          private readonly IContainer _container;
          public CustomSchedulerFactory(IContainer container)
          {
              _container = container;
          }
      
          public Scheduler Create()
          {
              var scheduler = _container.GetInstance<ISchedulerFactory>().GetScheduler();
              scheduler.JobFactory = _container.GetInstance<IJobFactory>();
              return scheduler;
          }
      }
      

      (或者只是注入所需的依赖项而不是容器) 然后简单地

      ForSingletonOf<IScheduler>().Use(ctx => ctx.GetInstance<ICustomSchedulerFactory>().Create());
      

      【讨论】:

        猜你喜欢
        • 2023-04-01
        • 1970-01-01
        • 2013-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多