public void DeleteEntity(CachedMetadata metadata, RowInfo rowInfo)
        {
            var dbSetInfo = rowInfo.dbSetInfo;

            if (rowInfo.changeType != ChangeType.Deleted)
            {
                throw new DomainServiceException(string.Format(ErrorStrings.ERR_REC_CHANGETYPE_INVALID,
                                                               dbSetInfo.EntityType.Name, rowInfo.changeType));
            }

            var methodData = metadata.getOperationMethodInfo(dbSetInfo.dbSetName, MethodType.Delete);

            if (methodData == null)
            {
                throw new DomainServiceException(string.Format(ErrorStrings.ERR_DB_DELETE_NOT_IMPLEMENTED,
                                                               dbSetInfo.EntityType.Name, GetType().Name));
            }

            var dbEntity = Activator.CreateInstance(dbSetInfo.EntityType);

            UpdateEntityFromRowInfo(dbEntity, rowInfo, true);
            rowInfo.changeState.Entity         = dbEntity;
            rowInfo.changeState.OriginalEntity = dbEntity;
            var instance = GetMethodOwner(methodData);

            methodData.methodInfo.Invoke(instance, new[] { dbEntity });
        }
        public virtual string GetScript(string comment = null, bool isDraft = false)
        {
            CachedMetadata metadata = MetadataHelper.GetInitializedMetadata(this._owner);
            var            helper   = new TypeScriptHelper(this._owner.ServiceContainer, metadata, this._clientTypes);

            return(helper.CreateTypeScript(comment));
        }
        public void UpdateEntity(CachedMetadata metadata, RowInfo rowInfo)
        {
            var dbSetInfo = rowInfo.dbSetInfo;

            if (rowInfo.changeType != ChangeType.Updated)
            {
                throw new DomainServiceException(string.Format(ErrorStrings.ERR_REC_CHANGETYPE_INVALID,
                                                               dbSetInfo.EntityType.Name, rowInfo.changeType));
            }
            var methodData = metadata.getOperationMethodInfo(dbSetInfo.dbSetName, MethodType.Update);

            if (methodData == null)
            {
                throw new DomainServiceException(string.Format(ErrorStrings.ERR_DB_UPDATE_NOT_IMPLEMENTED,
                                                               dbSetInfo.EntityType.Name, GetType().Name));
            }
            var dbEntity = Activator.CreateInstance(dbSetInfo.EntityType);

            UpdateEntityFromRowInfo(dbEntity, rowInfo, false);
            var original = GetOriginalEntity(dbEntity, rowInfo);

            rowInfo.changeState.Entity         = dbEntity;
            rowInfo.changeState.OriginalEntity = original;
            var instance = GetMethodOwner(methodData);

            //apply this changes to entity that is in the database (this is done in user domain service method)
            methodData.methodInfo.Invoke(instance, new[] { dbEntity });
        }
Esempio n. 4
0
 public TypeScriptHelper(IServiceContainer serviceContainer, CachedMetadata metadata,
                         IEnumerable <Type> clientTypes)
 {
     if (serviceContainer == null)
     {
         throw new ArgumentException("converter parameter must not be null", "serviceContainer");
     }
     _serviceContainer = serviceContainer;
     if (metadata == null)
     {
         throw new ArgumentException("metadata parameter must not be null", "metadata");
     }
     _metadata     = metadata;
     _serializer   = _serviceContainer.Serializer;
     _clientTypes  = new List <Type>(clientTypes == null ? Enumerable.Empty <Type>() : clientTypes);
     _dbSets       = _metadata.dbSets.Values.OrderBy(v => v.dbSetName).ToList();
     _associations = _metadata.associations.Values.OrderBy(a => a.name).ToList();
 }
Esempio n. 5
0
        public static DbSetPermit GetDbSetPermissions(CachedMetadata metadata, string dbSetName, IAuthorizer authorizer)
        {
            MethodInfoData method = null;
            var            permit = new DbSetPermit();

            permit.dbSetName = dbSetName;
            method           = metadata.getOperationMethodInfo(dbSetName, MethodType.Insert);
            permit.canAddRow = method != null && CanAccessMethod(method, authorizer);

            method            = metadata.getOperationMethodInfo(dbSetName, MethodType.Update);
            permit.canEditRow = method != null && CanAccessMethod(method, authorizer);

            method = metadata.getOperationMethodInfo(dbSetName, MethodType.Delete);
            permit.canDeleteRow = method != null && CanAccessMethod(method, authorizer);

            method = metadata.getOperationMethodInfo(dbSetName, MethodType.Refresh);
            permit.canRefreshRow = method != null && CanAccessMethod(method, authorizer);
            return(permit);
        }
Esempio n. 6
0
        public static MethodInfoData GetCRUDMethodInfo(CachedMetadata metadata, string dbSetName, RowInfo rowInfo)
        {
            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);
        }
Esempio n. 7
0
        private static CachedMetadata CreateCachedMetadata(BaseDomainService domainService)
        {
            Metadata metadata = domainService.GetMetadata(false);
            CachedMetadata cachedMetadata = new CachedMetadata();
            foreach (var dbSetInfo in metadata.DbSets)
            {
                dbSetInfo.Initialize(domainService.ServiceContainer);
                //indexed by dbSetName
                cachedMetadata.dbSets.Add(dbSetInfo.dbSetName, dbSetInfo);
            }

            MetadataHelper.ProcessMethodDescriptions(domainService, cachedMetadata);
            MetadataHelper.CheckDataServiceMethods(domainService, cachedMetadata);

            foreach (var assoc in metadata.Associations)
            {
                if (string.IsNullOrWhiteSpace(assoc.name))
                {
                    throw new DomainServiceException(ErrorStrings.ERR_ASSOC_EMPTY_NAME);
                }
                if (!cachedMetadata.dbSets.ContainsKey(assoc.parentDbSetName))
                {
                    throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_PARENT, assoc.name, assoc.parentDbSetName));
                }
                if (!cachedMetadata.dbSets.ContainsKey(assoc.childDbSetName))
                {
                    throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_CHILD, assoc.name, assoc.childDbSetName));
                }
                var childDb = cachedMetadata.dbSets[assoc.childDbSetName];
                var parentDb = cachedMetadata.dbSets[assoc.parentDbSetName];
                var parentDbFields = parentDb.GetFieldByNames();
                var childDbFields = childDb.GetFieldByNames();

                //check navigation field
                //dont allow to define  it explicitly, the association adds the field by itself (implicitly)
                if (!string.IsNullOrEmpty(assoc.childToParentName) && childDbFields.ContainsKey(assoc.childToParentName))
                {
                    throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_NAV_FIELD, assoc.name, assoc.childToParentName));
                }

                //check navigation field
                //dont allow to define  it explicitly, the association adds the field by itself (implicitly)
                if (!string.IsNullOrEmpty(assoc.parentToChildrenName) && parentDbFields.ContainsKey(assoc.parentToChildrenName))
                {
                    throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_NAV_FIELD, assoc.name, assoc.parentToChildrenName));
                }

                if (!string.IsNullOrEmpty(assoc.parentToChildrenName) && !string.IsNullOrEmpty(assoc.childToParentName) && assoc.childToParentName == assoc.parentToChildrenName)
                {
                    throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_NAV_FIELD, assoc.name, assoc.parentToChildrenName));
                }

                foreach (var frel in assoc.fieldRels)
                {
                    if (!parentDbFields.ContainsKey(frel.parentField))
                    {
                        throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_PARENT_FIELD, assoc.name, frel.parentField));
                    }
                    if (!childDbFields.ContainsKey(frel.childField))
                    {
                        throw new DomainServiceException(string.Format(ErrorStrings.ERR_ASSOC_INVALID_CHILD_FIELD, assoc.name, frel.childField));
                    }
                }
                //indexed by Name
                cachedMetadata.associations.Add(assoc.name, assoc);

                if (!string.IsNullOrEmpty(assoc.childToParentName))
                {
                    var sb = new System.Text.StringBuilder(120);
                    var dependentOn = assoc.fieldRels.Aggregate(sb, (a, b) => a.Append((a.Length == 0 ? "" : ",") + b.childField), a => a).ToString();
                    //add navigation field to dbSet's field collection
                    childDb.fieldInfos.Add(new Field()
                    {
                        fieldName = assoc.childToParentName,
                        fieldType = FieldType.Navigation,
                        dataType = DataType.None,
                        dependentOn = dependentOn,
                        _TypeScriptDataType = TypeScriptHelper.GetEntityTypeName(parentDb.dbSetName)
                    });
                }

                if (!string.IsNullOrEmpty(assoc.parentToChildrenName))
                {
                    var sb = new System.Text.StringBuilder(120);
                    //add navigation field to dbSet's field collection
                    parentDb.fieldInfos.Add(new Field()
                    {
                        fieldName = assoc.parentToChildrenName,
                        fieldType = FieldType.Navigation,
                        dataType = DataType.None,
                        _TypeScriptDataType = string.Format("{0}[]", TypeScriptHelper.GetEntityTypeName(childDb.dbSetName))
                    });
                }
            } //foreach (var assoc in metadata.Associations)

            return cachedMetadata;
        }
Esempio n. 8
0
        /// <summary>
        /// Test if public methods on the service has Invoke or Query Attribute
        /// and generates from this methods their invocation method descriptions 
        /// </summary>
        /// <returns></returns>
        private static void ProcessMethodDescriptions(BaseDomainService domainService, CachedMetadata metadata)
        {
            Type thisType= domainService.GetType();
            IServiceContainer services = domainService.ServiceContainer;
            Func<MethodInfo, MethodType> fn_getMethodType = (m) =>
            {
                if (m.IsDefined(typeof(QueryAttribute), false))
                    return MethodType.Query;
                else if (m.IsDefined(typeof(InvokeAttribute), false))
                    return MethodType.Invoke;
                else if (m.IsDefined(typeof(InsertAttribute), false))
                    return MethodType.Insert;
                else if (m.IsDefined(typeof(UpdateAttribute), false))
                    return MethodType.Update;
                else if (m.IsDefined(typeof(DeleteAttribute), false))
                    return MethodType.Delete;
                else if (m.IsDefined(typeof(ValidateAttribute), false))
                    return MethodType.Validate;
                else if (m.IsDefined(typeof(RefreshAttribute), false))
                    return MethodType.Refresh;
                else
                    return MethodType.None;
            };
            var dbsetsLookUp = metadata.dbSets.Values.ToLookup(v => v.EntityType);
            var dbsetsByType = dbsetsLookUp.ToDictionary(v => v.Key);
            var methodInfos = thisType.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public);
            var allList = methodInfos.Select(m => new { method = m, methodType = fn_getMethodType(m) });
            var queryAndInvokes = allList.Where((info) => info.methodType == MethodType.Query || info.methodType == MethodType.Invoke).ToArray();
            MethodsList methodList = new MethodsList();
            Array.ForEach(queryAndInvokes, (info) =>
            {
                methodList.Add(MethodDescription.FromMethodInfo(info.method, info.methodType, services));
            });
            metadata.InitMethods(methodList);

            var otherMethods = allList.Where((info) => !(info.methodType == MethodType.Query || info.methodType == MethodType.Invoke || info.methodType == MethodType.None)).ToArray();

            Array.ForEach(otherMethods, (info) =>
            {
                Type entityType = null;
                if (info.methodType == MethodType.Refresh)
                {
                    entityType = RemoveTaskFromType(info.method.ReturnType);
                }
                else
                {
                    entityType = info.method.GetParameters().First().ParameterType;
                }

                IGrouping<Type, DbSetInfo> dbSets = null;
                if (!dbsetsByType.TryGetValue(entityType, out dbSets))
                    return;

                foreach (var dbSet in dbSets)
                {
                    switch (info.methodType)
                    {
                        case MethodType.Insert:
                            dbSet.insertDataMethod = info.method.Name;
                            break;
                        case MethodType.Update:
                            dbSet.updateDataMethod = info.method.Name;
                            break;
                        case MethodType.Delete:
                            dbSet.deleteDataMethod = info.method.Name;
                            break;
                        case MethodType.Validate:
                            dbSet.validateDataMethod = info.method.Name;
                            break;
                        case MethodType.Refresh:
                            dbSet.refreshDataMethod = info.method.Name;
                            break;
                        default:
                            throw new DomainServiceException(string.Format("Unknown Method Type: {0}", info.methodType));
                    }
                }
            });

        }
Esempio n. 9
0
        private static void CheckDataServiceMethods(BaseDomainService domainService, CachedMetadata metadata)
        {
            StringBuilder sb = new StringBuilder();

            foreach (var dbSet in metadata.dbSets.Values)
            {
                try
                {
                    MetadataHelper.CheckMethod(domainService, dbSet, MethodType.Insert);
                    MetadataHelper.CheckMethod(domainService, dbSet, MethodType.Update);
                    MetadataHelper.CheckMethod(domainService, dbSet, MethodType.Delete);
                    MetadataHelper.CheckMethod(domainService, dbSet, MethodType.Validate);
                    MetadataHelper.CheckMethod(domainService, dbSet, MethodType.Refresh);
                }
                catch (DomainServiceException ex)
                {
                    sb.AppendLine(ex.Message);
                }
            }
           
            if (sb.Length > 0)
                throw new DomainServiceException(sb.ToString());
        }
Esempio n. 10
0
 public ServiceConfig(CachedMetadata metadata)
 {
     _metadata = metadata;
 }
Esempio n. 11
0
 public ChangeSetGraph(ChangeSet changeSet, CachedMetadata metadata)
 {
     this._changeSet = changeSet;
     this._metadata = metadata;
 }
        public async Task <bool> ValidateEntity(CachedMetadata metadata, RequestContext requestContext)
        {
            var validatorsContainer = metadata.ValidatorsContainer;
            var rowInfo             = requestContext.CurrentRowInfo;
            var dbSetInfo           = rowInfo.dbSetInfo;
            IEnumerable <ValidationErrorInfo> errs1 = null;
            IEnumerable <ValidationErrorInfo> errs2 = null;
            var mustBeChecked = new LinkedList <string>();
            LinkedList <string> skipCheckList = null;
            var dataHelper       = _services.DataHelper;
            var validationHelper = _services.ValidationHelper;

            if (rowInfo.changeType == ChangeType.Added)
            {
                skipCheckList = new LinkedList <string>();
                foreach (var pn in rowInfo.changeState.ParentRows)
                {
                    foreach (var frel in pn.association.fieldRels)
                    {
                        skipCheckList.AddLast(frel.childField);
                    }
                }
            }

            foreach (var fieldInfo in dbSetInfo.fieldInfos)
            {
                dataHelper.ForEachFieldInfo("", fieldInfo, (fullName, f) =>
                {
                    if (!f.GetIsIncludeInResult())
                    {
                        return;
                    }
                    if (f.fieldType == FieldType.Object || f.fieldType == FieldType.ServerCalculated)
                    {
                        return;
                    }

                    var value = dataHelper.SerializeField(rowInfo.changeState.Entity, fullName, f);
                    if (rowInfo.changeType == ChangeType.Added)
                    {
                        var isSkip = f.isAutoGenerated ||
                                     (skipCheckList != null && skipCheckList.Any(n => n == fullName));
                        if (!isSkip)
                        {
                            validationHelper.CheckValue(f, value);
                            mustBeChecked.AddLast(fullName);
                        }
                    }
                    else if (rowInfo.changeType == ChangeType.Updated)
                    {
                        string newVal;
                        var isChanged = isEntityValueChanged(rowInfo, fullName, f, out newVal);
                        if (isChanged)
                        {
                            validationHelper.CheckValue(f, newVal);
                        }
                        if (isChanged)
                        {
                            mustBeChecked.AddLast(fullName);
                        }
                    }
                });
            }

            rowInfo.changeState.NamesOfChangedFields = mustBeChecked.ToArray();
            var methodData = metadata.getOperationMethodInfo(dbSetInfo.dbSetName, MethodType.Validate);

            if (methodData != null)
            {
                var instance  = GetMethodOwner(methodData);
                var invokeRes = methodData.methodInfo.Invoke(instance,
                                                             new[] { rowInfo.changeState.Entity, rowInfo.changeState.NamesOfChangedFields });
                errs1 = (IEnumerable <ValidationErrorInfo>) await GetMethodResult(invokeRes).ConfigureAwait(false);
            }

            if (errs1 == null)
            {
                errs1 = Enumerable.Empty <ValidationErrorInfo>();
            }

            var validator = validatorsContainer.GetValidator(requestContext, rowInfo.dbSetInfo.EntityType);

            if (validator != null)
            {
                errs2 =
                    await
                    validator.ValidateModelAsync(rowInfo.changeState.Entity,
                                                 rowInfo.changeState.NamesOfChangedFields);
            }

            if (errs2 == null)
            {
                errs2 = Enumerable.Empty <ValidationErrorInfo>();
            }

            errs1 = errs1.Concat(errs2);

            if (errs1 != null && errs1.Count() > 0)
            {
                rowInfo.changeState.ValidationErrors = errs1.ToArray();
                return(false);
            }

            return(true);
        }
Esempio n. 13
0
 public ChangeSetGraph(ChangeSet changeSet, CachedMetadata metadata)
 {
     this.changeSet = changeSet;
     _metadata      = metadata;
 }