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);
            }
        }
Exemple #2
0
        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);
            }
        }
Exemple #4
0
        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 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 <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);
        }
Exemple #7
0
        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));
        }