📑 SingletonJobFactory.cs Documentation
Assembly: IrbyImaging.BusinessLogic
Namespace:IrbyImaging.BusinessLogic.Scheduler
Author: kvamshi
Created: 03‑31‑2022
Last Modified: 03‑31‑2022
🗂️ Table of Contents
- Overview & Purpose
- Class Diagram (UML)
- Key Functional Areas
- Dependencies & Technologies
- API Endpoints Overview
- View Models & Data Models
- Helper Methods & Utilities
- Error Handling & Return Types
- Third‑party Integrations
- Sample Code Blocks & Usage Examples
- Conclusion
1. Overview & Purpose
SingletonJobFactory is a custom Quartz.NET job factory that integrates with .NET’s dependency injection (DI) to instantiate scheduler jobs as singleton services. Instead of letting Quartz create new job instances via reflection, this factory:
- 🛠️ Leverages
IServiceProviderto resolve job types - 🔄 Ensures that all your jobs participate in the same DI scope
- 🔒 Allows constructor injection for clean, testable code
Use‑Case: When scheduling recurring or on‑demand tasks (e.g. image processing, notifications), you often require dependencies (database contexts, HTTP clients, loggers). SingletonJobFactory wires up Quartz with your DI container so that each job’s dependencies are satisfied automatically.
2. Class Diagram (UML)
classDiagram
IJobFactory <|-- SingletonJobFactory
class SingletonJobFactory {
- IServiceProvider _serviceProvider
+ SingletonJobFactory(IServiceProvider)
+ IJob NewJob(TriggerFiredBundle, IScheduler)
+ void ReturnJob(IJob)
}
3. Key Functional Areas
| Area | Description |
|---|---|
| 🔌 Dependency Injection | Accepts an IServiceProvider in the constructor. |
| 🏗️ Job Creation | Resolves bundle.JobDetail.JobType from DI in NewJob(...). |
| 🔙 Job Return | Implements ReturnJob(...) as a no‑op (Quartz does manual cleanup). |
4. Dependencies & Technologies
| Package / Namespace | Purpose |
|---|---|
| Microsoft.Extensions.DependencyInjection | .NET Core DI container |
| Quartz | Job scheduling framework |
| Quartz.Spi | SPI for custom IJobFactory |
| System | Base .NET types |
5. API Endpoints Overview
❌ No Web API endpoints are defined in this file.
This class purely sits in the Scheduler layer and is used internally at runtime.
6. View Models & Data Models
❌ None in this context.
SingletonJobFactory does not define any data or view models.
7. Helper Methods & Utilities
| Method | Description |
|---|---|
NewJob(TriggerFiredBundle, IScheduler) |
Resolves a job instance from IServiceProvider. |
ReturnJob(IJob) |
No‑op method called after job execution. |
8. Error Handling & Return Types
-
NewJob
- Return Type:
IJob - May throw a
SchedulerException(or underlying DI exceptions) if the job type is not registered. - Quartz will mark the trigger as Error on failure, requiring manual intervention.
- Return Type:
-
ReturnJob
- Return Type:
void - Currently no cleanup is performed here. If you need custom disposal, implement it here.
- Return Type:
9. Third‑party Integrations
- Quartz.NET: Scheduling engine
- Microsoft DI: Service locator to create job instances
No other external integrations in this file.
10. Sample Code Blocks & Usage Examples
10.1 Registering the Factory in .NET Core
// In Startup.cs or Program.cs (generic host)
using IrbyImaging.BusinessLogic.Scheduler;
using Quartz;
using Quartz.Spi;
public void ConfigureServices(IServiceCollection services)
{
// 1. Register your jobs
services.AddTransient<MyImageProcessingJob>();
services.AddTransient<AnotherScheduledJob>();
// 2. Register the SingletonJobFactory
services.AddSingleton<IJobFactory, SingletonJobFactory>();
// 3. Configure Quartz
services.AddQuartz(q =>
{
q.UseMicrosoftDependencyInjectionJobFactory<SingletonJobFactory>();
// Example: Add a job and trigger
var jobKey = new JobKey("ImageJob", "Group1");
q.AddJob<MyImageProcessingJob>(opts => opts
.WithIdentity(jobKey)
.StoreDurably());
q.AddTrigger(opts => opts
.ForJob(jobKey)
.WithIdentity("ImageJob-trigger")
.WithCronSchedule("0 0/5 * * * ?")); // run every 5 minutes
});
// 4. Add Quartz hosted service
services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
}
10.2 Sample Job Implementation
public class MyImageProcessingJob : IJob
{
private readonly ILogger<MyImageProcessingJob> _logger;
private readonly IImageService _imageService;
public MyImageProcessingJob(ILogger<MyImageProcessingJob> logger, IImageService imageService)
{
_logger = logger;
_imageService = imageService;
}
public async Task Execute(IJobExecutionContext context)
{
_logger.LogInformation("Starting image processing...");
await _imageService.ProcessPendingImagesAsync();
_logger.LogInformation("Image processing completed.");
}
}
11. Conclusion
🎯 SingletonJobFactory bridges the gap between Quartz.NET and ASP.NET Core’s DI, allowing your scheduled jobs to be resolved from the container with full support for constructor injection, lifetimes, and scoped services. By registering this factory, you ensure that:
- Your job dependencies are managed by the same DI container as the rest of your application.
- You can swap, mock, or decorate jobs easily for testing.
- The scheduler behaves consistently in cloud or containerized environments.
With this in place, adding, testing, and maintaining scheduled jobs becomes straightforward and aligned with modern .NET architecture best practices. 🚀