diff --git a/WatchList.Core/Model/Load/ILoadRulesConfig.cs b/WatchList.Core/Model/Load/ILoadRulesConfig.cs new file mode 100644 index 00000000..4bbf9ebc --- /dev/null +++ b/WatchList.Core/Model/Load/ILoadRulesConfig.cs @@ -0,0 +1,15 @@ +using WatchList.Core.Model.ItemCinema.Components; + +namespace WatchList.Core.Model.Load +{ + public interface ILoadRulesConfig + { + bool DeleteGrade { get; } + + ActionDuplicateItems ActionsWithDuplicates { get; } + + TypeCinema TypeCinemaLoad { get; } + + Grade MoreGrade { get; } + } +} diff --git a/WatchList.Core/Repository/Extension/DuplicateIdExtension.cs b/WatchList.Core/Repository/Extension/DuplicateIdExtension.cs new file mode 100644 index 00000000..849d3eb6 --- /dev/null +++ b/WatchList.Core/Repository/Extension/DuplicateIdExtension.cs @@ -0,0 +1,14 @@ +using WatchList.Core.Model.ItemCinema; +using WatchList.Core.Repository.Db; + +namespace WatchList.Core.Repository.Extension +{ + public static class DuplicateIdExtension + { + public static Guid ReplaceIdIsNotFree(this WatchCinemaDbContext dbContext, WatchItem item) + { + var idDuplicate = dbContext.WatchItem.Where(x => x.Id == item.Id).Take(2).Select(x => x.Id).ToList(); + return idDuplicate.Count != 0 ? Guid.NewGuid() : item.Id; + } + } +} diff --git a/WatchList.Core/Repository/WatchItemRepository.cs b/WatchList.Core/Repository/WatchItemRepository.cs index 99e6d660..edb59e8e 100644 --- a/WatchList.Core/Repository/WatchItemRepository.cs +++ b/WatchList.Core/Repository/WatchItemRepository.cs @@ -21,6 +21,7 @@ public PagedList GetPage(WatchItemSearchRequest searchRequest) public void Add(WatchItem item) { + item.Id = _db.ReplaceIdIsNotFree(item); _db.Add(item); _db.SaveChanges(); } @@ -52,5 +53,15 @@ public void Update(WatchItem editItem) _db.SaveChanges(); } + + public List SelectDuplicateItems(WatchItem item) => + _db.WatchItem.Where(x => + (x.Title == item.Title && x.Sequel == item.Sequel && x.Type == item.Type)). + Take(2).Select(x => x.Id).ToList(); + + public List DuplicateItemsCaseSensitive(WatchItem item) => + _db.WatchItem.ToList(). + Where(x => x.TitleNormalized == item.TitleNormalized && x.Sequel == item.Sequel && x.Type == item.Type). + Take(2).Select(x => x.Id).ToList(); } } diff --git a/WatchList.Core/Service/DataLoading/DownloadDataService.cs b/WatchList.Core/Service/DataLoading/DownloadDataService.cs index 6584865e..efff5da2 100644 --- a/WatchList.Core/Service/DataLoading/DownloadDataService.cs +++ b/WatchList.Core/Service/DataLoading/DownloadDataService.cs @@ -3,30 +3,25 @@ using WatchList.Core.Model.Sorting; using WatchList.Core.PageItem; using WatchList.Core.Repository; -using WatchList.Core.Repository.Db; using WatchList.Core.Service.Component; using WatchList.Core.Service.DataLoading.Rules; -using WatchList.Core.Service.Extension; namespace WatchList.Core.Service.DataLoading { public class DownloadDataService { - private readonly WatchCinemaDbContext _db; - private readonly WatchItemRepository _repository; private readonly IMessageBox _messageBox; - public DownloadDataService(WatchCinemaDbContext db, IMessageBox messageBox) + public DownloadDataService(WatchItemRepository repository, IMessageBox messageBox, int numberOfItemPerPage = 500) { - _db = db; - db.ChangeTracker.Clear(); - _repository = new WatchItemRepository(_db); + _repository = repository; _messageBox = messageBox; + NumberOfItemPerPage = numberOfItemPerPage; } - public int NumberOfItemPerPage { get; set; } = 500; + public int NumberOfItemPerPage { get; set; } public void Download(WatchItemRepository repository, ILoadRule loadRule) { @@ -55,7 +50,6 @@ private void AddItems(WatchItemCollection itemCollection) foreach (var item in itemCollection.NewItems) { - item.Id = _db.ReplaceIdIsNotFree(item); _repository.Add(item); } } diff --git a/WatchList.Core/Service/DataLoading/Rules/DeleteGradeRule.cs b/WatchList.Core/Service/DataLoading/Rules/DeleteGradeRule.cs index 454ec408..caf306ca 100644 --- a/WatchList.Core/Service/DataLoading/Rules/DeleteGradeRule.cs +++ b/WatchList.Core/Service/DataLoading/Rules/DeleteGradeRule.cs @@ -1,10 +1,11 @@ using WatchList.Core.Model.ItemCinema.Components; +using WatchList.Core.Model.Load; namespace WatchList.Core.Service.DataLoading.Rules { public class DeleteGradeRule : ILoadRule { - public DeleteGradeRule(bool isDeleteGrade) => IsDeleteGrade = isDeleteGrade; + public DeleteGradeRule(ILoadRulesConfig config) => IsDeleteGrade = config.DeleteGrade; public bool IsDeleteGrade { get; private set; } diff --git a/WatchList.Core/Service/DataLoading/Rules/DuplicateLoadRule.cs b/WatchList.Core/Service/DataLoading/Rules/DuplicateLoadRule.cs index 437165cf..76fd0d8c 100644 --- a/WatchList.Core/Service/DataLoading/Rules/DuplicateLoadRule.cs +++ b/WatchList.Core/Service/DataLoading/Rules/DuplicateLoadRule.cs @@ -1,6 +1,5 @@ using WatchList.Core.Model.Load; -using WatchList.Core.Repository.Db; -using WatchList.Core.Service.Extension; +using WatchList.Core.Repository; namespace WatchList.Core.Service.DataLoading.Rules { @@ -12,11 +11,13 @@ public class DuplicateLoadRule : ILoadRule private readonly bool _caseSensitive; - private readonly WatchCinemaDbContext _dbContext; + private readonly WatchItemRepository _itemRepository; - public DuplicateLoadRule(WatchCinemaDbContext db, ActionDuplicateItems actionsWithDuplicates) + public DuplicateLoadRule(WatchItemRepository itemRepository, ILoadRulesConfig config) { - _dbContext = db; + var actionsWithDuplicates = config.ActionsWithDuplicates; + + _itemRepository = itemRepository; _actionSelected = actionsWithDuplicates.ActionSelected; if (_actionSelected) { @@ -38,7 +39,7 @@ public WatchItemCollection Apply(WatchItemCollection items) foreach (var item in items.Items) { - var selectItem = _caseSensitive && _actionSelected ? _dbContext.WatchItem.SelectDuplicateItems(item) : _dbContext.WatchItem.DuplicateItemsCaseSensitive(item); + var selectItem = _caseSensitive && _actionSelected ? _itemRepository.SelectDuplicateItems(item) : _itemRepository.DuplicateItemsCaseSensitive(item); if (selectItem.Count == 0) { idAddItems.Add(item.Id); diff --git a/WatchList.Core/Service/DataLoading/Rules/FilterByMoreGradeLoadRule.cs b/WatchList.Core/Service/DataLoading/Rules/FilterByMoreGradeLoadRule.cs index 31caaa97..cf862b47 100644 --- a/WatchList.Core/Service/DataLoading/Rules/FilterByMoreGradeLoadRule.cs +++ b/WatchList.Core/Service/DataLoading/Rules/FilterByMoreGradeLoadRule.cs @@ -4,10 +4,7 @@ namespace WatchList.Core.Service.DataLoading.Rules { public class FilterByMoreGradeLoadRule : ILoadRule { - public FilterByMoreGradeLoadRule(Grade moreGrade) - { - MoreGrade = moreGrade; - } + public FilterByMoreGradeLoadRule(ILoadRulesConfig config) => MoreGrade = config.MoreGrade; public Grade MoreGrade { get; private set; } diff --git a/WatchList.Core/Service/DataLoading/Rules/FilterByTypeCinemaLoadRule.cs b/WatchList.Core/Service/DataLoading/Rules/FilterByTypeCinemaLoadRule.cs index 78606dd7..b4efb924 100644 --- a/WatchList.Core/Service/DataLoading/Rules/FilterByTypeCinemaLoadRule.cs +++ b/WatchList.Core/Service/DataLoading/Rules/FilterByTypeCinemaLoadRule.cs @@ -1,10 +1,11 @@ using WatchList.Core.Model.ItemCinema.Components; +using WatchList.Core.Model.Load; namespace WatchList.Core.Service.DataLoading.Rules { public class FilterByTypeCinemaLoadRule : ILoadRule { - public FilterByTypeCinemaLoadRule(TypeCinema typeCinema) => TypeCinemaDataLoad = typeCinema; + public FilterByTypeCinemaLoadRule(ILoadRulesConfig config) => TypeCinemaDataLoad = config.TypeCinemaLoad; public TypeCinema TypeCinemaDataLoad { get; private set; } diff --git a/WatchList.Core/Service/Extension/DuplicateItemExtension.cs b/WatchList.Core/Service/Extension/DuplicateItemExtension.cs deleted file mode 100644 index 0d3d6c34..00000000 --- a/WatchList.Core/Service/Extension/DuplicateItemExtension.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using WatchList.Core.Model.ItemCinema; -using WatchList.Core.Repository.Db; - -namespace WatchList.Core.Service.Extension -{ - public static class DuplicateItemExtension - { - public static List SelectDuplicateItems(this DbSet dbSet, WatchItem item) => - dbSet.Where(x => - (x.Title == item.Title && x.Sequel == item.Sequel && x.Type == item.Type)). - Take(2).Select(x => x.Id).ToList(); - - public static List DuplicateItemsCaseSensitive(this DbSet dbSet, WatchItem item) => - dbSet.ToList(). - Where(x => x.TitleNormalized == item.TitleNormalized && x.Sequel == item.Sequel && x.Type == item.Type). - Take(2).Select(x => x.Id).ToList(); - - public static Guid ReplaceIdIsNotFree(this WatchCinemaDbContext dbContext, WatchItem item) - { - var idDuplicate = dbContext.WatchItem.Where(x => x.Id == item.Id).Take(2).Select(x => x.Id).ToList(); - return idDuplicate.Count != 0 ? Guid.NewGuid() : item.Id; - } - } -} diff --git a/WatchList.Core/Service/WatchItemService.cs b/WatchList.Core/Service/WatchItemService.cs index afff06f3..5190109f 100644 --- a/WatchList.Core/Service/WatchItemService.cs +++ b/WatchList.Core/Service/WatchItemService.cs @@ -1,9 +1,7 @@ using WatchList.Core.Model.ItemCinema; using WatchList.Core.PageItem; using WatchList.Core.Repository; -using WatchList.Core.Repository.Db; using WatchList.Core.Service.Component; -using WatchList.Core.Service.Extension; namespace WatchList.Core.Service { @@ -11,16 +9,13 @@ public class WatchItemService { public const string DuplicateReplaceMessage = "The append item is a duplicate. Replace element?"; - private readonly WatchCinemaDbContext _db; - private readonly WatchItemRepository _repository; private readonly IMessageBox _messageBox; - public WatchItemService(WatchCinemaDbContext dbContext, IMessageBox messageBox) + public WatchItemService(WatchItemRepository itemRepository, IMessageBox messageBox) { - _db = dbContext; - _repository = new WatchItemRepository(_db); + _repository = itemRepository; _messageBox = messageBox; } @@ -30,7 +25,7 @@ public WatchItemService(WatchCinemaDbContext dbContext, IMessageBox messageBox) public void Add(WatchItem item) { - var selectItem = _db.WatchItem.SelectDuplicateItems(item); + var selectItem = _repository.SelectDuplicateItems(item); var countDuplicate = selectItem.Count; if (countDuplicate == 0) @@ -48,10 +43,10 @@ public void Add(WatchItem item) public void Update(WatchItem oldItem, WatchItem modifiedItem) { - var selectItem = _db.WatchItem.SelectDuplicateItems(modifiedItem); + var selectItem = _repository.SelectDuplicateItems(modifiedItem); var countDuplicate = selectItem.Count; - if (countDuplicate == 1) + if (countDuplicate == 1 && oldItem.Title != modifiedItem.Title) { if (_messageBox.ShowQuestionSaveItem(DuplicateReplaceMessage)) { diff --git a/WatchList.Test/Components/TestAggregateLoadRule.cs b/WatchList.Test/Components/TestAggregateLoadRule.cs new file mode 100644 index 00000000..7344f54a --- /dev/null +++ b/WatchList.Test/Components/TestAggregateLoadRule.cs @@ -0,0 +1,19 @@ +using WatchList.Core.Repository; +using WatchList.Core.Service.DataLoading.Rules; + +namespace WatchList.Test.Components +{ + public class TestAggregateLoadRule : AggregateLoadRule + { + public TestAggregateLoadRule(WatchItemRepository itemRepository, TestLoadRuleConfig loadRulesConfig) + : base(new ILoadRule[] + { + new DeleteGradeRule(loadRulesConfig), + new FilterByTypeCinemaLoadRule(loadRulesConfig), + new FilterByMoreGradeLoadRule(loadRulesConfig), + new DuplicateLoadRule(itemRepository, loadRulesConfig), + }) + { + } + } +} diff --git a/WatchList.Test/TestAppDbContextFactory.cs b/WatchList.Test/Components/TestAppDbContextFactory.cs similarity index 94% rename from WatchList.Test/TestAppDbContextFactory.cs rename to WatchList.Test/Components/TestAppDbContextFactory.cs index 63671788..3645425c 100644 --- a/WatchList.Test/TestAppDbContextFactory.cs +++ b/WatchList.Test/Components/TestAppDbContextFactory.cs @@ -2,7 +2,7 @@ using Microsoft.EntityFrameworkCore; using WatchList.Core.Repository.Db; -namespace WatchList.Test +namespace WatchList.Test.Components { public class TestAppDbContextFactory { diff --git a/WatchList.Test/Components/TestLoadRuleConfig.cs b/WatchList.Test/Components/TestLoadRuleConfig.cs new file mode 100644 index 00000000..c12dc7ed --- /dev/null +++ b/WatchList.Test/Components/TestLoadRuleConfig.cs @@ -0,0 +1,28 @@ +using WatchList.Core.Model.ItemCinema.Components; +using WatchList.Core.Model.Load; + +namespace WatchList.Test.Components +{ + public class TestLoadRuleConfig : ILoadRulesConfig + { + public TestLoadRuleConfig() + { + } + + public TestLoadRuleConfig(bool deleteGrade, TypeCinema typeCinema, Grade grade, ActionDuplicateItems actionDuplicateItems) + { + DeleteGrade = deleteGrade; + TypeCinemaLoad = typeCinema; + MoreGrade = grade; + ActionsWithDuplicates = actionDuplicateItems; + } + + public bool DeleteGrade { get; set; } = false; + + public TypeCinema TypeCinemaLoad { get; set; } = TypeCinema.AllType; + + public Grade MoreGrade { get; set; } = Grade.AnyGrade; + + public ActionDuplicateItems ActionsWithDuplicates { get; set; } = new ActionDuplicateItems(); + } +} diff --git a/WatchList.Test/CoreTest/FilterItemTest/FilterByStatusTest.cs b/WatchList.Test/CoreTest/FilterItemTest/FilterByStatusTest.cs index f976a5ee..9757c8b7 100644 --- a/WatchList.Test/CoreTest/FilterItemTest/FilterByStatusTest.cs +++ b/WatchList.Test/CoreTest/FilterItemTest/FilterByStatusTest.cs @@ -5,6 +5,7 @@ using WatchList.Core.Model.ItemCinema.Components; using WatchList.Core.Model.Sorting; using WatchList.Core.PageItem; +using WatchList.Test.Components; namespace WatchList.Test.CoreTest.FilterItemTest { diff --git a/WatchList.Test/CoreTest/FilterItemTest/FilterByTypeTest.cs b/WatchList.Test/CoreTest/FilterItemTest/FilterByTypeTest.cs index 727ea613..f03eb9dc 100644 --- a/WatchList.Test/CoreTest/FilterItemTest/FilterByTypeTest.cs +++ b/WatchList.Test/CoreTest/FilterItemTest/FilterByTypeTest.cs @@ -5,6 +5,7 @@ using WatchList.Core.Model.ItemCinema.Components; using WatchList.Core.Model.Sorting; using WatchList.Core.PageItem; +using WatchList.Test.Components; namespace WatchList.Test.CoreTest.FilterItemTest { diff --git a/WatchList.Test/CoreTest/SortFieldTest.cs b/WatchList.Test/CoreTest/SortFieldTest.cs index de283262..a700939f 100644 --- a/WatchList.Test/CoreTest/SortFieldTest.cs +++ b/WatchList.Test/CoreTest/SortFieldTest.cs @@ -4,6 +4,7 @@ using WatchList.Core.Model.ItemCinema.Components; using WatchList.Core.Model.Sorting; using WatchList.Core.PageItem; +using WatchList.Test.Components; namespace WatchList.Test.CoreTest { diff --git a/WatchList.Test/CoreTest/WatchItemRepositoryTest.cs b/WatchList.Test/CoreTest/WatchItemRepositoryTest.cs index 6a45b3b0..5d1e063c 100644 --- a/WatchList.Test/CoreTest/WatchItemRepositoryTest.cs +++ b/WatchList.Test/CoreTest/WatchItemRepositoryTest.cs @@ -6,6 +6,7 @@ using WatchList.Core.Model.Sorting; using WatchList.Core.PageItem; using WatchList.Core.Repository; +using WatchList.Test.Components; namespace WatchList.Test.CoreTest { diff --git a/WatchList.Test/CoreTest/WatchItemServiceTest/AddDuplicateItemTest.cs b/WatchList.Test/CoreTest/WatchItemServiceTest/AddDuplicateItemTest.cs index ab804135..41328678 100644 --- a/WatchList.Test/CoreTest/WatchItemServiceTest/AddDuplicateItemTest.cs +++ b/WatchList.Test/CoreTest/WatchItemServiceTest/AddDuplicateItemTest.cs @@ -2,8 +2,10 @@ using Moq; using WatchList.Core.Model.ItemCinema; using WatchList.Core.Model.ItemCinema.Components; +using WatchList.Core.Repository; using WatchList.Core.Service; using WatchList.Core.Service.Component; +using WatchList.Test.Components; namespace WatchList.Test.CoreTest.WatchItemServiceTest { @@ -56,9 +58,10 @@ public void Add_With_Replace_Duplicate_Element(List items, WatchItem { // Arrange var dbContext = new TestAppDbContextFactory().Create(); + var itemRepository = new WatchItemRepository(dbContext); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowQuestionSaveItem(WatchItemService.DuplicateReplaceMessage)).Returns(true); - var service = new WatchItemService(dbContext, messageBox.Object); + var service = new WatchItemService(itemRepository, messageBox.Object); dbContext.AddRange(items); dbContext.SaveChanges(); service.Add(addItem); diff --git a/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByDeleteGradeRuleTest.cs b/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByDeleteGradeRuleTest.cs index dc1a6d3d..8a703e62 100644 --- a/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByDeleteGradeRuleTest.cs +++ b/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByDeleteGradeRuleTest.cs @@ -8,7 +8,7 @@ using WatchList.Core.Repository; using WatchList.Core.Service.Component; using WatchList.Core.Service.DataLoading; -using WatchList.Core.Service.DataLoading.Rules; +using WatchList.Test.Components; namespace WatchList.Test.CoreTest.WatchItemServiceTest.DataLoadingTest { @@ -112,21 +112,24 @@ public void Add_Data_File_And_Replace_Duplicate_Element(List items, L { // Arrange var dbContext = new TestAppDbContextFactory().Create(); + var itemRepository = new WatchItemRepository(dbContext); var dbContextDownloadItem = new TestAppDbContextFactory().Create(); + var watchItemRepository = new WatchItemRepository(dbContext); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowDataReplaceQuestion(It.IsAny())).Returns(dialogReplaceItem); - var service = new DownloadDataService(dbContext, messageBox.Object); - var loadRuleGrade = new DeleteGradeRule(false); - var loadDuplicateItemRule = new DuplicateLoadRule( - dbContext, - new ActionDuplicateItems(true, new List + var service = new DownloadDataService(watchItemRepository, messageBox.Object); + var loadRuleConfig = new TestLoadRuleConfig() + { + DeleteGrade = false, + ActionsWithDuplicates = new ActionDuplicateItems(true, new List { new DuplicateLoadingRules(DuplicateLoadingRules.UpdateDuplicate, true), new DuplicateLoadingRules(DuplicateLoadingRules.CaseSensitive, true), - })); - var loadRule = new AggregateLoadRule(new ILoadRule[] { loadRuleGrade, loadDuplicateItemRule }); + }), + }; + var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig); var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem); dbContext.AddRange(items); @@ -149,21 +152,24 @@ public async Task Add_Data_File_And_Replace_Duplicate_ElementAsync(List(); messageBox.Setup(foo => foo.ShowDataReplaceQuestion(It.IsAny())).Returns(dialogReplaceItem); - var service = new DownloadDataService(dbContext, messageBox.Object); - var loadRuleGrade = new DeleteGradeRule(false); - var loadDuplicateItemRule = new DuplicateLoadRule( - dbContext, - new ActionDuplicateItems(true, new List + var service = new DownloadDataService(watchItemRepository, messageBox.Object); + var loadRuleConfig = new TestLoadRuleConfig() + { + DeleteGrade = false, + ActionsWithDuplicates = new ActionDuplicateItems(true, new List { new DuplicateLoadingRules(DuplicateLoadingRules.UpdateDuplicate, true), new DuplicateLoadingRules(DuplicateLoadingRules.CaseSensitive, true), - })); - var loadRule = new AggregateLoadRule(new ILoadRule[] { loadRuleGrade, loadDuplicateItemRule }); + }), + }; + var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig); var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem); dbContext.AddRange(items); @@ -185,7 +191,9 @@ public void Add_Data_File_And_One_Replace_And_Not_Replace_Duplicate_Element(List { // Arrange var dbContext = new TestAppDbContextFactory().Create(); + var itemRepository = new WatchItemRepository(dbContext); var dbContextDownloadItem = new TestAppDbContextFactory().Create(); + var watchItemRepository = new WatchItemRepository(dbContext); var messageBox = new Mock(); foreach (var item in dictionaryAddItem) @@ -193,16 +201,17 @@ public void Add_Data_File_And_One_Replace_And_Not_Replace_Duplicate_Element(List messageBox.Setup(foo => foo.ShowDataReplaceQuestion(item.Key)).Returns(item.Value); } - var service = new DownloadDataService(dbContext, messageBox.Object); - var loadRuleGrade = new DeleteGradeRule(false); - var loadDuplicateItemRule = new DuplicateLoadRule( - dbContext, - new ActionDuplicateItems(true, new List + var service = new DownloadDataService(watchItemRepository, messageBox.Object); + var loadRuleConfig = new TestLoadRuleConfig() + { + DeleteGrade = false, + ActionsWithDuplicates = new ActionDuplicateItems(true, new List { new DuplicateLoadingRules(DuplicateLoadingRules.UpdateDuplicate, true), new DuplicateLoadingRules(DuplicateLoadingRules.CaseSensitive, true), - })); - var loadRule = new AggregateLoadRule(new ILoadRule[] { loadRuleGrade, loadDuplicateItemRule }); + }), + }; + var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig); var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem); dbContext.AddRange(items); @@ -224,7 +233,9 @@ public async Task Add_Data_File_And_One_Replace_And_Not_Replace_Duplicate_Elemen { // Arrange var dbContext = new TestAppDbContextFactory().Create(); + var itemRepository = new WatchItemRepository(dbContext); var dbContextDownloadItem = new TestAppDbContextFactory().Create(); + var watchItemRepository = new WatchItemRepository(dbContext); var messageBox = new Mock(); foreach (var item in dictionaryAddItem) @@ -232,16 +243,17 @@ public async Task Add_Data_File_And_One_Replace_And_Not_Replace_Duplicate_Elemen messageBox.Setup(foo => foo.ShowDataReplaceQuestion(item.Key)).Returns(item.Value); } - var service = new DownloadDataService(dbContext, messageBox.Object); - var loadRuleGrade = new DeleteGradeRule(false); - var loadDuplicateItemRule = new DuplicateLoadRule( - dbContext, - new ActionDuplicateItems(true, new List + var service = new DownloadDataService(watchItemRepository, messageBox.Object); + var loadRuleConfig = new TestLoadRuleConfig() + { + DeleteGrade = false, + ActionsWithDuplicates = new ActionDuplicateItems(true, new List { new DuplicateLoadingRules(DuplicateLoadingRules.UpdateDuplicate, true), new DuplicateLoadingRules(DuplicateLoadingRules.CaseSensitive, true), - })); - var loadRule = new AggregateLoadRule(new ILoadRule[] { loadRuleGrade, loadDuplicateItemRule }); + }), + }; + var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig); var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem); dbContext.AddRange(items); diff --git a/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByRuleMoreGradeTest.cs b/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByRuleMoreGradeTest.cs index a57e5021..f9aafc3a 100644 --- a/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByRuleMoreGradeTest.cs +++ b/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByRuleMoreGradeTest.cs @@ -7,7 +7,7 @@ using WatchList.Core.Repository; using WatchList.Core.Service.Component; using WatchList.Core.Service.DataLoading; -using WatchList.Core.Service.DataLoading.Rules; +using WatchList.Test.Components; namespace WatchList.Test.CoreTest.WatchItemServiceTest.DataLoadingTest { @@ -129,15 +129,16 @@ public void Load_Data_File_By_Rule_Type_Cinema(List items, List(); messageBox.Setup(foo => foo.ShowDataReplaceQuestion(It.IsAny())).Returns(DialogReplaceItemQuestion.AllYes); - var service = new DownloadDataService(dbContext, messageBox.Object); - var loadRuleMoreGrade = new FilterByMoreGradeLoadRule(moreGrade); - var loadRuleDuplicateItem = new DuplicateLoadRule(dbContext, new ActionDuplicateItems()); - var loadRule = new AggregateLoadRule(new ILoadRule[] { loadRuleMoreGrade, loadRuleDuplicateItem }); + var service = new DownloadDataService(watchItemRepository, messageBox.Object); + var loadRuleConfig = new TestLoadRuleConfig() { MoreGrade = moreGrade }; + var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig); var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem); dbContext.AddRange(items); @@ -161,15 +162,16 @@ public async Task Load_Data_File_By_Rule_Type_CinemaAsync(List items, { // Arrange var dbContext = new TestAppDbContextFactory().Create(); + var itemRepository = new WatchItemRepository(dbContext); var dbContextDownloadItem = new TestAppDbContextFactory().Create(); + var watchItemRepository = new WatchItemRepository(dbContext); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowDataReplaceQuestion(It.IsAny())).Returns(DialogReplaceItemQuestion.AllYes); - var service = new DownloadDataService(dbContext, messageBox.Object); - var loadRuleMoreGrade = new FilterByMoreGradeLoadRule(moreGrade); - var loadRuleDuplicateItem = new DuplicateLoadRule(dbContext, new ActionDuplicateItems()); - var loadRule = new AggregateLoadRule(new ILoadRule[] { loadRuleMoreGrade, loadRuleDuplicateItem }); + var service = new DownloadDataService(watchItemRepository, messageBox.Object); + var loadRuleConfig = new TestLoadRuleConfig() { MoreGrade = moreGrade }; + var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig); var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem); dbContext.AddRange(items); diff --git a/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByRuleTypeCinemaTest.cs b/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByRuleTypeCinemaTest.cs index b8e3dfbf..555af5a0 100644 --- a/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByRuleTypeCinemaTest.cs +++ b/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadDataByRuleTypeCinemaTest.cs @@ -2,12 +2,11 @@ using Moq; using WatchList.Core.Model.ItemCinema; using WatchList.Core.Model.ItemCinema.Components; -using WatchList.Core.Model.Load; using WatchList.Core.Model.QuestionResult; using WatchList.Core.Repository; using WatchList.Core.Service.Component; using WatchList.Core.Service.DataLoading; -using WatchList.Core.Service.DataLoading.Rules; +using WatchList.Test.Components; namespace WatchList.Test.CoreTest.WatchItemServiceTest.DataLoadingTest { @@ -128,15 +127,16 @@ public void Load_Data_File_By_Rule_Type_Cinema(List items, List(); messageBox.Setup(foo => foo.ShowDataReplaceQuestion(It.IsAny())).Returns(DialogReplaceItemQuestion.AllYes); - var service = new DownloadDataService(dbContext, messageBox.Object); - var loadRuleTypeCinema = new FilterByTypeCinemaLoadRule(typeCinema); - var loadRuleDuplicateItem = new DuplicateLoadRule(dbContext, new ActionDuplicateItems()); - var loadRule = new AggregateLoadRule(new ILoadRule[] { loadRuleTypeCinema, loadRuleDuplicateItem }); + var service = new DownloadDataService(watchItemRepository, messageBox.Object); + var loadRuleConfig = new TestLoadRuleConfig() { TypeCinemaLoad = typeCinema }; + var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig); var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem); dbContext.AddRange(items); @@ -160,15 +160,16 @@ public async Task Load_Data_File_By_Rule_Type_CinemaAsync(List items, { // Arrange var dbContext = new TestAppDbContextFactory().Create(); + var itemRepository = new WatchItemRepository(dbContext); var dbContextDownloadItem = new TestAppDbContextFactory().Create(); + var watchItemRepository = new WatchItemRepository(dbContext); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowDataReplaceQuestion(It.IsAny())).Returns(DialogReplaceItemQuestion.AllYes); - var service = new DownloadDataService(dbContext, messageBox.Object); - var loadRuleTypeCinema = new FilterByTypeCinemaLoadRule(typeCinema); - var loadRuleDuplicateItem = new DuplicateLoadRule(dbContext, new ActionDuplicateItems()); - var loadRule = new AggregateLoadRule(new ILoadRule[] { loadRuleTypeCinema, loadRuleDuplicateItem }); + var service = new DownloadDataService(watchItemRepository, messageBox.Object); + var loadRuleConfig = new TestLoadRuleConfig() { TypeCinemaLoad = typeCinema }; + var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig); var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem); dbContext.AddRange(items); diff --git a/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadPagedListTest.cs b/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadPagedListTest.cs index 2bad5b56..3fd80614 100644 --- a/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadPagedListTest.cs +++ b/WatchList.Test/CoreTest/WatchItemServiceTest/DataLoadingTest/DownloadPagedListTest.cs @@ -2,12 +2,11 @@ using Moq; using WatchList.Core.Model.ItemCinema; using WatchList.Core.Model.ItemCinema.Components; -using WatchList.Core.Model.Load; using WatchList.Core.Model.QuestionResult; using WatchList.Core.Repository; using WatchList.Core.Service.Component; using WatchList.Core.Service.DataLoading; -using WatchList.Core.Service.DataLoading.Rules; +using WatchList.Test.Components; namespace WatchList.Test.CoreTest.WatchItemServiceTest.DataLoadingTest { @@ -51,17 +50,16 @@ public void Add_Data_File(List items, List addDownloadItem { // Arrange var dbContext = new TestAppDbContextFactory().Create(); + var itemRepository = new WatchItemRepository(dbContext); var dbContextDownloadItem = new TestAppDbContextFactory().Create(); + var watchItemRepository = new WatchItemRepository(dbContext); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowDataReplaceQuestion(It.IsAny())).Returns(DialogReplaceItemQuestion.AllYes); - var service = new DownloadDataService(dbContext, messageBox.Object) { NumberOfItemPerPage = PageSize }; - var loadRuleGrade = new DeleteGradeRule(false); - var loadRuleType = new FilterByTypeCinemaLoadRule(TypeCinema.AllType); - var loadRuleMoreGrade = new FilterByMoreGradeLoadRule(Grade.AnyGrade); - var loadRuleDuplicateItem = new DuplicateLoadRule(dbContext, new ActionDuplicateItems()); - var loadRule = new AggregateLoadRule(new ILoadRule[] { loadRuleGrade, loadRuleType, loadRuleMoreGrade, loadRuleDuplicateItem }); + var service = new DownloadDataService(watchItemRepository, messageBox.Object) { NumberOfItemPerPage = PageSize }; + var loadRuleConfig = new TestLoadRuleConfig(); + var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig); var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem); dbContext.AddRange(items); @@ -83,17 +81,16 @@ public async Task Add_Data_FileAsync(List items, List addD { // Arrange var dbContext = new TestAppDbContextFactory().Create(); + var itemRepository = new WatchItemRepository(dbContext); var dbContextDownloadItem = new TestAppDbContextFactory().Create(); + var watchItemRepository = new WatchItemRepository(dbContext); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowDataReplaceQuestion(It.IsAny())).Returns(DialogReplaceItemQuestion.AllYes); - var service = new DownloadDataService(dbContext, messageBox.Object) { NumberOfItemPerPage = PageSize }; - var loadRuleGrade = new DeleteGradeRule(false); - var loadRuleType = new FilterByTypeCinemaLoadRule(TypeCinema.AllType); - var loadRuleMoreGrade = new FilterByMoreGradeLoadRule(Grade.AnyGrade); - var loadRuleDuplicateItem = new DuplicateLoadRule(dbContext, new ActionDuplicateItems()); - var loadRule = new AggregateLoadRule(new ILoadRule[] { loadRuleGrade, loadRuleType, loadRuleMoreGrade, loadRuleDuplicateItem }); + var service = new DownloadDataService(watchItemRepository, messageBox.Object) { NumberOfItemPerPage = PageSize }; + var loadRuleConfig = new TestLoadRuleConfig(); + var loadRule = new TestAggregateLoadRule(itemRepository, loadRuleConfig); var repositoryDataDownload = new WatchItemRepository(dbContextDownloadItem); dbContext.AddRange(items); diff --git a/WatchList.Test/CoreTest/WatchItemServiceTest/NonDuplicateElementsTest.cs b/WatchList.Test/CoreTest/WatchItemServiceTest/NonDuplicateElementsTest.cs index 6103ea46..4b558a87 100644 --- a/WatchList.Test/CoreTest/WatchItemServiceTest/NonDuplicateElementsTest.cs +++ b/WatchList.Test/CoreTest/WatchItemServiceTest/NonDuplicateElementsTest.cs @@ -2,8 +2,10 @@ using Moq; using WatchList.Core.Model.ItemCinema; using WatchList.Core.Model.ItemCinema.Components; +using WatchList.Core.Repository; using WatchList.Core.Service; using WatchList.Core.Service.Component; +using WatchList.Test.Components; namespace WatchList.Test.CoreTest.WatchItemServiceTest { @@ -65,9 +67,10 @@ public void Add_New_Item_In_Database(List items, WatchItem addItem, L { // Arrange var dbContext = new TestAppDbContextFactory().Create(); + var itemRepository = new WatchItemRepository(dbContext); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowQuestionSaveItem(WatchItemService.DuplicateReplaceMessage)).Returns(true); - var service = new WatchItemService(dbContext, messageBox.Object); + var service = new WatchItemService(itemRepository, messageBox.Object); dbContext.AddRange(items); dbContext.SaveChanges(); @@ -85,9 +88,10 @@ public void Update_Item_In_Database(List items, WatchItem oldItem, Wa { // Arrange var dbContext = new TestAppDbContextFactory().Create(); + var itemRepository = new WatchItemRepository(dbContext); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowQuestionSaveItem(WatchItemService.DuplicateReplaceMessage)).Returns(true); - var service = new WatchItemService(dbContext, messageBox.Object); + var service = new WatchItemService(itemRepository, messageBox.Object); dbContext.AddRange(items); dbContext.SaveChanges(); diff --git a/WatchList.Test/CoreTest/WatchItemServiceTest/UpdateDuplicateItemTest.cs b/WatchList.Test/CoreTest/WatchItemServiceTest/UpdateDuplicateItemTest.cs index 1495b091..9420ab40 100644 --- a/WatchList.Test/CoreTest/WatchItemServiceTest/UpdateDuplicateItemTest.cs +++ b/WatchList.Test/CoreTest/WatchItemServiceTest/UpdateDuplicateItemTest.cs @@ -2,8 +2,10 @@ using Moq; using WatchList.Core.Model.ItemCinema; using WatchList.Core.Model.ItemCinema.Components; +using WatchList.Core.Repository; using WatchList.Core.Service; using WatchList.Core.Service.Component; +using WatchList.Test.Components; namespace WatchList.Test.CoreTest.WatchItemServiceTest { @@ -56,9 +58,10 @@ public void Update_With_Replace_Duplicate_Element(List items, WatchIt { // Arrange var dbContext = new TestAppDbContextFactory().Create(); + var itemRepository = new WatchItemRepository(dbContext); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowQuestionSaveItem(WatchItemService.DuplicateReplaceMessage)).Returns(true); - var service = new WatchItemService(dbContext, messageBox.Object); + var service = new WatchItemService(itemRepository, messageBox.Object); dbContext.AddRange(items); dbContext.SaveChanges(); @@ -76,9 +79,10 @@ public void Update_With_Not_Replace_Duplicate_Element(List items, Wat { // Arrange var dbContext = new TestAppDbContextFactory().Create(); + var itemRepository = new WatchItemRepository(dbContext); var messageBox = new Mock(); messageBox.Setup(foo => foo.ShowQuestionSaveItem(WatchItemService.DuplicateReplaceMessage)).Returns(false); - var service = new WatchItemService(dbContext, messageBox.Object); + var service = new WatchItemService(itemRepository, messageBox.Object); dbContext.AddRange(items); dbContext.SaveChanges(); diff --git a/WatchList.WinForms/BindingItem/ModelDataLoad/ModelProcessUploadData.cs b/WatchList.WinForms/BindingItem/ModelDataLoad/LoadRulesConfigModel.cs similarity index 76% rename from WatchList.WinForms/BindingItem/ModelDataLoad/ModelProcessUploadData.cs rename to WatchList.WinForms/BindingItem/ModelDataLoad/LoadRulesConfigModel.cs index 9f3c5f9c..4e8f453a 100644 --- a/WatchList.WinForms/BindingItem/ModelDataLoad/ModelProcessUploadData.cs +++ b/WatchList.WinForms/BindingItem/ModelDataLoad/LoadRulesConfigModel.cs @@ -3,14 +3,14 @@ namespace WatchList.WinForms.BindingItem.ModelDataLoad { - public class ModelProcessUploadData : ModelBase + public class LoadRulesConfigModel : ModelBase, ILoadRulesConfig { - public ModelProcessUploadData() + public LoadRulesConfigModel() : this(false, new ActionDuplicateItems(), TypeCinema.AllType, Grade.AnyGrade) { } - public ModelProcessUploadData(bool deleteGrade, ActionDuplicateItems actionsWithDuplicates, TypeCinema typeCinema, Grade moreGrade) + public LoadRulesConfigModel(bool deleteGrade, ActionDuplicateItems actionsWithDuplicates, TypeCinema typeCinema, Grade moreGrade) { DeleteGrade = deleteGrade; ActionsWithDuplicates = actionsWithDuplicates; diff --git a/WatchList.WinForms/BoxCinemaForm.cs b/WatchList.WinForms/BoxCinemaForm.cs index 74c64d76..d81dae61 100644 --- a/WatchList.WinForms/BoxCinemaForm.cs +++ b/WatchList.WinForms/BoxCinemaForm.cs @@ -1,10 +1,10 @@ using MaterialSkin.Controls; +using Microsoft.Extensions.DependencyInjection; using WatchList.Core.Model.Filter.Components; using WatchList.Core.Model.ItemCinema; using WatchList.Core.Model.ItemCinema.Components; using WatchList.Core.PageItem; using WatchList.Core.Repository; -using WatchList.Core.Repository.Db; using WatchList.Core.Service; using WatchList.Core.Service.Component; using WatchList.Core.Service.DataLoading; @@ -14,7 +14,6 @@ using WatchList.WinForms.ChildForms; using WatchList.WinForms.ChildForms.Extension; using WatchList.WinForms.Extension; -using WatchList.WinForms.Message; namespace WatchList.WinForms { @@ -24,7 +23,6 @@ namespace WatchList.WinForms public partial class BoxCinemaForm : MaterialForm { private const string HighlightTheDesiredLine = "No items selected."; - private const int NumberOfItemPerPage = 500; private const int IndexColumnName = 0; private const int IndexColumnSequel = 1; @@ -34,20 +32,22 @@ public partial class BoxCinemaForm : MaterialForm private const int IndexColumnId = 5; private const int IndexColumnType = 6; + private readonly IServiceProvider _serviceProvider; private readonly WatchItemService _itemService; private readonly IMessageBox _messageBox; - private readonly WatchCinemaDbContext _dbContext; + private readonly WatchItemRepository _itemRepository; private WatchItemSearchRequest _searchRequest = new WatchItemSearchRequest(); private PagedList _pagedList; - public BoxCinemaForm(WatchCinemaDbContext db) + public BoxCinemaForm(IServiceProvider serviceProvider) { InitializeComponent(); - _dbContext = db; - _messageBox = new MessageBoxShow(); - _itemService = new WatchItemService(db, _messageBox); + _serviceProvider = serviceProvider; + _itemRepository = serviceProvider.GetRequiredService(); + _messageBox = serviceProvider.GetRequiredService(); + _itemService = serviceProvider.GetRequiredService(); _pagedList = _itemService.GetPage(_searchRequest); Load += BoxCinemaForm_Load; @@ -88,7 +88,7 @@ private void BtnCancelFilter_Click(object sender, EventArgs e) private void BtnAddCinema_Click(object sender, EventArgs e) { - var addForm = new AddCinemaForm(); + var addForm = new AddCinemaForm(_messageBox); if (addForm.ShowDialog() != DialogResult.OK) { @@ -107,7 +107,7 @@ private void BtnEditRow_Click(object sender, EventArgs e) if (indexEditRow.Count == 1) { var oldItem = GetItem(indexEditRow.First()); - var updateForm = new EditorItemCinemaForm(oldItem); + var updateForm = new EditorItemCinemaForm(oldItem, _messageBox); if (updateForm.ShowDialog() != DialogResult.OK) { @@ -152,7 +152,7 @@ private void BtnDeleteMovie_Click(object sender, EventArgs e) private void BtnDownloadDataFile_Click(object sender, EventArgs e) { - var dataLoadingForm = new MergeDatabaseForm(); + var dataLoadingForm = _serviceProvider.GetRequiredService(); if (dataLoadingForm.ShowDialog() != DialogResult.OK) { return; @@ -165,17 +165,17 @@ private void BtnDownloadDataFile_Click(object sender, EventArgs e) } var dbContext = new FileDbContextFactory(openReplaceDataFromFile.FileName).Create(); - var algorithmLoadData = dataLoadingForm.GetLoadData(); - - var loadRuleHasGrade = new DeleteGradeRule(algorithmLoadData.DeleteGrade); - var loadRuleType = new FilterByTypeCinemaLoadRule(algorithmLoadData.TypeCinemaLoad); - var loadRuleMoreGrade = new FilterByMoreGradeLoadRule(algorithmLoadData.MoreGrade); - var loadDuplicateItem = new DuplicateLoadRule(_dbContext, algorithmLoadData.ActionsWithDuplicates); - var rules = new AggregateLoadRule { loadRuleHasGrade, loadRuleType, loadRuleMoreGrade, loadDuplicateItem }; + var loadRuleConfig = dataLoadingForm.GetLoadRuleConfig(); + var loadRuleHasGrade = new DeleteGradeRule(loadRuleConfig); + var loadRuleType = new FilterByTypeCinemaLoadRule(loadRuleConfig); + var loadRuleMoreGrade = new FilterByMoreGradeLoadRule(loadRuleConfig); + var loadDuplicateItem = new DuplicateLoadRule(_itemRepository, loadRuleConfig); + var aggregateRules = new AggregateLoadRule { loadRuleHasGrade, loadRuleType, loadRuleMoreGrade, loadDuplicateItem }; var repositoryDataDownload = new WatchItemRepository(dbContext); - var downloadDataService = new DownloadDataService(_dbContext, _messageBox) { NumberOfItemPerPage = NumberOfItemPerPage }; - downloadDataService.Download(repositoryDataDownload, rules); + + var downloadDataService = _serviceProvider.GetRequiredService(); + downloadDataService.Download(repositoryDataDownload, aggregateRules); UpdateGridData(); } diff --git a/WatchList.WinForms/BuilderDbContext/FileDbContextFactory.cs b/WatchList.WinForms/BuilderDbContext/FileDbContextFactory.cs index 7826f24d..ca3567b2 100644 --- a/WatchList.WinForms/BuilderDbContext/FileDbContextFactory.cs +++ b/WatchList.WinForms/BuilderDbContext/FileDbContextFactory.cs @@ -16,6 +16,7 @@ public WatchCinemaDbContext Create() { x.MigrationsAssembly(typeof(DbContextFactory).Assembly.FullName); }); + return new WatchCinemaDbContext(builder.Options); } } diff --git a/WatchList.WinForms/ChildForms/AddCinemaForm.cs b/WatchList.WinForms/ChildForms/AddCinemaForm.cs index e366eadc..f1483634 100644 --- a/WatchList.WinForms/ChildForms/AddCinemaForm.cs +++ b/WatchList.WinForms/ChildForms/AddCinemaForm.cs @@ -4,7 +4,6 @@ using WatchList.WinForms.BindingItem.ModelAddAndEditForm; using WatchList.WinForms.BindingItem.ModelBoxForm; using WatchList.WinForms.ChildForms.Extension; -using WatchList.WinForms.Message; namespace WatchList.WinForms.ChildForms { @@ -17,9 +16,9 @@ public partial class AddCinemaForm : MaterialForm private StatusCinema _status = StatusCinema.Planned; - public AddCinemaForm() + public AddCinemaForm(IMessageBox messageBox) { - _messageBox = new MessageBoxShow(); + _messageBox = messageBox; InitializeComponent(); } diff --git a/WatchList.WinForms/ChildForms/EditorItemCinemaForm.cs b/WatchList.WinForms/ChildForms/EditorItemCinemaForm.cs index 7cd2dc7e..adb3c8c1 100644 --- a/WatchList.WinForms/ChildForms/EditorItemCinemaForm.cs +++ b/WatchList.WinForms/ChildForms/EditorItemCinemaForm.cs @@ -4,7 +4,6 @@ using WatchList.WinForms.BindingItem.ModelAddAndEditForm; using WatchList.WinForms.BindingItem.ModelBoxForm; using WatchList.WinForms.ChildForms.Extension; -using WatchList.WinForms.Message; namespace WatchList.WinForms.ChildForms { @@ -16,10 +15,10 @@ public partial class EditorItemCinemaForm : MaterialForm private readonly CinemaModel _cinema; private readonly IMessageBox _messageBox; - public EditorItemCinemaForm(CinemaModel cinema) + public EditorItemCinemaForm(CinemaModel cinema, IMessageBox messageBox) { _cinema = cinema ?? throw new ArgumentNullException("Item cinema not null", nameof(cinema)); - _messageBox = new MessageBoxShow(); + _messageBox = messageBox; InitializeComponent(); } diff --git a/WatchList.WinForms/ChildForms/MergeDatabaseForm.cs b/WatchList.WinForms/ChildForms/MergeDatabaseForm.cs index 52875782..01c88e5b 100644 --- a/WatchList.WinForms/ChildForms/MergeDatabaseForm.cs +++ b/WatchList.WinForms/ChildForms/MergeDatabaseForm.cs @@ -4,7 +4,6 @@ using WatchList.Core.Model.Load.ItemActions; using WatchList.Core.Service.Component; using WatchList.WinForms.BindingItem.ModelDataLoad; -using WatchList.WinForms.Message; namespace WatchList.WinForms.ChildForms { @@ -15,9 +14,9 @@ public partial class MergeDatabaseForm : MaterialForm { private readonly IMessageBox _messageBox; - public MergeDatabaseForm() + public MergeDatabaseForm(IMessageBox messageBox) { - _messageBox = new MessageBoxShow(); + _messageBox = messageBox; InitializeComponent(); } @@ -27,20 +26,20 @@ public MergeDatabaseForm() private Grade SelectGrade => cmbGrade.SelectedValue != null ? (Grade)cmbGrade.SelectedValue : throw new Exception("Wrong combo box format"); - public ModelProcessUploadData GetLoadData() + public ILoadRulesConfig GetLoadRuleConfig() { var isDeleteGrade = cbExistGrade.Checked; var considerDuplicates = cbConsiderDuplicates.Checked; if (!considerDuplicates) { - return new ModelProcessUploadData(isDeleteGrade, new ActionDuplicateItems(), SelectTypeCinema, SelectGrade); + return new LoadRulesConfigModel(isDeleteGrade, new ActionDuplicateItems(), SelectTypeCinema, SelectGrade); } var listDuplicateLoadRule = clbActionsWithDuplicates.Items.Select(e => new DuplicateLoadingRules( e.Tag as DuplicateLoadingRules ?? throw new NullReferenceException("Null reference argument for parameter"), checkAction: e.Checked)).ToList(); - return new ModelProcessUploadData(isDeleteGrade, new ActionDuplicateItems(considerDuplicates, listDuplicateLoadRule), SelectTypeCinema, SelectGrade); + return new LoadRulesConfigModel(isDeleteGrade, new ActionDuplicateItems(considerDuplicates, listDuplicateLoadRule), SelectTypeCinema, SelectGrade); } private void BtnClear_Click(object sender, EventArgs e) => SetupDefaultValues(); diff --git a/WatchList.WinForms/Program.cs b/WatchList.WinForms/Program.cs index df3ffeba..b56d4022 100644 --- a/WatchList.WinForms/Program.cs +++ b/WatchList.WinForms/Program.cs @@ -1,6 +1,13 @@ using MaterialSkin; +using Microsoft.Extensions.DependencyInjection; +using WatchList.Core.Repository; +using WatchList.Core.Service; +using WatchList.Core.Service.Component; +using WatchList.Core.Service.DataLoading; using WatchList.Migrations.SQLite; using WatchList.WinForms.BuilderDbContext; +using WatchList.WinForms.ChildForms; +using WatchList.WinForms.Message; namespace WatchList.WinForms { @@ -16,10 +23,27 @@ public static void Main() // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - using var db = new FileDbContextFactory("app.db").Create(); - var migrator = new DbMigrator(db); - migrator.Migrate(); - var form = new BoxCinemaForm(db); + var serviceCollection = new ServiceCollection() + .AddSingleton(new FileDbContextFactory("app.db")) + .AddScoped(e => e.GetRequiredService().Create()) + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped() + .AddScoped() + .AddTransient() + .AddTransient(); + + using var contain = serviceCollection.BuildServiceProvider(new ServiceProviderOptions + { + ValidateOnBuild = true, + ValidateScopes = true, + }); + + using var scope = contain.CreateScope(); + + scope.ServiceProvider.GetRequiredService().Migrate(); + var form = scope.ServiceProvider.GetRequiredService(); var materialSkinManager = MaterialSkinManager.Instance; materialSkinManager.AddFormToManage(form); diff --git a/WatchList.WinForms/WatchList.WinForms.csproj b/WatchList.WinForms/WatchList.WinForms.csproj index 0783b31b..d0e8ad5a 100644 --- a/WatchList.WinForms/WatchList.WinForms.csproj +++ b/WatchList.WinForms/WatchList.WinForms.csproj @@ -14,6 +14,7 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive