public async Task Invoke(InvokeContext <TService> ctx) { IDataHelper <TService> dataHelper = ctx.ServiceContainer.GetDataHelper(); IServiceOperationsHelper <TService> serviceHelper = ctx.ServiceContainer.GetServiceHelper(); RunTimeMetadata metadata = ctx.Service.GetMetadata(); MethodDescription method = metadata.GetInvokeMethod(ctx.Request.methodName); List <object> methParams = new List <object>(); for (int i = 0; i < method.parameters.Count; ++i) { methParams.Add(ctx.Request.paramInfo.GetValue(method.parameters[i].name, method, dataHelper)); } RequestContext req = InvokeContext <TService> .CreateRequestContext(ctx.Service); using (RequestCallContext callContext = new RequestCallContext(req)) { MethodInfoData methodData = method.GetMethodData(); object instance = serviceHelper.GetMethodOwner(methodData); object invokeRes = methodData.MethodInfo.Invoke(instance, methParams.ToArray()); object methodResult = await serviceHelper.GetMethodResult(invokeRes); if (method.methodResult) { ctx.Response.result = methodResult; } } await _next(ctx); }
public virtual string GenerateScript(string comment = null, bool isDraft = false) { RunTimeMetadata metadata = Owner.GetMetadata(); TypeScriptHelper helper = new TypeScriptHelper(_serializer, _dataHelper, _valueConverter, metadata, _clientTypes()); return(helper.CreateTypeScript(comment)); }
public async Task Invoke(CRUDContext <TService> ctx) { IAuthorizer <TService> authorizer = ctx.ServiceContainer.GetAuthorizer(); RunTimeMetadata metadata = ctx.Service.GetMetadata(); foreach (DbSet dbSet in ctx.Request.dbSets) { //methods on domain service which are attempted to be executed by client (SaveChanges triggers their execution) Dictionary <string, MethodInfoData> domainServiceMethods = new Dictionary <string, MethodInfoData>(); DbSetInfo dbInfo = metadata.DbSets[dbSet.dbSetName]; dbSet.rows.Aggregate <RowInfo, Dictionary <string, MethodInfoData> >(domainServiceMethods, (dict, rowInfo) => { MethodInfoData method = rowInfo.GetCRUDMethodInfo(metadata, dbInfo.dbSetName); if (method == null) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_REC_CHANGETYPE_INVALID, dbInfo.GetEntityType().Name, rowInfo.changeType)); } string dicKey = string.Format("{0}:{1}", method.OwnerType.FullName, method.MethodInfo.Name); if (!dict.ContainsKey(dicKey)) { dict.Add(dicKey, method); } return(dict); }); await authorizer.CheckUserRightsToExecute(domainServiceMethods.Values); } await _next(ctx); }
public async Task Invoke(RefreshContext <TService> ctx) { DbSetInfo dbSetInfo = ctx.Request.GetDbSetInfo() ?? throw new InvalidOperationException($"Could not get the DbSet for {ctx.Request.dbSetName}"); IServiceOperationsHelper <TService> serviceHelper = ctx.ServiceContainer.GetServiceHelper(); RunTimeMetadata metadata = ctx.Service.GetMetadata(); RequestContext req = RefreshContext <TService> .CreateRequestContext(ctx.Service, ctx.Request.rowInfo); using (RequestCallContext callContext = new RequestCallContext(req)) { MethodInfoData methodData = metadata.GetOperationMethodInfo(ctx.Request.dbSetName, MethodType.Refresh); object instance = serviceHelper.GetMethodOwner(methodData); object invokeRes = methodData.MethodInfo.Invoke(instance, new object[] { ctx.Request }); object dbEntity = await serviceHelper.GetMethodResult(invokeRes); if (dbEntity != null) { serviceHelper.UpdateRowInfoFromEntity(dbEntity, ctx.Request.rowInfo); } else { throw new InvalidOperationException($"Refresh Operation for {ctx.Request.dbSetName} did not return a result"); } } await _next(ctx); }
public async Task UpdateEntity(RunTimeMetadata metadata, RowInfo rowInfo) { DbSetInfo dbSetInfo = rowInfo.GetDbSetInfo(); if (rowInfo.changeType != ChangeType.Updated) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_REC_CHANGETYPE_INVALID, dbSetInfo.GetEntityType().Name, rowInfo.changeType)); } MethodInfoData methodData = metadata.GetOperationMethodInfo(dbSetInfo.dbSetName, MethodType.Update); if (methodData == null) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_DB_UPDATE_NOT_IMPLEMENTED, dbSetInfo.GetEntityType().Name, GetType().Name)); } object dbEntity = Activator.CreateInstance(dbSetInfo.GetEntityType()); UpdateEntityFromRowInfo(dbEntity, rowInfo, false); object original = GetOriginalEntity(dbEntity, rowInfo); rowInfo.GetChangeState().Entity = dbEntity; rowInfo.GetChangeState().OriginalEntity = original; object instance = GetMethodOwner(methodData); //apply this changes to entity that is in the database (this is done in user domain service method) object res = methodData.MethodInfo.Invoke(instance, new[] { dbEntity }); if (res is Task) { await(res as Task); } }
public async Task <Permissions> ServiceGetPermissions() { try { await Task.CompletedTask; RunTimeMetadata metadata = GetMetadata(); Permissions result = new Permissions() { serverTimezone = DateTimeHelper.GetTimezoneOffset() }; IAuthorizer authorizer = ServiceContainer.Authorizer; foreach (DbSetInfo dbInfo in metadata.DbSets.Values) { DbSetPermit permissions = await authorizer.GetDbSetPermissions(metadata, dbInfo.dbSetName); result.permissions.Add(permissions); } return(result); } catch (Exception ex) { _OnError(ex); throw new DummyException(ex.GetFullMessage(), ex); } }
public async Task DeleteEntity(RunTimeMetadata metadata, RowInfo rowInfo) { DbSetInfo dbSetInfo = rowInfo.GetDbSetInfo(); if (rowInfo.changeType != ChangeType.Deleted) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_REC_CHANGETYPE_INVALID, dbSetInfo.GetEntityType().Name, rowInfo.changeType)); } MethodInfoData methodData = metadata.GetOperationMethodInfo(dbSetInfo.dbSetName, MethodType.Delete); if (methodData == null) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_DB_DELETE_NOT_IMPLEMENTED, dbSetInfo.GetEntityType().Name, GetType().Name)); } object dbEntity = Activator.CreateInstance(dbSetInfo.GetEntityType()); UpdateEntityFromRowInfo(dbEntity, rowInfo, true); rowInfo.GetChangeState().Entity = dbEntity; rowInfo.GetChangeState().OriginalEntity = dbEntity; object instance = GetMethodOwner(methodData); object res = methodData.MethodInfo.Invoke(instance, new[] { dbEntity }); if (res is Task) { await(res as Task); } }
public async Task Invoke(QueryContext <TService> ctx) { Security.IAuthorizer <TService> authorizer = ctx.ServiceContainer.GetAuthorizer(); RunTimeMetadata metadata = ctx.Service.GetMetadata(); MethodDescription method = metadata.GetQueryMethod(ctx.Request.dbSetName, ctx.Request.queryName); await authorizer.CheckUserRightsToExecute(method.GetMethodData()); await _next(ctx); }
private async Task Delete(CRUDContext <TService> ctx, RunTimeMetadata metadata, ChangeSetRequest changeSet, RowInfo rowInfo) { TService service = ctx.Service; IServiceOperationsHelper <TService> serviceHelper = ctx.ServiceContainer.GetServiceHelper(); CheckRowInfo(rowInfo); using (RequestCallContext callContext = new RequestCallContext(CRUDContext <TService> .CreateRequestContext(service, changeSet, rowInfo))) { rowInfo.SetChangeState(new EntityChangeState()); await serviceHelper.DeleteEntity(metadata, rowInfo);; } }
public TypeScriptHelper(ISerializer serializer, IDataHelper dataHelper, IValueConverter valueConverter, RunTimeMetadata metadata, IEnumerable <Type> clientTypes) { _serializer = serializer ?? throw new ArgumentNullException(nameof(serializer)); _dataHelper = dataHelper ?? throw new ArgumentNullException(nameof(dataHelper)); _valueConverter = valueConverter ?? throw new ArgumentNullException(nameof(valueConverter)); _metadata = metadata ?? throw new ArgumentNullException(nameof(metadata)); _clientTypes = new List <Type>(clientTypes ?? Enumerable.Empty <Type>()); _dbSets = _metadata.DbSets.Values.OrderBy(v => v.dbSetName).ToList(); _associations = _metadata.Associations.Values.OrderBy(a => a.name).ToList(); }
private async Task Insert(CRUDContext <TService> ctx, RunTimeMetadata metadata, ChangeSetRequest changeSet, IChangeSetGraph graph, RowInfo rowInfo) { TService service = ctx.Service; IServiceOperationsHelper <TService> serviceHelper = ctx.ServiceContainer.GetServiceHelper(); CheckRowInfo(rowInfo); using (RequestCallContext callContext = new RequestCallContext(CRUDContext <TService> .CreateRequestContext(service, changeSet, rowInfo))) { rowInfo.SetChangeState(new EntityChangeState { ParentRows = graph.GetParents(rowInfo) }); await serviceHelper.InsertEntity(metadata, rowInfo); } }
public async Task Invoke(CRUDContext <TService> ctx) { IServiceOperationsHelper <TService> serviceHelper = ctx.ServiceContainer.GetServiceHelper(); RunTimeMetadata metadata = ctx.Service.GetMetadata(); ChangeSetRequest changeSet = ctx.Request; ChangeSetGraph graph = new ChangeSetGraph(ctx.Request, metadata); graph.Prepare(); ctx.Properties.Add(CRUDContext <TService> .CHANGE_GRAPH_KEY, graph); RowInfo currentRowInfo = null; try { foreach (RowInfo rowInfo in graph.InsertList) { currentRowInfo = rowInfo; await Insert(ctx, metadata, changeSet, graph, rowInfo); } foreach (RowInfo rowInfo in graph.UpdateList) { currentRowInfo = rowInfo; await Update(ctx, metadata, changeSet, rowInfo); } foreach (RowInfo rowInfo in graph.DeleteList) { currentRowInfo = rowInfo; await Delete(ctx, metadata, changeSet, rowInfo); } } catch (Exception ex) { if (currentRowInfo != null) { object dbEntity = currentRowInfo.GetChangeState()?.Entity; currentRowInfo.SetChangeState(new EntityChangeState { Entity = dbEntity, Error = ex }); } throw; } await _next(ctx); }
public static string CreateMethods(RunTimeMetadata metadata, DbContext DB) { StringBuilder sb = new StringBuilder(4096); System.Collections.Generic.List <DbSetInfo> dbSets = metadata.DbSets.Values.OrderBy(d => d.dbSetName).ToList(); dbSets.ForEach(dbSetInfo => { string tableName = GetTableName(DB, dbSetInfo.GetEntityType()); if (tableName == string.Empty) { return; } sb.AppendLine(CreateDbSetMethods(dbSetInfo, tableName)); }); return(sb.ToString()); }
public object GetMethodOwner(MethodInfoData methodData) { if (!methodData.IsInDataManager) { return(_domainService); } if (methodData.EntityType == null) { return(_domainService); } RunTimeMetadata metadata = _domainService.GetMetadata(); object managerInstance = _dataManagers.GetOrAdd(methodData.EntityType, t => { return(_dataManagerContainer.GetDataManager(t)); }); return(managerInstance); }
public MetadataResult ServiceGetMetadata() { try { RunTimeMetadata metadata = GetMetadata(); MetadataResult result = new MetadataResult() { methods = metadata.MethodDescriptions }; result.associations.AddRange(metadata.Associations.Values); result.dbSets.AddRange(metadata.DbSets.Values.OrderBy(d => d.dbSetName)); return(result); } catch (Exception ex) { _OnError(ex); throw new DummyException(ex.GetFullMessage(), ex); } }
public async Task Invoke(CRUDContext <TService> ctx) { RunTimeMetadata metadata = ctx.Service.GetMetadata(); ChangeSetRequest changeSet = ctx.Request; IChangeSetGraph graph = ctx.Properties.Get <IChangeSetGraph>(CRUDContext <TService> .CHANGE_GRAPH_KEY) ?? throw new InvalidOperationException("Could not get Graph changes from properties"); if (!await ValidateRows(ctx, changeSet, metadata, graph.InsertList)) { throw new ValidationException(ErrorStrings.ERR_SVC_CHANGES_ARENOT_VALID); } if (!await ValidateRows(ctx, changeSet, metadata, graph.UpdateList)) { throw new ValidationException(ErrorStrings.ERR_SVC_CHANGES_ARENOT_VALID); } await _next(ctx); }
public async Task Invoke(RefreshContext <TService> ctx) { DbSetInfo dbSetInfo = ctx.Request.GetDbSetInfo() ?? throw new InvalidOperationException($"Could not get the DbSet for {ctx.Request.dbSetName}"); Security.IAuthorizer <TService> authorizer = ctx.ServiceContainer.GetAuthorizer(); RunTimeMetadata metadata = ctx.Service.GetMetadata(); MethodInfoData methodData = metadata.GetOperationMethodInfo(ctx.Request.dbSetName, MethodType.Refresh); if (methodData == null) { throw new InvalidOperationException(string.Format(ErrorStrings.ERR_REC_REFRESH_INVALID, dbSetInfo.GetEntityType().Name, GetType().Name)); } await authorizer.CheckUserRightsToExecute(methodData); await _next(ctx); }
public async Task Invoke(QueryContext <TService> ctx) { DbSetInfo dbSetInfo = ctx.Request.GetDbSetInfo() ?? throw new InvalidOperationException($"Could not get the DbSet for {ctx.Request.dbSetName}"); IDataHelper <TService> dataHelper = ctx.ServiceContainer.GetDataHelper(); IServiceOperationsHelper <TService> serviceHelper = ctx.ServiceContainer.GetServiceHelper(); RunTimeMetadata metadata = ctx.Service.GetMetadata(); MethodDescription method = metadata.GetQueryMethod(ctx.Request.dbSetName, ctx.Request.queryName); LinkedList <object> methParams = new LinkedList <object>(); for (int i = 0; i < method.parameters.Count; ++i) { methParams.AddLast(ctx.Request.paramInfo.GetValue(method.parameters[i].name, method, dataHelper)); } RequestContext req = QueryContext <TService> .CreateRequestContext(ctx.Service, ctx.Request); using (RequestCallContext callContext = new RequestCallContext(req)) { MethodInfoData methodData = method.GetMethodData(); object instance = serviceHelper.GetMethodOwner(methodData); object invokeRes = methodData.MethodInfo.Invoke(instance, methParams.ToArray()); QueryResult queryResult = (QueryResult)await serviceHelper.GetMethodResult(invokeRes); IEnumerable <object> entities = queryResult.Result; int? totalCount = queryResult.TotalCount; RowGenerator rowGenerator = new RowGenerator(dbSetInfo, entities, dataHelper); IEnumerable <Row> rows = rowGenerator.CreateRows(); SubsetsGenerator subsetsGenerator = new SubsetsGenerator(metadata, dataHelper); SubsetList subResults = subsetsGenerator.CreateSubsets(queryResult.subResults); ctx.Response.names = dbSetInfo.GetNames(); ctx.Response.totalCount = totalCount; ctx.Response.rows = rows; ctx.Response.subsets = subResults; ctx.Response.extraInfo = queryResult.extraInfo; } await _next(ctx); }
public static MethodInfoData GetCRUDMethodInfo(this RowInfo rowInfo, RunTimeMetadata metadata, string dbSetName) { MethodInfoData method = null; switch (rowInfo.changeType) { case ChangeType.Added: method = metadata.GetOperationMethodInfo(dbSetName, MethodType.Insert); break; case ChangeType.Deleted: method = metadata.GetOperationMethodInfo(dbSetName, MethodType.Delete); break; case ChangeType.Updated: method = metadata.GetOperationMethodInfo(dbSetName, MethodType.Update); break; default: throw new DomainServiceException(string.Format(ErrorStrings.ERR_REC_CHANGETYPE_INVALID, dbSetName, rowInfo.changeType)); } return(method); }
public ChangeSetGraph(ChangeSetRequest changeSet, RunTimeMetadata metadata) { ChangeSet = changeSet ?? throw new ArgumentNullException(nameof(changeSet)); _metadata = metadata ?? throw new ArgumentNullException(nameof(metadata)); }
public static async Task <bool> CanAccessOperation(this IAuthorizer authorizer, RunTimeMetadata metadata, string dbSetName, MethodType methodType) { MethodInfoData method = metadata.GetOperationMethodInfo(dbSetName, methodType); return(method != null && await authorizer.CanAccessMethod(method)); }
public static async Task <DbSetPermit> GetDbSetPermissions(this IAuthorizer authorizer, RunTimeMetadata metadata, string dbSetName) { return(new DbSetPermit() { dbSetName = dbSetName, canAddRow = await authorizer.CanAccessOperation(metadata, dbSetName, MethodType.Insert), canEditRow = await authorizer.CanAccessOperation(metadata, dbSetName, MethodType.Update), canDeleteRow = await authorizer.CanAccessOperation(metadata, dbSetName, MethodType.Delete), canRefreshRow = await authorizer.CanAccessOperation(metadata, dbSetName, MethodType.Refresh) }); }
private async Task <bool> ValidateRows(CRUDContext <TService> ctx, ChangeSetRequest changeSet, RunTimeMetadata metadata, IEnumerable <RowInfo> rows) { TService service = ctx.Service; IServiceOperationsHelper <TService> serviceHelper = ctx.ServiceContainer.GetServiceHelper(); foreach (RowInfo rowInfo in rows) { RequestContext req = CRUDContext <TService> .CreateRequestContext(service, changeSet, rowInfo); using (RequestCallContext callContext = new RequestCallContext(req)) { if (!await serviceHelper.ValidateEntity(metadata, req)) { rowInfo.invalid = rowInfo.GetChangeState().ValidationErrors; return(false); } } } return(true); }
public SubsetsGenerator(RunTimeMetadata metadata, IDataHelper dataHelper) { _metadata = metadata ?? throw new ArgumentNullException(nameof(metadata)); _dataHelper = dataHelper ?? throw new ArgumentNullException(nameof(dataHelper)); }
public async Task <bool> ValidateEntity(RunTimeMetadata metadata, RequestContext requestContext) { RowInfo rowInfo = requestContext.CurrentRowInfo; DbSetInfo dbSetInfo = rowInfo.GetDbSetInfo(); IEnumerable <ValidationErrorInfo> errs1 = null; IEnumerable <ValidationErrorInfo> errs2 = null; LinkedList <string> mustBeChecked = new LinkedList <string>(); LinkedList <string> skipCheckList = new LinkedList <string>(); if (rowInfo.changeType == ChangeType.Added) { foreach (ParentChildNode pn in rowInfo.GetChangeState().ParentRows) { foreach (FieldRel frel in pn.Association.fieldRels) { skipCheckList.AddLast(frel.childField); } } } foreach (Field fieldInfo in dbSetInfo.fieldInfos) { _dataHelper.ForEachFieldInfo("", fieldInfo, (fullName, f) => { if (!f.GetIsIncludeInResult()) { return; } if (f.fieldType == FieldType.Object || f.fieldType == FieldType.ServerCalculated) { return; } string value = _dataHelper.SerializeField(rowInfo.GetChangeState().Entity, fullName, f); switch (rowInfo.changeType) { case ChangeType.Added: { bool isSkip = f.isAutoGenerated || skipCheckList.Any(n => n == fullName); if (!isSkip) { _validationHelper.CheckValue(f, value); mustBeChecked.AddLast(fullName); } } break; case ChangeType.Updated: { bool isChanged = isEntityValueChanged(rowInfo, fullName, f, out string newVal); if (isChanged) { _validationHelper.CheckValue(f, newVal); mustBeChecked.AddLast(fullName); } } break; } }); } rowInfo.GetChangeState().ChangedFieldNames = mustBeChecked.ToArray(); MethodInfoData methodData = metadata.GetOperationMethodInfo(dbSetInfo.dbSetName, MethodType.Validate); if (methodData != null) { object instance = GetMethodOwner(methodData); object invokeRes = methodData.MethodInfo.Invoke(instance, new[] { rowInfo.GetChangeState().Entity, rowInfo.GetChangeState().ChangedFieldNames }); errs1 = (IEnumerable <ValidationErrorInfo>) await GetMethodResult(invokeRes); } if (errs1 == null) { errs1 = Enumerable.Empty <ValidationErrorInfo>(); } IValidator validator = _validatorsContainer.GetValidator(rowInfo.GetDbSetInfo().GetEntityType()); if (validator != null) { errs2 = await validator.ValidateModelAsync(rowInfo.GetChangeState().Entity, rowInfo.GetChangeState().ChangedFieldNames); } if (errs2 == null) { errs2 = Enumerable.Empty <ValidationErrorInfo>(); } errs1 = errs1.Concat(errs2); rowInfo.GetChangeState().ValidationErrors = errs1.ToArray(); return(rowInfo.GetChangeState().ValidationErrors.Length == 0); }