예제 #1
0
        /// <summary>
        /// Creates a new element.
        /// </summary>
        /// <param name="settings">New element settings.</param>
        /// <param name="unitOfWork">Unit of work.</param>
        /// <returns>Newly allocated element identifier.</returns>
        public long Create(IElementSettings settings, IUnitOfWork unitOfWork = null)
        {
            try
            {
                IAdvancedElementService customElementService   = (IAdvancedElementService)_elementFactory.GetElementService(settings.ElementTypeId);
                ICustomElementValidator customElementValidator = _elementFactory.GetElementValidator(settings.ElementTypeId);
                IUnitOfWork             localUnitOfWork        = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null;

                try
                {
                    if (customElementValidator != null)
                    {
                        customElementValidator.ValidateCreate(settings, unitOfWork ?? localUnitOfWork);
                    }
                    settings.ElementId = _elementRepository.Create(settings, unitOfWork ?? localUnitOfWork);
                    if (customElementService != null)
                    {
                        customElementService.Create(settings, unitOfWork ?? localUnitOfWork);
                    }
                    if (localUnitOfWork != null)
                    {
                        localUnitOfWork.Commit();
                    }
                    return(settings.ElementId);
                }
                catch
                {
                    if (localUnitOfWork != null)
                    {
                        localUnitOfWork.Rollback();
                    }
                    throw;
                }
                finally
                {
                    if (localUnitOfWork != null)
                    {
                        localUnitOfWork.Dispose();
                    }
                }
            }
            catch (ValidationErrorException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new ValidationErrorException(new ValidationError(null, ApplicationResource.UnexpectedErrorMessage), ex);
            }
        }