public Attempt <OperationResult <OperationResultType, EntityContainer> > CreateContainer(int parentId, Guid key, string name, int userId = Cms.Core.Constants.Security.SuperUserId)
        {
            EventMessages eventMessages = EventMessagesFactory.Get();

            using (IScope scope = ScopeProvider.CreateScope())
            {
                scope.WriteLock(WriteLockIds); // also for containers

                try
                {
                    var container = new EntityContainer(ContainedObjectType)
                    {
                        Name      = name,
                        ParentId  = parentId,
                        CreatorId = userId,
                        Key       = key
                    };

                    var savingNotification = new EntityContainerSavingNotification(container, eventMessages);
                    if (scope.Notifications.PublishCancelable(savingNotification))
                    {
                        scope.Complete();
                        return(OperationResult.Attempt.Cancel(eventMessages, container));
                    }

                    _containerRepository.Save(container);
                    scope.Complete();

                    var savedNotification = new EntityContainerSavedNotification(container, eventMessages);
                    savedNotification.WithStateFrom(savingNotification);
                    scope.Notifications.Publish(savedNotification);
                    // TODO: Audit trail ?

                    return(OperationResult.Attempt.Succeed(eventMessages, container));
                }
                catch (Exception ex)
                {
                    scope.Complete();
                    return(OperationResult.Attempt.Fail <OperationResultType, EntityContainer>(OperationResultType.FailedCancelledByEvent, eventMessages, ex));
                }
            }
        }
        public Attempt <OperationResult <OperationResultType, EntityContainer> > CreateContainer(int parentId, string name, int userId = Constants.Security.SuperUserId)
        {
            var evtMsgs = EventMessagesFactory.Get();

            using (var scope = ScopeProvider.CreateScope())
            {
                scope.WriteLock(WriteLockIds); // also for containers

                try
                {
                    var container = new EntityContainer(ContainedObjectType)
                    {
                        Name      = name,
                        ParentId  = parentId,
                        CreatorId = userId
                    };

                    var saveEventArgs = new SaveEventArgs <EntityContainer>(container, evtMsgs);
                    if (OnSavingContainerCancelled(scope, saveEventArgs))
                    {
                        scope.Complete();
                        return(OperationResult.Attempt.Cancel(evtMsgs, container));
                    }

                    _containerRepository.Save(container);
                    scope.Complete();

                    saveEventArgs.CanCancel = false;
                    OnSavedContainer(scope, saveEventArgs);
                    // TODO: Audit trail ?

                    return(OperationResult.Attempt.Succeed(evtMsgs, container));
                }
                catch (Exception ex)
                {
                    scope.Complete();
                    return(OperationResult.Attempt.Fail <OperationResultType, EntityContainer>(OperationResultType.FailedCancelledByEvent, evtMsgs, ex));
                }
            }
        }