private static BarcodeConfig GetConfig(IServiceProvider provider) { if (provider == null) { return(new BarcodeConfig()); } var options = provider.GetService <IOptions <BarcodeConfig> >(); BarcodeConfig config = options?.Value ?? new BarcodeConfig(); return(config); }
static Scannable() { Triggers <Scannable> .Inserting += entry => { BarcodeConfig config = GetConfig(entry.Service); if (string.IsNullOrEmpty(entry.Entity.BarcodeEntry)) { entry.Entity.BarcodeEntry = Generator.Generate(); } entry.Entity.Barcode.Alignment = config.Alignment; entry.Entity.BarcodeImage = ToBase64(entry.Entity.Barcode.Encode( config.BarcodeType, entry.Entity.Barcode.RawData, Color.Black, Color.White, entry.Entity.Barcode.Width, entry.Entity.Barcode.Height) ); }; Triggers <Scannable> .Updating += entry => { if (!string.IsNullOrEmpty(entry.Entity.BarcodeEntry)) { BarcodeConfig config = GetConfig(entry.Service); entry.Entity.Barcode.Alignment = config.Alignment; entry.Entity.BarcodeImage = ToBase64(entry.Entity.Barcode.Encode( config.BarcodeType, entry.Entity.Barcode.RawData, Color.Black, Color.White, entry.Entity.Barcode.Width, entry.Entity.Barcode.Height) ); } }; }