public void Ctor() { var c = new AnyServiceConfig(); c.MaxMultipartBoundaryLength.ShouldBe(50); c.MaxValueCount.ShouldBe(25); c.ManageEntityPermissions.ShouldBeTrue(); c.DefaultPaginationSettings.ShouldSatisfyAllConditions( () => c.DefaultPaginationSettings.DefaultOffset.ShouldBe(1), () => c.DefaultPaginationSettings.DefaultPageSize.ShouldBe(50), () => c.DefaultPaginationSettings.DefaultSortOrder.ShouldBe("asc") ); c.FilterFactoryType.ShouldBeOfType <DefaultFilterFactory>(); c.ModelPrepararType.ShouldBeOfType(typeof(DummyModelPreparar <>)); c.ServiceResponseMapperType.ShouldBeOfType <DataOnlyServiceResponseMapper>(); c.AuditSettings.Disabled.ShouldBeFalse(); c.AuditSettings.AuditRules.AuditCreate.ShouldBeTrue(); c.AuditSettings.AuditRules.AuditRead.ShouldBeTrue(); c.AuditSettings.AuditRules.AuditUpdate.ShouldBeTrue(); c.AuditSettings.AuditRules.AuditDelete.ShouldBeTrue(); c.ErrorEventKey.ShouldBe(LoggingEvents.UnexpectedException.Name); c.UseErrorEndpointForExceptionHandling.ShouldBeTrue(); c.UseLogRecordEndpoint.ShouldBeTrue(); c.MapperName.ShouldBe("default"); }
public CrudService( AnyServiceConfig config, IRepository <TEntity> repository, CrudValidatorBase <TEntity> validator, WorkContext workContext, IModelPreparar <TEntity> modelPreparar, IEventBus eventBus, IFileStoreManager fileStoreManager, IFilterFactory filterFactory, IPermissionManager permissionManager, IEnumerable <EntityConfigRecord> entityConfigRecords, ILogger <CrudService <TEntity> > logger) { Logger = logger; Config = config; Repository = repository; Validator = validator; WorkContext = workContext; ModelPreparar = modelPreparar; EventBus = eventBus; FileStorageManager = fileStoreManager; FilterFactory = filterFactory; PermissionManager = permissionManager; CurrentEntityConfigRecord = entityConfigRecords.First(typeof(TEntity)); }
private static void AddAnyServiceControllers(AnyServiceConfig config) { var list = new List <EntityConfigRecord>(config.EntityConfigRecords); if (!config.AuditSettings.Disabled) { list.Add(new EntityConfigRecord { Type = typeof(AuditRecord), EndpointSettings = new EndpointSettings { Area = "__anyservice", ControllerType = typeof(AuditController), } }); } if (config.UseLogRecordEndpoint) { list.Add(new EntityConfigRecord { Type = typeof(LogRecord), EndpointSettings = new EndpointSettings { Area = "__anyservice", ControllerType = typeof(LogRecordController), } }); } config.EntityConfigRecords = list; }
public IServiceCollection Configure(IServiceCollection services, IConfiguration configuration, IWebHostEnvironment env) { var config = new AnyServiceConfig { EntityConfigRecords = new[] { new EntityConfigRecord { Type = typeof(Product), Authorization = new AuthorizationInfo { PostAuthorizationNode = new AuthorizationNode { Roles = new[] { "product-create" } }, GetAuthorizationNode = new AuthorizationNode { Roles = new[] { "product-read" } }, PutAuthorizationNode = new AuthorizationNode { Roles = new[] { "product-update" } }, DeleteAuthorizationNode = new AuthorizationNode { Roles = new[] { "product-delete" } }, } } } }; services.AddAnyService(config); return(services); }
public void Configure(AnyServiceConfig config) { MappingExtensions.AddConfiguration(config.MapperName, cfg => { cfg.CreateMap <CategoryModel, Category>() .ForMember(dest => dest.AdminComment, mo => mo.Ignore()); }); }
public void ErrorEventKey_ModfiesErrorControllerEventKey() { var c = new AnyServiceConfig(); c.ErrorEventKey.ShouldBe(LoggingEvents.UnexpectedException.Name); ErrorController.ErrorEventKey.ShouldBe(LoggingEvents.UnexpectedException.Name); var newKey = "new-key"; c.ErrorEventKey = newKey; ErrorController.ErrorEventKey.ShouldBe(newKey); }
//private readonly ICrudService<T> _crudService; //private readonly IServiceResponseMapper _serviceResponseMapper; //private readonly ILogger<GenericParentController<T>> _logger; //private readonly AnyServiceConfig _config; //private readonly WorkContext _workContext; //private readonly Type _curType; //private readonly Type _mapToType; #endregion #region ctor public GenericParentController( IServiceProvider serviceProvider, AnyServiceConfig config, IServiceResponseMapper serviceResponseMapper, WorkContext workContext, ILogger <GenericParentController <T> > logger) { //_crudService = serviceProvider.GetService<ICrudService<T>>(); //_config = config; //_serviceResponseMapper = serviceResponseMapper; //_workContext = workContext; //_logger = logger; }
public static IServiceCollection AddAnyService(this IServiceCollection services, IEnumerable <Type> entities) { var config = new AnyServiceConfig { EntityConfigRecords = entities.Select(e => new EntityConfigRecord { Type = e, }) }; return(AddAnyService(services, config)); }
private static void NormalizeConfiguration(AnyServiceConfig config) { AddAnyServiceControllers(config); var temp = config.EntityConfigRecords.ToArray(); foreach (var ecr in temp) { var e = ecr.Type; var fn = e.FullName.ToLower(); var ekr = new EventKeyRecord(fn + "_created", fn + "_read", fn + "_update", fn + "_delete"); var pr = new PermissionRecord(fn + "_created", fn + "_read", fn + "_update", fn + "_delete"); ecr.Name ??= ecr.EndpointSettings != null && ecr.EndpointSettings.Area.HasValue() ? $"{ecr.EndpointSettings?.Area}_{ecr.Type.Name}" : ecr.Type.Name; var hasDuplication = temp.Where(e => e.Name == ecr.Name); if (hasDuplication.Count() > 1) { throw new InvalidOperationException($"Duplication in {nameof(EntityConfigRecord.Name)} field : {ecr.Name}. Please provide unique name for the controller. See configured entities where Routes equals {hasDuplication.First().EndpointSettings.Route} and {hasDuplication.Last().EndpointSettings.Route}"); } ecr.EventKeys ??= ekr; ecr.PermissionRecord ??= pr; ecr.EntityKey ??= fn; ecr.PaginationSettings ??= config.DefaultPaginationSettings; ecr.FilterFactoryType ??= config.FilterFactoryType; ecr.ModelPrepararType ??= config.ModelPrepararType; ecr.AuditSettings = NormalizeAudity(ecr, config.AuditSettings); ecr.EndpointSettings = NormalizeEndpointSettings(ecr, config); if (ecr.CrudValidatorType != null) { var cvType = typeof(CrudValidatorBase <>); //validate inheritance from CrudValidatorBase<> if (!ecr.CrudValidatorType.GetAllBaseTypes().All(t => t != cvType)) { throw new InvalidOperationException($"{ecr.CrudValidatorType.Name} must implement {typeof(CrudValidatorBase<>).Name}"); } } else { ecr.CrudValidatorType = typeof(AlwaysTrueCrudValidator <>).MakeGenericType(e); } } config.EntityConfigRecords = temp; }
public GenericController( IServiceProvider serviceProvider, AnyServiceConfig config, IServiceResponseMapper serviceResponseMapper, WorkContext workContext, ILogger <GenericController <TResponseObject, TDomainObject> > logger) { _crudService = serviceProvider.GetService <ICrudService <TDomainObject> >(); _config = config; _serviceResponseMapper = serviceResponseMapper; _workContext = workContext; _logger = logger; _curTypeName = _workContext.CurrentEntityConfigRecord.Name; _curType = _workContext.CurrentEntityConfigRecord.Type; _mapToType = _workContext.CurrentEntityConfigRecord.EndpointSettings.MapToType; _mapToPageType = _workContext.CurrentEntityConfigRecord.EndpointSettings.MapToPaginationType; _shouldMap = _curType != _mapToType; }
public IServiceCollection Configure(IServiceCollection services, IConfiguration configuration, IWebHostEnvironment env) { var config = new AnyServiceConfig { EntityConfigRecords = new[] { new EntityConfigRecord { Type = typeof(Product), Route = new PathString("/admin/product") } } }; services.AddAnyService(config); return(services); }
public IServiceCollection Configure(IServiceCollection services, IConfiguration configuration, IWebHostEnvironment env) { var config = new AnyServiceConfig { EntityConfigRecords = new[] { new EntityConfigRecord { Type = typeof(Product), ControllerType = typeof(AdminProductController), } } }; services.AddAnyService(config); return(services); }
public void ReturnExpectedActionResult(string result, TestClass1 payload, string message, Type expectedActionResultType) { var serRes = new ServiceResponse <TestClass1> { Result = result, Payload = payload, Message = message }; var c = new AnyServiceConfig { MapperName = "default" }; var mapper = new DataOnlyServiceResponseMapper(c); var r = mapper.MapServiceResponse <TestClass2>(serRes); r.ShouldBeOfType(expectedActionResultType); if (result == ServiceResult.Ok && payload != null) { var ok = r.ShouldBeOfType <OkObjectResult>(); (ok.Value as TestClass2).Id.ShouldBe(payload.Id.ToString()); } }
public DefaultServiceResponseMapper(AnyServiceConfig config) { _config = config; }
public AnyServiceConfig Configure(IServiceCollection services) { var anyServiceConfig = new AnyServiceConfig { EntityConfigRecords = new[] { new EntityConfigRecord { Type = typeof(Category), EndpointSettings = new EndpointSettings { Area = "admin" } }, new EntityConfigRecord { Type = typeof(Category), ShowSoftDelete = true, EndpointSettings = new EndpointSettings { MapToType = typeof(CategoryModel) } }, new EntityConfigRecord { Type = typeof(Product), EndpointSettings = new EndpointSettings { MapToType = typeof(ProductModel) } }, new EntityConfigRecord { Type = typeof(ProductAttribute) }, new EntityConfigRecord { Type = typeof(DependentModel), }, new EntityConfigRecord { Type = typeof(Stock), EndpointSettings = new EndpointSettings { Authorization = new AuthorizeAttribute { Roles = "some-role" } } }, new EntityConfigRecord { Type = typeof(Dependent2), EndpointSettings = new EndpointSettings { Route = "/api/d/", }, CrudValidatorType = typeof(Dependent2AlwaysTrueCrudValidator) }, new EntityConfigRecord { Type = typeof(MultipartSampleModel), }, new EntityConfigRecord { EndpointSettings = new EndpointSettings { Route = "/v1/my-great-route", ControllerType = typeof(CustomController), }, Type = typeof(CustomEntity), }, new EntityConfigRecord { Type = typeof(CustomEntity), Name = "area2_cutomModel" }, new EntityConfigRecord { Type = typeof(CustomEntity), Name = "method_not_allowed", EndpointSettings = new EndpointSettings { Route = "/api/na", PostSettings = new EndpointMethodSettings { Disabled = true }, PutSettings = new EndpointMethodSettings { Disabled = true }, DeleteSettings = new EndpointMethodSettings { Disabled = true }, } }, } }; services.AddAnyService(anyServiceConfig); return(anyServiceConfig); }
public DataOnlyServiceResponseMapper(AnyServiceConfig config) { _config = config; }
private static void RegisterDependencies(IServiceCollection services, AnyServiceConfig config) { services.TryAddSingleton <IdGeneratorFactory>(sp => { var stringGenerator = new StringIdGenerator(); var f = new IdGeneratorFactory(); f.AddOrReplace(typeof(string), stringGenerator); return(f); }); services.TryAddSingleton(config); services.TryAddScoped(sp => sp.GetService <WorkContext>().CurrentEntityConfigRecord?.AuditSettings ?? config.AuditSettings); services.TryAddScoped(typeof(ICrudService <>), typeof(CrudService <>)); // services. services.AddSingleton(config.EntityConfigRecords); //response mappers var mappers = config.EntityConfigRecords.Select(t => t.EndpointSettings.ResponseMapperType).ToArray(); foreach (var m in mappers) { services.TryAddSingleton(m); } //mapper factory services.TryAddSingleton <IMapperFactory, DefaultMapperFactory>(); //validator factory var validatorTypes = config.EntityConfigRecords.Select(t => t.CrudValidatorType).ToArray(); foreach (var vType in validatorTypes) { foreach (var vt in vType.GetAllBaseTypes(typeof(object))) { services.TryAddScoped(vt, vType); } } foreach (var ecr in config.EntityConfigRecords) { ValidateType <IFilterFactory>(ecr.FilterFactoryType); services.TryAddScoped(ecr.FilterFactoryType); var srv = typeof(IModelPreparar <>).MakeGenericType(ecr.Type); var impl = ecr.ModelPrepararType.IsGenericTypeDefinition ? ecr.ModelPrepararType.MakeGenericType(ecr.Type) : ecr.ModelPrepararType; services.TryAddScoped(srv, impl); } services.TryAddScoped(typeof(IFilterFactory), sp => { var wc = sp.GetService <WorkContext>(); var ffType = wc.CurrentEntityConfigRecord.FilterFactoryType; return(sp.GetService(ffType) as IFilterFactory); }); services.TryAddScoped <WorkContext>(); services.TryAddSingleton <IIdGenerator, StringIdGenerator>(); services.TryAddTransient <IPermissionManager, PermissionManager>(); services.TryAddTransient <ILogRecordManager, LogRecordManager>(); services.TryAddScoped(sp => { var wc = sp.GetService <WorkContext>(); var ct = wc.CurrentType; //var ecrm = sp.GetService<EntityConfigRecordManager>(); return(wc.CurrentEntityConfigRecord.EventKeys); }); services.AddScoped(sp => { var wc = sp.GetService <WorkContext>(); var ct = wc.CurrentType; return(wc.CurrentEntityConfigRecord.PermissionRecord); }); services.TryAddScoped(sp => { var wc = sp.GetService <WorkContext>(); var mt = wc.CurrentEntityConfigRecord?.EndpointSettings.ResponseMapperType ?? config.ServiceResponseMapperType; return(sp.GetService(mt) as IServiceResponseMapper); }); var auditManagerType = config.AuditSettings.Disabled ? typeof(DummyAuditManager) : typeof(AuditManager); services.TryAddTransient(typeof(IAuditManager), auditManagerType); services.TryAddSingleton <IEventBus, DefaultEventsBus>(); if (config.ManageEntityPermissions) { services.TryAddSingleton <IPermissionEventsHandler, DefaultPermissionsEventsHandler>(); } }
public static IServiceCollection AddAnyService(this IServiceCollection services, AnyServiceConfig config) { NormalizeConfiguration(config); RegisterDependencies(services, config); AddDefaultMapping(config.MapperName); AddEntityConfigRecordsMappings(config.MapperName, config.EntityConfigRecords); return(services); }
private static EndpointSettings NormalizeEndpointSettings(EntityConfigRecord ecr, AnyServiceConfig config) { var settings = (ecr.EndpointSettings ??= new EndpointSettings()); if (settings.Disabled) { return(null); } BuildControllerMethodSettings(settings, ecr); if (!settings.Route.HasValue) { var areaPrefix = settings.Area.HasValue() ? $"{settings.Area}/" : ""; settings.Route = new PathString($"/{areaPrefix}{ecr.Type.Name}"); } var route = settings.Route; if (route.Value.EndsWith("/")) { settings.Route = new PathString(route.Value[0..^ 1]);