示例#1
0
        private bool DeleteCore(IEnumerable <Domain.Attribute> deleteds, Func <Domain.Attribute, bool> validation)
        {
            Guard.NotEmpty(deleteds, nameof(deleteds));
            var result = true;

            if (validation != null)
            {
                foreach (var deleted in deleteds)
                {
                    result = validation?.Invoke(deleted) ?? true;
                }
            }
            if (result)
            {
                var ids = deleteds.Select(x => x.AttributeId).ToArray();
                using (UnitOfWork.Build(_attributeRepository.DbContext))
                {
                    foreach (var deleted in deleteds)
                    {
                        result = _attributeRepository.Delete(deleted);
                        //删除依赖项
                        _dependencyService.Delete(deleted.AttributeId);
                        //remove from cache
                        _cacheService.RemoveEntity(deleted);
                        //localization
                        _localizedLabelService.DeleteByObject(deleted.AttributeId);
                        _eventPublisher.Publish(new ObjectDeletedEvent <Domain.Attribute>(AttributeDefaults.ModuleName, deleted));
                    }
                    //cascade delete
                    _cascadeDeletes?.ToList().ForEach((x) => { x.CascadeDelete(deleteds.ToArray()); });
                    //update db view
                    _metadataService.AlterView(_entityFinder.FindById(deleteds.First().EntityId));
                }
            }
            return(result);
        }
示例#2
0
        public bool Create(Domain.Attribute entity)
        {
            //检查名称是否已存在
            if (_attributeRepository.Exists(x => x.EntityId == entity.EntityId && x.Name == entity.Name))
            {
                throw new XmsException(_loc["ATTRIBUTE_NAME_EXISTS"]);
            }
            var result = true;

            if (entity.DefaultValue.IsEmpty() && (entity.TypeIsBit() || entity.TypeIsDecimal() || entity.TypeIsFloat() ||
                                                  entity.TypeIsInt() || entity.TypeIsMoney() || entity.TypeIsSmallInt() || entity.TypeIsSmallMoney() || entity.TypeIsState() || entity.TypeIsStatus()))
            {
                entity.DefaultValue = "0";
            }
            var parentEntity = _entityFinder.FindById(entity.EntityId);

            using (UnitOfWork.Build(_attributeRepository.DbContext))
            {
                result = _attributeRepository.Create(entity);
                if (result)
                {
                    //引用类型
                    if (entity.TypeIsLookUp() || entity.TypeIsOwner() || entity.TypeIsCustomer())
                    {
                        var referencing  = _entityFinder.FindById(entity.EntityId);
                        var referenced   = _attributeRepository.Find(x => x.EntityId == entity.ReferencedEntityId.Value && x.AttributeTypeName == AttributeTypeIds.PRIMARYKEY);
                        var relationShip = new Domain.RelationShip
                        {
                            Name                   = "lk_" + referencing.Name + "_" + entity.Name,
                            RelationshipType       = RelationShipType.ManyToOne,
                            ReferencingAttributeId = entity.AttributeId,
                            ReferencingEntityId    = entity.EntityId,
                            ReferencedAttributeId  = referenced.AttributeId,
                            ReferencedEntityId     = referenced.EntityId,
                            CascadeLinkMask        = parentEntity.ParentEntityId.HasValue ? 1 : 2,
                            CascadeAssign          = parentEntity.ParentEntityId.HasValue ? 1 : 4,
                            CascadeDelete          = parentEntity.ParentEntityId.HasValue ? 1 : 4,
                            CascadeShare           = parentEntity.ParentEntityId.HasValue ? 1 : 4,
                            CascadeUnShare         = parentEntity.ParentEntityId.HasValue ? 1 : 4,
                            IsCustomizable         = !parentEntity.ParentEntityId.HasValue
                        };
                        _relationShipCreater.Create(relationShip);
                    }
                    //选项类型
                    else if (entity.TypeIsPickList())
                    {
                        if (entity.OptionSet != null && !entity.OptionSet.IsPublic)
                        {
                            _optionSetCreater.Create(entity.OptionSet);
                        }
                    }
                    //bit类型
                    else if (entity.TypeIsBit())
                    {
                        _stringMapCreater.CreateMany(entity.PickLists);
                    }
                    //update db view
                    _metadataService.AlterView(_entityFinder.FindById(entity.EntityId));
                    //依赖项
                    _dependencyService.Create(entity);
                    //本地化标签
                    _localizedLabelService.Append(SolutionDefaults.DefaultSolutionId, entity.LocalizedName.IfEmpty(""), AttributeDefaults.ModuleName, "LocalizedName", entity.AttributeId)
                    .Append(SolutionDefaults.DefaultSolutionId, entity.Description.IfEmpty(""), AttributeDefaults.ModuleName, "Description", entity.AttributeId)
                    .Save();

                    //add to cache
                    _cacheService.SetEntity(entity);
                }
            }
            return(result);
        }