Beispiel #1
0
 public static Catalog Create(ICatalogCreateIo input)
 {
     Debug.Assert(input.Id != null, "input.Id != null");
     return(new Catalog(input.Id.Value)
     {
         CategoryCode = input.CategoryCode,
         Code = input.Code,
         Description = input.Description,
         IsEnabled = input.IsEnabled,
         Name = input.Name,
         ParentCode = input.ParentCode,
         SortCode = input.SortCode
     });
 }
Beispiel #2
0
 public static Catalog Create(ICatalogCreateIo input)
 {
     Debug.Assert(input.Id != null, "input.Id != null");
     return new Catalog
     {
         Id = input.Id.Value,
         CategoryCode = input.CategoryCode,
         Code = input.Code,
         Description = input.Description,
         IsEnabled = input.IsEnabled,
         Name = input.Name,
         ParentCode = input.ParentCode,
         SortCode = input.SortCode
     };
 }
Beispiel #3
0
            private void Handle(IAcSession acSession, ICatalogCreateIo input, bool isCommand)
            {
                var acDomain          = _set._acDomain;
                var dicByCode         = _set._dicByCode;
                var dicById           = _set._dicById;
                var catalogRepository = acDomain.RetrieveRequiredService <IRepository <Catalog> >();

                if (!input.Id.HasValue)
                {
                    throw new ValidationException("标识是必须的");
                }
                if (string.IsNullOrEmpty(input.Code))
                {
                    throw new ValidationException("编码不能为空");
                }
                if (string.IsNullOrEmpty(input.Name))
                {
                    throw new ValidationException("名称是必须的");
                }
                Catalog entity;

                lock (MessageLocker)
                {
                    CatalogState catalog;
                    if (acDomain.CatalogSet.TryGetCatalog(input.Id.Value, out catalog))
                    {
                        throw new ValidationException("给定标识的目录已经存在");
                    }
                    if (acDomain.CatalogSet.TryGetCatalog(input.Code, out catalog))
                    {
                        throw new ValidationException("重复的目录码");
                    }
                    if (!string.IsNullOrEmpty(input.ParentCode))
                    {
                        CatalogState parentOragnization;
                        if (!acDomain.CatalogSet.TryGetCatalog(input.ParentCode, out parentOragnization))
                        {
                            throw new ValidationException("标识为" + input.ParentCode + "的父目录不存在");
                        }
                        if (string.Equals(input.Code, input.ParentCode) || !input.Code.StartsWith(parentOragnization.Code, StringComparison.OrdinalIgnoreCase))
                        {
                            throw new ValidationException("子级目录的编码必须以父级目录编码为前缀");
                        }
                        if (acDomain.CatalogSet.Any(a => string.Equals(a.ParentCode, input.ParentCode) && string.Equals(a.Name, input.Name, StringComparison.OrdinalIgnoreCase)))
                        {
                            throw new ValidationException("兄弟目录间不能重名");
                        }
                    }
                    else
                    {
                        if (acDomain.CatalogSet.Any(a => string.IsNullOrEmpty(a.ParentCode) && string.Equals(a.Name, input.Name, StringComparison.OrdinalIgnoreCase)))
                        {
                            throw new ValidationException("重复的目录名");
                        }
                    }

                    entity = Catalog.Create(input);
                    var state = CatalogState.Create(acDomain, entity);
                    if (!dicByCode.ContainsKey(entity.Code))
                    {
                        dicByCode.Add(entity.Code, state);
                    }
                    if (!dicById.ContainsKey(entity.Id))
                    {
                        dicById.Add(entity.Id, state);
                    }
                    if (isCommand)
                    {
                        try
                        {
                            catalogRepository.Add(entity);
                            catalogRepository.Context.Commit();
                        }
                        catch
                        {
                            if (dicByCode.ContainsKey(entity.Code))
                            {
                                dicByCode.Remove(entity.Code);
                            }
                            if (dicById.ContainsKey(entity.Id))
                            {
                                dicById.Remove(entity.Id);
                            }
                            catalogRepository.Context.Rollback();
                            throw;
                        }
                    }
                }
                if (isCommand)
                {
                    acDomain.MessageDispatcher.DispatchMessage(new CatalogAddedEvent(acSession, entity, input, isPrivate: true));
                }
            }