π ODSUpdate.cs Documentation
File Path:
IrbyImaging.BusinessLogic/Jobs/ODSUpdate.cs
Assembly: IrbyImaging.BusinessLogic
Author: kvamshi
Created: 03-31-2022
Last Modified: 03-31-2022
π Table of Contents
- Overview & Purpose
- Architectural Context
- Dependencies & Technologies
- Key Functional Areas
- Class:
ODSUpdate - Usage & Integration
- Error Handling & Logging
- Sample Code Snippets
- Mermaid UML Diagram
- Conclusion
π― Overview & Purpose
The ODSUpdate job is designed to synchronize and refresh the Operational Data Store (ODS) within the IrbyImaging ecosystem.
By leveraging Quartz.NET scheduling and a combination of Entity Framework Core, SMO (SQL Management Objects), and raw SQL script execution, this job ensures that:
- π Jobs, Employees, Customers, Assets, Purchase Orders, and Work Orders data are upβtoβdate.
- π Custom SQL scripts (e.g., status updates) are applied to the production database.
This helps downstream reporting, analytics, and other systems to read from a consistent, denormalized ODS layer.
ποΈ Architectural Context
[Quartz Scheduler]
β Execute()
[ODSUpdate Job]
β UpdateODS()
ββββββββββββββββββββββββ
β IrbyContext (EF) β βββ
ββββββββββββββββββββββββ β Dependency Injection
β β
[ ODSManager Service ] β
ββββββββββββββββββββββββββββ
β Calls: UpdateJob(), UpdateEmployee(), ...
β
[ SQL Server (Production) ]
β Executes .sql scripts via SMO
- Quartz.NET triggers
Execute(). - DI container provides
IConfiguration,IServiceProvider,IHttpContextAccessor,IServiceScopeFactory. ODSManagerencapsulates per-entity data-sync logic.- Raw scripts under
wwwroot/Scripts/*.sqlare executed using SMO.
π Dependencies & Technologies
| Category | Package / Technology | Purpose |
|---|---|---|
| Scheduling | Quartz | Job scheduling & trigger management |
| ORM / Data Access | Microsoft.EntityFrameworkCore | Database context (IrbyContext) |
| SQL Management | Microsoft.SqlServer.Management.Smo & Common | Execute raw SQL scripts via SMO |
| ADO.NET | Microsoft.Data.SqlClient | Lowβlevel SQL connection and statistics retrieval |
| Configuration | Microsoft.Extensions.Configuration | Load connection strings & settings |
| Dependency Injection | Microsoft.Extensions.DependencyInjection | ServiceProvider & scopes |
| HTTP Context | Microsoft.AspNetCore.Http | Access to incoming HTTP context if needed |
| CSV Processing | CsvHelper | (Unused in this file but available for potential CSV I/O) |
π Key Functional Areas
-
Initialization & DI
Injects required services:IServiceProviderIConfigurationIHttpContextAccessorIServiceScopeFactory
-
Quartz Job Execution
ImplementsIJob.Execute(IJobExecutionContext)to KickβoffUpdateODS(). -
EntityβBased Sync
CallsODSManagerto update:- Jobs
- Employees
- Customers
- Assets
- Purchase Orders
- Work Orders
-
Script Execution via SMO
LoadsODS_D_EmployeeStatusUpdate.sqlfromwwwroot/Scriptsand executes it against the target database. -
Logging & Error Handling
Captures SQL info messages, warnings, and errors viaOnInfoMessageGenerated.
π·οΈ Class: ODSUpdate
public class ODSUpdate : IJob
{
// DI-backed fields
private readonly IServiceProvider _provider;
private readonly IServiceScopeFactory _factory;
private IConfiguration _configuration;
private IHttpContextAccessor _httpContextAccessor;
// Shared message log
public static string message = string.Empty;
// Constructor
public ODSUpdate(IServiceProvider provider,
IConfiguration configuration,
IHttpContextAccessor httpContextAccessor,
IServiceScopeFactory factory) { ... }
// Quartz entry point
public Task Execute(IJobExecutionContext context) { ... }
// Main ODS update routine
public void UpdateODS() { ... }
// Event handler for SQL messages
private static void OnInfoMessageGenerated(object sender, SqlInfoMessageEventArgs args) { ... }
}
Properties
| Name | Type | Description |
|---|---|---|
_provider |
IServiceProvider |
.NET DI service provider |
_factory |
IServiceScopeFactory |
For creating scoped services |
_configuration |
IConfiguration |
Application configuration (e.g., connection strings) |
_httpContextAccessor |
IHttpContextAccessor |
Provides access to HTTP context |
message |
string (static) |
Aggregated log of SQL messages & errors |
Constructor
public ODSUpdate(
IServiceProvider provider,
IConfiguration configuration,
IHttpContextAccessor httpContextAccessor,
IServiceScopeFactory factory)
{
_provider = provider;
_configuration = configuration;
_httpContextAccessor = httpContextAccessor;
_factory = factory;
}
- Purpose: Capture all required dependencies via DI.
- Usage: Registered as a Quartz job in Startup.
Methods
| Method | Return Type | Description |
|---|---|---|
Task Execute(IJobExecutionContext) |
Task |
Quartz entry point. Calls UpdateODS(). |
void UpdateODS() |
void |
Main routine to sync data & run scripts. |
void OnInfoMessageGenerated(...) |
void (static) |
Handler for ADO.NET info messages & errors. |
Execute
public Task Execute(IJobExecutionContext context)
{
UpdateODS();
return Task.CompletedTask;
}
- Invoked by: Quartz scheduler.
- Action: Triggers the full ODS sync routine.
UpdateODS
- Load Connection String
var sqlConnectionString = _configuration["ConnectionStringsProduction"]; - Initialize EF Context & Manager
var context = new IrbyContext(_configuration, _httpContextAccessor); var manager = new ODSManager(context); - Perform Entity Updates
manager.UpdateJob(sqlConnectionString); manager.UpdateEmployee(sqlConnectionString); manager.UpdateCustomer(sqlConnectionString); manager.UpdateAsset(sqlConnectionString); manager.UpdatePurchaseOrder(sqlConnectionString); manager.UpdateWorkOrder(sqlConnectionString); - Execute Custom SQL Script
var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Scripts", "ODS_D_EmployeeStatusUpdate.sql"); string script = File.ReadAllText(path); using var conn = new SqlConnection(sqlConnectionString); var server = new Server(new ServerConnection(conn)); server.ConnectionContext.ExecuteNonQuery(script); - (Optional) Logging to File
Commented out by default; can be enabled for detailed logging.
OnInfoMessageGenerated
- Purpose: Build up the
messagelog whenever SQL Server emits an InfoMessage or Error. - Registers with:
conn.InfoMessage += OnInfoMessageGenerated;(commented out by default).
π Usage & Integration
-
Registering with DI & Quartz
// In Startup.cs or wherever you configure services services.AddSingleton<ODSUpdate>(); services.AddQuartz(q => { q.UseMicrosoftDependencyInjectionJobFactory(); var jobKey = new JobKey("ODSUpdateJob"); q.AddJob<ODSUpdate>(opts => opts.WithIdentity(jobKey)); q.AddTrigger(opts => opts .ForJob(jobKey) .WithIdentity("ODSUpdateTrigger") .WithSimpleSchedule(x => x .WithIntervalInHours(1) .RepeatForever())); }); services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true); -
Configuration
// appsettings.json { "ConnectionStringsProduction": "Server=...;Database=IrbyImaging;Trusted_Connection=True;TrustServerCertificate=True;MultipleActiveResultSets=true;" } -
Script Deployment
- Place
ODS_D_EmployeeStatusUpdate.sqlunderwwwroot/Scripts/. - Ensure folder & file are included in build/publish (set ).
- Place
π οΈ Error Handling & Logging
- SMO Execution: Any script errors bubble up as exceptions.
- InfoMessage Handler:
- Captures warnings/info from SQL Server.
- Aggregates into the static
messagefield. - Can be written out to a
.txtlog if the commented code is enabled.
π Sample Code Snippets
Triggering the Job Manually
// Obtain scheduler
var scheduler = await StdSchedulerFactory.GetDefaultScheduler();
await scheduler.Start();
// Trigger the ODSUpdate job immediately
await scheduler.TriggerJob(new JobKey("ODSUpdateJob"));
Reading the Log
string logContents = ODSUpdate.message;
Console.WriteLine(logContents);
π Mermaid UML Diagram
classDiagram
class ODSUpdate {
- IServiceProvider _provider
- IServiceScopeFactory _factory
- IConfiguration _configuration
- IHttpContextAccessor _httpContextAccessor
+ static string message
+ ODSUpdate(provider, configuration, httpContextAccessor, factory)
+ Task Execute(IJobExecutionContext context)
+ void UpdateODS()
- void OnInfoMessageGenerated(object, SqlInfoMessageEventArgs)
}
class ODSManager {
+ UpdateJob(string conn)
+ UpdateEmployee(string conn)
+ UpdateCustomer(string conn)
+ UpdateAsset(string conn)
+ UpdatePurchaseOrder(string conn)
+ UpdateWorkOrder(string conn)
}
ODSUpdate --> ODSManager : uses
ODSUpdate --> IrbyContext : creates
ODSUpdate --> SqlConnection : opens
SqlConnection --> Server : used by
β Conclusion
The ODSUpdate job is a central component for maintaining the Operational Data Store in the IrbyImaging platform. By orchestrating multiple entityβbased updates via ODSManager and executing custom SQL scripts, it ensures data consistency and freshness for reporting and analytics.
With Quartz.NET, EF Core, and SMO working in concert, this job demonstrates a robust pattern for scheduled, multiβlayered database synchronization tasks.
Feel free to extend or customize:
- Add more scripts under
wwwroot/Scripts. - Hook into
OnInfoMessageGeneratedfor richer logging. - Adjust Quartz triggers for different schedules.
Happy coding! π