Esempio n. 1
0
        public ComponentType GetOrCreateComponentType(
            Guid accountId,
            GetOrCreateComponentTypeRequestData data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (data.DisplayName == null)
            {
                data.DisplayName = data.SystemName;
            }
            if (data.SystemName == null)
            {
                data.SystemName = data.DisplayName;
            }
            if (string.IsNullOrEmpty(data.SystemName))
            {
                throw new UserFriendlyException("Не задано системное имя типа компонента");
            }

            var context    = Context.GetAccountDbContext(accountId);
            var repository = context.GetComponentTypeRepository();

            var lockObject = LockObject.ForAccount(accountId);

            lock (lockObject)
            {
                var result = repository.GetOneOrNullBySystemName(data.SystemName);
                if (result == null)
                {
                    // Проверим лимит
                    var checker          = AccountLimitsCheckerManager.GetCheckerForAccount(accountId);
                    var limitCheckResult = checker.CheckMaxComponentTypes(context);
                    if (!limitCheckResult.Success)
                    {
                        throw new OverLimitException(limitCheckResult.Message);
                    }

                    result = repository.Add(data.DisplayName, data.SystemName);

                    checker.RefreshComponentTypesCount();
                }
                return(result);
            }
        }