public void MultiThreads_Sync_UowCreation()
            {
                IPersistenceUnitOfWork uow1 = null, uow2 = null;

                Thread t = new Thread(new ThreadStart(() => {
                    using (uow2 = Subject.Create())
                    {
                        Thread.Sleep(500);
                        uow2.Complete();
                    }
                }));

                using (uow1 = Subject.Create())
                {
                    using (Subject.SuppressAmbientScope())
                    {
                        t.Start();
                    }
                    uow1.Complete();
                }

                t.Join();

                // assert
                IPersistenceContext context1 = uow1.Context;
                IPersistenceContext context2 = uow2.Context;

                Assert.AreNotSame(uow2, uow1);
                Assert.AreNotSame(context2, context1);

                (context1 as IPersistenceContextExplicit).Received(1).Flush();
                (context2 as IPersistenceContextExplicit).Received(1).Flush();
            }
Exemple #2
0
 public CategoryCommandHandler(IPersistenceUnitOfWork persistenceUnitOfWork,
                               IMapper mapper,
                               ILogger <CategoryCommandHandler> logger)
 {
     _persistenceUnitOfWork = persistenceUnitOfWork;
     _mapper = mapper;
     _logger = logger;
 }
Exemple #3
0
 private void Validate(UpdateUserCommand command, IPersistenceUnitOfWork uow)
 {
     // user should have at least one role
     if (command.RoleIds == null || !command.RoleIds.Any())
     {
         throw new ValidationException("No role has been assigned to User");
     }
 }
Exemple #4
0
            public async void SingleThread_ASync_MultipleUowsWithTransactionCreation()
            {
                IPersistenceUnitOfWork uow1;

                Task t1, t3;

                using (uow1 = Subject.CreateWithTransaction(IsolationLevel.ReadCommitted))
                {
                    t1 = uow1.CompleteAsync();
                    await t1;
                }

                IPersistenceUnitOfWork uow2 = null;

                try
                {
                    using (uow2 = Subject.CreateWithTransaction(IsolationLevel.ReadCommitted))
                    {
                        ThrowsException();
                    }
                }
                catch (ApplicationException)
                {
                    // exception handled
                }

                IPersistenceUnitOfWork uow3;

                using (uow3 = Subject.CreateWithTransaction(IsolationLevel.ReadCommitted))
                {
                    t3 = uow3.CompleteAsync();
                    await t3;
                }

                t1.Wait();
                t3.Wait();

                // assert
                IPersistenceContext context1 = uow1.Context;
                IPersistenceContext context2 = uow2.Context;
                IPersistenceContext context3 = uow3.Context;

                Assert.AreNotSame(uow2, uow1);
                Assert.AreNotSame(uow3, uow1);
                Assert.AreNotSame(uow3, uow2);
                Assert.AreNotSame(context2, context1);
                Assert.AreNotSame(context3, context1);
                Assert.AreNotSame(context3, context2);

                (context1 as IPersistenceContextExplicit).Received(1).Flush();
                Assert.IsFalse(context1.IsInActiveTransaction);

                (context2 as IPersistenceContextExplicit).Received(0).Flush();
                Assert.IsFalse(context2.IsInActiveTransaction);

                (context3 as IPersistenceContextExplicit).Received(1).Flush();
                Assert.IsFalse(context3.IsInActiveTransaction);
            }
 private void Validate(IPersistenceUnitOfWork uow, Entities.Security.Role roleToUpdate, UpdateRoleCommand command)
 {
     if (roleToUpdate.IsSystemRole && command.IncludeFuctions)
     {
         if (!string.Equals((roleToUpdate.Name ?? "").Trim(), (command.Name ?? "").Trim(), StringComparison.OrdinalIgnoreCase))
         {
             throw new ValidationException("A system role permissions cannot be changed.");
         }
     }
 }
Exemple #6
0
        private void Validate(AddRoleCommand command, IPersistenceUnitOfWork uow)
        {
            // check duplicate name
            string newRoleName = command.Name;

            if (uow.Context.Query <Entities.Security.Role>().Any(s => s.Name == newRoleName))
            {
                throw new ValidationException($"Role with '{newRoleName}' name already exist");
            }
        }
Exemple #7
0
        private void Validate(AddZoneCommand command, IPersistenceUnitOfWork uow)
        {
            // check duplicate name
            var      siteId      = command.SiteId;
            PageName newZoneName = command.Title;

            if (uow.Context.Query <Entities.Sites.Zone>().Any(s => s.Name == newZoneName && s.Site.Id == siteId))
            {
                throw new ValidationException($"Zone with '{newZoneName}' name already exist");
            }
        }
Exemple #8
0
 private List <Setting> GetDatabaseSettings(IPersistenceUnitOfWork uow, long?siteId)
 {
     if (siteId == null)
     {
         return(uow.Context.Query <Setting>().Where(s => s.Site == null).ToList());
     }
     else
     {
         long siteIdValue = siteId.Value;
         return(uow.Context.Query <Setting>().Where(s => s.Site.Id == siteIdValue).ToList());
     }
 }
Exemple #9
0
        private void Validate(IPersistenceUnitOfWork uow, Entities.Sites.Zone zoneToDelete)
        {
            long zoneId = zoneToDelete.Id;

            bool hasPosts = uow.Context
                            .Query <Entities.Posts.Post>()
                            .Any(e => e.Zone.Id == zoneId);

            if (hasPosts)
            {
                throw new ValidationException("Zone has posts, please delete them first and try again.");
            }
        }
        private void Validate(Entities.Posts.Category categoryToDelete, IPersistenceUnitOfWork uow)
        {
            var categoryId = categoryToDelete.Id;

            // should check the category is used in posts or not?
            var categoryUsed = uow.Context.Query <Entities.Posts.Post>()
                               .SelectMany(p => p.Tags)
                               .Any(t => t.Id == categoryId);

            if (categoryUsed)
            {
                throw new ValidationException($"Category '{categoryToDelete.Name}' is used in post entries");
            }
        }
            public void MultiThreads_Sync_InnerUowCreation()
            {
                IPersistenceUnitOfWork uow, innerUow, tUow = null, tInnerUow = null;

                Thread t = new Thread(new ThreadStart(() => {
                    using (tUow = Subject.Create())
                    {
                        using (tInnerUow = Subject.Create())
                        {
                            Thread.Sleep(500);
                            tInnerUow.Complete();
                        }
                        tUow.Complete();
                    }
                }));


                using (uow = Subject.Create())
                {
                    using (innerUow = Subject.Create())
                    {
                        using (Subject.SuppressAmbientScope())
                        {
                            t.Start();
                        }
                        innerUow.Complete();
                    }
                    uow.Complete();
                }

                t.Join();

                // assert
                IPersistenceContext context       = uow.Context;
                IPersistenceContext childContext  = innerUow.Context;
                IPersistenceContext tContext      = tUow.Context;
                IPersistenceContext tChildContext = tInnerUow.Context;


                Assert.AreNotSame(uow, innerUow);
                Assert.AreSame(context, childContext);
                Assert.AreNotSame(uow, tUow);

                Assert.AreNotSame(tUow, tInnerUow);
                Assert.AreSame(tContext, tChildContext);
                Assert.AreNotSame(context, tContext);

                (context as IPersistenceContextExplicit).Received(1).Flush();
                (tContext as IPersistenceContextExplicit).Received(1).Flush();
            }
Exemple #12
0
        public void SaveSettings <T>(IPersistenceUnitOfWork uow, T settingsToSave) where T : ISettings
        {
            var  settingsMetadata = ReadSettingMetadata <T>();
            var  databaseSettings = GetDatabaseSettings(uow, settingsToSave.SiteId);
            Site site             = null;

            if (settingsToSave.SiteId != null)
            {
                site = uow.Context.FindById <Site>(settingsToSave.SiteId.Value);
            }

            foreach (var setting in settingsMetadata)
            {
                // Write over it using the stored value
                var value     = setting.Read(settingsToSave);
                var dbSetting = databaseSettings.FirstOrDefault(x => x.Name == setting.Storage.Key);
                if (dbSetting != null)
                {
                    dbSetting.Value = value ?? setting.DefaultValue as string ?? string.Empty;
                    uow.Context.Update(dbSetting);
                }
                else
                {
                    var newSettings = new Setting
                    {
                        Section     = "",
                        Name        = setting.Storage.Key,
                        Title       = setting.DisplayName,
                        Description = setting.Description,
                        Value       = value ?? setting.DefaultValue as string ?? string.Empty,
                        Site        = site
                    };
                    databaseSettings.Add(newSettings);
                    uow.Context.Add(newSettings);
                }
            }

            var key = new KeyValuePair <Type, long?>(typeof(T), settingsToSave.SiteId);

            if (_settingsStore.ContainsKey(key))
            {
                _settingsStore[key] = settingsToSave;
            }
            else
            {
                _settingsStore.Add(key, settingsToSave);
            }
        }
        private void Validate(NewPostCommand command, IPersistenceUnitOfWork uow)
        {
            // check duplicate name
            string newTitle = command.Title;

            if (uow.Context.Query <Entities.Posts.Post>().Any(p => p.Title == newTitle))
            {
                throw new ValidationException($"Post with '{newTitle}' title already exist");
            }
            string newName = command.Name;

            if (uow.Context.Query <Entities.Posts.Post>().Any(p => p.Name == newName))
            {
                throw new ValidationException($"Post with '{newName}' url already exist");
            }
        }
Exemple #14
0
        private void Validate(IPersistenceUnitOfWork uow, Entities.Security.Role roleToDelete)
        {
            if (roleToDelete.IsSystemRole)
            {
                throw new ValidationException("A system role cannot be deleted.");
            }
            long roleId   = roleToDelete.Id;
            bool hasUsers = uow.Context
                            .Query <Entities.Security.User>()
                            .Any(u => u.Roles.Any(r => r.Id == roleId));

            if (hasUsers)
            {
                throw new ValidationException("Role is assigned to users and cannot be deleted.");
            }
        }
Exemple #15
0
        private void Validate(SavePostCommand command, IPersistenceUnitOfWork uow)
        {
            // check duplicate name
            var    postId   = command.PostId;
            string newTitle = command.Title;

            if (uow.Context.Query <Entities.Posts.Post>().Any(p => p.Title == newTitle && p.Id != postId))
            {
                throw new ValidationException($"Another post with '{newTitle}' title already exist");
            }
            string newName = command.Name;

            if (uow.Context.Query <Entities.Posts.Post>().Any(p => p.Name == newName && p.Id != postId))
            {
                throw new ValidationException($"Another post with '{newName}' url already exist");
            }
        }
        private void Validate(AddSiteCommand command, IPersistenceUnitOfWork uow)
        {
            // check duplicate name
            var newSiteTitle = command.SiteTitle;

            if (uow.Context.Query <Entities.Sites.Site>().Any(s => s.Title == newSiteTitle))
            {
                throw new ValidationException($"Site with '{newSiteTitle}' title already exist");
            }

            PageName newSiteUrl = command.Name;

            if (uow.Context.Query <Entities.Sites.Site>().Any(s => s.Name == newSiteUrl))
            {
                throw new ValidationException($"Site with '{newSiteUrl}' url already exist");
            }
        }
Exemple #17
0
        private void Validate(UpdateSiteCommand command, IPersistenceUnitOfWork uow)
        {
            // check duplicate name
            long siteId    = command.SiteId;
            var  siteTitle = command.SiteTitle;

            if (uow.Context.Query <Entities.Sites.Site>().Any(s => s.Title == siteTitle && s.Id != siteId))
            {
                throw new ValidationException($"Site with '{siteTitle}' title already exist");
            }

            PageName siteName = command.Name;

            if (uow.Context.Query <Entities.Sites.Site>().Any(s => s.Name == siteName && s.Id != siteId))
            {
                throw new ValidationException($"Site with '{siteName}' url already exist");
            }
        }
            public void SingleThread_Sync_InnerUowsWithTransactionCreation_2()
            {
                IPersistenceUnitOfWork uow, innerUow, innerInnerUow = null;
                using (uow = Subject.CreateWithTransaction(IsolationLevel.ReadCommitted))
                {
                    using (innerUow = Subject.Create())
                    {
                        try
                        {
                            using (innerInnerUow = Subject.CreateWithTransaction(IsolationLevel.RepeatableRead))
                            {
                                ThrowsException();
                            }
                        }
                        catch (ApplicationException)
                        {
                            // exception handled
                        }

                        innerUow.Complete();
                    }
                    uow.Complete();
                }

                // assert
                IPersistenceContext context = uow.Context;
                IPersistenceContext childContext = innerUow.Context;
                IPersistenceContext grandChildContext = innerInnerUow.Context;

                Assert.AreNotSame(uow, innerUow);
                Assert.AreNotSame(uow, innerInnerUow);
                Assert.AreNotSame(innerUow, innerInnerUow);

                Assert.AreSame(context, childContext);
                Assert.AreNotSame(context, grandChildContext);

                (context as IPersistenceContextExplicit).Received(1).Flush();
                Assert.IsFalse(context.IsInActiveTransaction);

                (grandChildContext as IPersistenceContextExplicit).Received(0).Flush();
                Assert.IsFalse(grandChildContext.IsInActiveTransaction);
            }
        private void Validate(AddUserCommand command, IPersistenceUnitOfWork uow)
        {
            // user should have at least one role
            if (command.RoleIds == null || !command.RoleIds.Any())
            {
                throw new ValidationException("No role has been assigned to User");
            }

            // user with the same email should not exist
            if (uow.Context.Query <Entities.Security.User>().Any(s => s.Email == command.Email || s.UserName == command.Email))
            {
                throw new ValidationException($"User with same email: '{command.Email}' already exist");
            }

            // user with the same username should not exist
            if (uow.Context.Query <Entities.Security.User>().Any(s => s.UserName == command.UserName || s.Email == command.UserName))
            {
                throw new ValidationException($"User with same username: '******' already exist");
            }
        }
            public void SingleThread_Sync_UowWithRollbackTransactionCreation()
            {
                IPersistenceUnitOfWork uow = null;

                try
                {
                    using (uow = Subject.CreateWithTransaction(IsolationLevel.ReadCommitted))
                    {
                        ThrowsException();
                    }
                }
                catch (ApplicationException)
                {
                    // exception handled
                }

                // assert
                IPersistenceContext context = uow.Context;
                (context as IPersistenceContextExplicit).Received(0).Flush();
                Assert.IsFalse(context.IsInActiveTransaction);
            }
Exemple #21
0
        private void Validate(AddCategoryCommand command, IPersistenceUnitOfWork uow)
        {
            var siteId = command.SiteId;

            if (siteId == 0)
            {
                throw new ValidationException("SiteId is not valid");
            }

            if (!uow.Context.Query <Entities.Sites.Site>().Any(s => s.Id == siteId))
            {
                throw new ValidationException($"Site with id:{siteId} not found");
            }

            string newCategoryName = command.Name;

            if (uow.Context.Query <Entities.Posts.Category>().Any(s => s.Name == newCategoryName))
            {
                throw new ValidationException($"Category with '{newCategoryName}' name already exist");
            }
        }
        private void Validate(IPersistenceUnitOfWork uow, Entities.Security.User userToDelete)
        {
            // user has assigned to site
            var userId = userToDelete.Id;
            var site   = uow.Context.Query <Entities.Sites.Site>().FirstOrDefault(s => s.Users.Any(u => u.Id == userId));

            if (site != null)
            {
                throw new ValidationException($"User is assigned to site: {site.Name}");
            }

            // user is creator of post
            if (uow.Context.Query <Entities.Posts.Post>().Any(p => p.Creator.Id == userId))
            {
                throw new ValidationException("User has associated post entry, please delete all user's posts first and try again");
            }

            // user is owner of job
            if (uow.Context.Query <Entities.Jobs.Job>().Any(j => j.Owner.Id == userId))
            {
                throw new ValidationException($"User has jobs, please delete all user's jobs first and try again.");
            }
        }
Exemple #23
0
 private void Validate(UpdateCategoryCommand command, IPersistenceUnitOfWork uow)
 {
     // empty
 }
Exemple #24
0
 private void Validate(IPersistenceUnitOfWork uow, UpdatePostSerieCommand command)
 {
     // TODO:
 }
 public CategoryQueryHandler(IMapper mapper, IPersistenceUnitOfWork persistenceUnitOfWork)
 {
     _mapper = mapper;
     _persistenceUnitOfWork = persistenceUnitOfWork;
 }
 private void Validate(IPersistenceUnitOfWork uow, DeleteJobCommand command)
 {
     // TODO:
 }
 public PostCommandHandler(ILogger <PostCommandHandler> logger, IMapper mapper, IPersistenceUnitOfWork persistenceUnitOfWork)
 {
     _logger = logger;
     _mapper = mapper;
     _persistenceUnitOfWork = persistenceUnitOfWork;
 }