public static void UpdateSuccessMessageOnGoodWrite(IBizActionStatus bizStatus, IGenericBizRunnerConfig config) { if (bizStatus.HasErrors) { return; } if (bizStatus.Message != null && (bizStatus.Message == config.DefaultSuccessMessage || bizStatus.Message == StatusGenericHandler.ConstDefaultMessage)) { bizStatus.Message = config.DefaultSuccessAndWriteMessage; } else if (bizStatus.Message.LastOrDefault() != '.' && config.AppendToMessageOnGoodWriteToDb != null) { bizStatus.Message += config.AppendToMessageOnGoodWriteToDb; } }
public SetupDtoMappings(IGenericBizRunnerConfig publicConfig) { PublicConfig = publicConfig ?? throw new ArgumentNullException(nameof(publicConfig)); }
//see https://blogs.msdn.microsoft.com/dotnet/2016/09/29/implementing-seeding-custom-conventions-and-interceptors-in-ef-core-1-0/ //for why I call DetectChanges before ChangeTracker, and why I then turn ChangeTracker.AutoDetectChangesEnabled off/on around SaveChanges /// <summary> /// This will validate any entity classes that will be added or updated /// If the validation does not produce any errors then SaveChangesAsync will be called /// </summary> /// <param name="context"></param> /// <param name="config"></param> /// <returns>List of errors, empty if there were no errors</returns> public static async Task <IStatusGeneric> SaveChangesWithValidationAsync(this DbContext context, IGenericBizRunnerConfig config = null) { var status = context.ExecuteValidation(); return(status.HasErrors ? status : await context.SaveChangesWithExtrasAsync(config, true)); }
//see https://blogs.msdn.microsoft.com/dotnet/2016/09/29/implementing-seeding-custom-conventions-and-interceptors-in-ef-core-1-0/ //for why I call DetectChanges before ChangeTracker, and why I then turn ChangeTracker.AutoDetectChangesEnabled off/on around SaveChanges /// <summary> /// This will validate any entity classes that will be added or updated /// If the validation does not produce any errors then SaveChanges will be called /// </summary> /// <param name="context"></param> /// <param name="config"></param> /// <returns>List of errors, empty if there were no errors</returns> public static IStatusGeneric SaveChangesWithValidation(this DbContext context, IGenericBizRunnerConfig config = null) { var status = context.ExecuteValidation(); return(status.HasErrors ? status : context.SaveChangesWithExtras(config, true)); }
/// <summary> /// This is the method for registering GeneriBizRunner with .NET Core DI provider /// </summary> /// <typeparam name="TDefaultDbContext"></typeparam> /// <param name="services"></param> /// <param name="config"></param> public static void RegisterGenericBizRunnerBasic <TDefaultDbContext>(this IServiceCollection services, IGenericBizRunnerConfig config = null) where TDefaultDbContext : DbContext { services.AddScoped <DbContext>(sp => sp.GetService <TDefaultDbContext>()); services.AddTransient(typeof(IActionService <>), typeof(ActionService <>)); services.AddTransient(typeof(IActionServiceAsync <>), typeof(ActionServiceAsync <>)); //Register the GenericBizRunnerConfig if given if (config != null) { services.AddSingleton(config); } }
/// <summary> /// This SaveChanges, with a boolean to decide whether to validate or not /// </summary> /// <param name="context"></param> /// <param name="shouldValidate"></param> /// <param name="config"></param> /// <returns></returns> public static IStatusGeneric SaveChangesWithOptionalValidation(this DbContext context, bool shouldValidate, IGenericBizRunnerConfig config) { return(shouldValidate ? context.SaveChangesWithValidation(config) : context.SaveChangesWithExtras(config)); }
/// <summary> /// This creates the NonDiBizSetup - useful if you don't have any DTOs to map /// </summary> /// <param name="publicConfig"></param> public NonDiBizSetup(IGenericBizRunnerConfig publicConfig = null) { _config = publicConfig ?? new GenericBizRunnerConfig(); _bizInProfile = new BizRunnerProfile(); _bizOutProfile = new BizRunnerProfile(); }
public ActionServiceInOut(bool requiresSaveChanges, IGenericBizRunnerConfig config) : base(requiresSaveChanges, config) { }
//--------------------------------------------------- //private methods private dynamic CreateRequiredServiceInstance(ServiceBuilderLookup serviceBaseInfo, Type iBizType, Type genericInterfacePart, IGenericBizRunnerConfig config) { var genericAgruments = genericInterfacePart.GetGenericArguments().ToList(); genericAgruments.Insert(0, iBizType); return(Activator.CreateInstance( serviceBaseInfo.ServiceHandleType.MakeGenericType(genericAgruments.ToArray()), new object[] { RequiresSaveChanges, config })); }
/// <summary> /// This is the instance that can be called to run the service /// </summary> public dynamic GetServiceInstance(IGenericBizRunnerConfig config) { return(_bizInstance ?? (_bizInstance = CreateRequiredServiceInstance(_matchingServiceType, _iBizType, _extractedActionInterface, config))); }
/// <inheritdoc /> public ActionServiceAsync(DbContext context, TBizInstance bizInstance, IMapper mapper, IGenericBizRunnerConfig config = null) : base(context, bizInstance, mapper, config) { }
protected ActionServiceBase(bool requiresSaveChanges, IGenericBizRunnerConfig config) { RequiresSaveChanges = requiresSaveChanges; Config = config; }
public static void RegisterGenericBizRunnerMultiDbContext(this IServiceCollection services, IGenericBizRunnerConfig config = null) { services.AddTransient(typeof(IActionService <,>), typeof(ActionService <,>)); services.AddTransient(typeof(IActionServiceAsync <,>), typeof(ActionServiceAsync <,>)); //Register the GenericBizRunnerConfig if given and not already registered if (config != null && !services.Contains( new ServiceDescriptor(typeof(IGenericBizRunnerConfig), config), new CheckDescriptor())) { services.AddSingleton(config); } }
public IWrappedBizRunnerConfigAndMappings BuildWrappedConfigByScanningForDtos(Assembly[] assembliesToScan, IGenericBizRunnerConfig config) { if (assembliesToScan == null || assembliesToScan.Length == 0) { throw new ArgumentException("Needs assemblies to scan for DTOs. If not using DTOs just supply (Assembly)null as parameter." , nameof(assembliesToScan)); } foreach (var assembly in assembliesToScan.Where(x => x != null)) { ScanAssemblyAndAddToProfiles(assembly); } return(CreateWrappedConfig(config, _bizInProfile, _bizOutProfile)); }
/// <summary> /// This is the method for registering GenericBizRunner and any GenericBizRunner DTOs with .NET Core DI provider with config /// </summary> /// <typeparam name="TDefaultDbContext"></typeparam> /// <param name="services"></param> /// <param name="config"></param> /// <param name="assembliesToScan">These are the assemblies to scan for DTOs</param> public static void RegisterBizRunnerWithDtoScans <TDefaultDbContext>(this IServiceCollection services, IGenericBizRunnerConfig config, params Assembly[] assembliesToScan) where TDefaultDbContext : DbContext { if (config == null) { throw new ArgumentNullException(nameof(config)); } services.AddScoped <DbContext>(sp => sp.GetService <TDefaultDbContext>()); services.AddTransient(typeof(IActionService <>), typeof(ActionService <>)); services.AddTransient(typeof(IActionServiceAsync <>), typeof(ActionServiceAsync <>)); services.BuildRegisterWrappedConfig(config, assembliesToScan); }
//see https://blogs.msdn.microsoft.com/dotnet/2016/09/29/implementing-seeding-custom-conventions-and-interceptors-in-ef-core-1-0/ //for why I call DetectChanges before ChangeTracker, and why I then turn ChangeTracker.AutoDetectChangesEnabled off/on around SaveChanges /// <summary> /// This will validate any entity classes that will be added or updated /// If the validation does not produce any errors then SaveChanges will be called /// </summary> /// <param name="context"></param> /// <param name="config"></param> /// <returns>List of errors, empty if there were no errors</returns> public static IStatusGeneric SaveChangesWithValidation(this DbContext context, IGenericBizRunnerConfig config = null) { var status = context.ExecuteValidation(); if (status.HasErrors) { return(status); } context.ChangeTracker.AutoDetectChangesEnabled = false; try { context.SaveChanges(); } catch (Exception e) { var exStatus = config?.SaveChangesExceptionHandler(e, context); if (exStatus == null) { throw; //error wasn't handled, so rethrow } status.CombineErrors(exStatus); } finally { context.ChangeTracker.AutoDetectChangesEnabled = true; } return(status); }
/// <summary> /// This is used to register GenericBizRunner and any GenericBizRunner DTOs with .NET Core DI provider to work with multiple DbContexts /// </summary> /// <param name="services"></param> /// <param name="config"></param> /// <param name="assembliesToScan">These are the assemblies to scan for DTOs</param> public static void RegisterBizRunnerMultiDbContextWithDtoScans(this IServiceCollection services, IGenericBizRunnerConfig config, params Assembly[] assembliesToScan) { if (config == null) { throw new ArgumentNullException(nameof(config)); } services.AddTransient(typeof(IActionService <,>), typeof(ActionService <,>)); services.AddTransient(typeof(IActionServiceAsync <,>), typeof(ActionServiceAsync <,>)); services.BuildRegisterWrappedConfig(config, assembliesToScan); }
/// <summary> /// This SaveChangesAsync, with a boolean to decide whether to validate or not /// </summary> /// <param name="context"></param> /// <param name="shouldValidate"></param> /// <param name="config"></param> /// <returns></returns> public static async Task <IStatusGeneric> SaveChangesWithOptionalValidationAsync(this DbContext context, bool shouldValidate, IGenericBizRunnerConfig config) { return(shouldValidate ? await context.SaveChangesWithValidationAsync(config).ConfigureAwait(false) : await context.SaveChangesWithExtrasAsync(config).ConfigureAwait(false)); }
internal WrappedBizRunnerConfigAndMappings(IGenericBizRunnerConfig config, MapperConfiguration toBizMapping, MapperConfiguration fromBizMapping) { Config = config ?? throw new ArgumentNullException(nameof(config)); ToBizIMapper = toBizMapping?.CreateMapper() ?? throw new ArgumentNullException(nameof(toBizMapping)); FromBizIMapper = fromBizMapping?.CreateMapper() ?? throw new ArgumentNullException(nameof(fromBizMapping)); }