Autofac 和 Quartz.Net 集成

发布于 2024-10-16 07:46:29 字数 946 浏览 6 评论 0 原文

有人有集成 autofacQuartz.Net 的经验吗?如果是这样,最好在哪里控制生命周期管理——IJobFactory、IJob 的执行中还是通过事件侦听器?


现在,我正在使用自定义 autofac IJobFactory 来创建 IJob 实例,但我没有一种简单的方法来插入 ILifetimeScope< /code> 在 IJobFactory 中,以确保清除注入 IJob 中的所有昂贵资源。作业工厂只是创建一个作业实例并返回它。以下是我当前的想法(希望有更好的想法...)

  • 看起来大多数 AutoFac 集成都以某种方式将 ILifetimeScope 包裹在他们创建的工作单元周围。明显的强力方法似乎是将 ILifetimeScope 传递到 IJob 中,并让 Execute 方法创建一个子 ILifetimeScope code> 并实例化那里的任何依赖项。这似乎有点太接近服务定位器模式,这反过来似乎违背了 autofac 的精神,但它可能是确保正确处理作用域的最明显的方法。

  • 我可以插入一些 Quartz 事件来处理作业执行堆栈的不同阶段,并在那里处理生命周期管理。这可能需要做更多的工作,但如果它能够更清晰地分离关注点,那么可能是值得的。

  • 确保 IJob 是 IServiceComponent 类型的简单包装器,它将完成所有工作,并将其请求为 Owned Func。我喜欢 autofac 的感觉,但我不喜欢它对于 IJob 的所有实现者来说不是严格执行的。

Does anyone have any experience integrating autofac and Quartz.Net? If so, where is it best to control lifetime management -- the IJobFactory, within the Execute of the IJob, or through event listeners?


Right now, I'm using a custom autofac IJobFactory to create the IJob instances, but I don't have an easy way to plug in to a ILifetimeScope in the IJobFactory to ensure any expensive resources that are injected in the IJob are cleaned up. The job factory just creates an instance of a job and returns it. Here are my current ideas (hopefully there are better ones...)

  • It looks like most AutoFac integrations somehow wrap a ILifetimeScope around the unit of work they create. The obvious brute force way seems to be to pass an ILifetimeScope into the IJob and have the Execute method create a child ILifetimeScope and instantiate any dependencies there. This seems a little too close to a service locator pattern, which in turn seems to go against the spirit of autofac, but it might be the most obvious way to ensure proper handling of a scope.

  • I could plug into some of the Quartz events to handle the different phases of the Job execution stack, and handle lifetime management there. That would probably be a lot more work, but possibly worth it if it gets cleaner separation of concerns.

  • Ensure that an IJob is a simple wrapper around an IServiceComponent type, which would do all the work, and request it as Owned<T>, or Func<Owned<T>>. I like how this seems to vibe more with autofac, but I don't like that its not strictly enforceable for all implementors of IJob.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间☆小暴躁 2024-10-23 07:46:29

在不太了解 Quartz.Net 和 IJob 的情况下,我仍然大胆提出一个建议。

考虑以下作业包装器:

public class JobWrapper<T>: IJob where T:IJob
{
    private Func<Owned<T>> _jobFactory;

    public JobWrapper(Func<Owned<T>> jobFactory)
    {
        _jobFactory = jobFactory;
    }


    void IJob.Execute()
    {
        using (var ownedJob = _jobFactory())
        {
            var theJob = ownedJob.Value;
            theJob.Execute();
        }
    }
}

给定以下注册:

builder.RegisterGeneric(typeof(JobWrapper<>));
builder.RegisterType<SomeJob>();

作业工厂现在可以解析此包装器:

var job = _container.Resolve<JobWrapper<SomeJob>>();

注意:a 生命周期范围 将作为 ownedJob 实例的一部分创建,在本例中为 Owned 类型。 SomeJob 所需的任何 InstancePerLifetimeScopeInstancePerDependency 依赖项都将与 Owned 实例一起创建和销毁。

Without knowing too much about Quartz.Net and IJobs, I'll venture a suggestion still.

Consider the following Job wrapper:

public class JobWrapper<T>: IJob where T:IJob
{
    private Func<Owned<T>> _jobFactory;

    public JobWrapper(Func<Owned<T>> jobFactory)
    {
        _jobFactory = jobFactory;
    }


    void IJob.Execute()
    {
        using (var ownedJob = _jobFactory())
        {
            var theJob = ownedJob.Value;
            theJob.Execute();
        }
    }
}

Given the following registrations:

builder.RegisterGeneric(typeof(JobWrapper<>));
builder.RegisterType<SomeJob>();

A job factory could now resolve this wrapper:

var job = _container.Resolve<JobWrapper<SomeJob>>();

Note: a lifetime scope will be created as part of the ownedJob instance, which in this case is of type Owned<SomeJob>. Any dependencies required by SomeJob that is InstancePerLifetimeScope or InstancePerDependency will be created and destroyed along with the Owned instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文