From e7deda4d5d82cb27540beef75dc030224a1e7344 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sat, 16 Feb 2013 21:09:35 -0800 Subject: [PATCH] added open generic registration for simple repository, services can now use simple repository independently. --- NzbDrone.Core.Test/Framework/ObjectDbTest.cs | 14 +++++++------- .../FreeSpaceOnDrivesFixture.cs | 9 +++++---- .../RootFolderServiceFixture.cs | 11 ++++++----- NzbDrone.Core/ContainerExtentions.cs | 2 ++ NzbDrone.Core/Datastore/BasicRepository.cs | 6 ++++-- .../Datastore/Migrations/Migration20130203.cs | 5 +---- NzbDrone.Core/NzbDrone.Core.csproj | 1 - .../RootFolders/RootFolderRepository.cs | 19 ------------------- .../RootFolders/RootFolderService.cs | 6 +++--- 9 files changed, 28 insertions(+), 45 deletions(-) delete mode 100644 NzbDrone.Core/RootFolders/RootFolderRepository.cs diff --git a/NzbDrone.Core.Test/Framework/ObjectDbTest.cs b/NzbDrone.Core.Test/Framework/ObjectDbTest.cs index a4787696d5..9fa6347c84 100644 --- a/NzbDrone.Core.Test/Framework/ObjectDbTest.cs +++ b/NzbDrone.Core.Test/Framework/ObjectDbTest.cs @@ -50,14 +50,14 @@ protected IObjectDatabase Db protected void WithObjectDb(bool memory = true) { - if (memory) - { - _db = new SiaqoDbFactory(new DiskProvider(),new EnvironmentProvider()).CreateMemoryDb(); - } - else - { + //if (memory) + //{ + // _db = new SiaqoDbFactory(new DiskProvider(),new EnvironmentProvider()).CreateMemoryDb(); + //} + //else + //{ _db = new SiaqoDbFactory(new DiskProvider(),new EnvironmentProvider()).Create(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Guid.NewGuid().ToString())); - } + //} Mocker.SetConstant(Db); } diff --git a/NzbDrone.Core.Test/RootFolderTests/FreeSpaceOnDrivesFixture.cs b/NzbDrone.Core.Test/RootFolderTests/FreeSpaceOnDrivesFixture.cs index 2b1530524a..beffa0c234 100644 --- a/NzbDrone.Core.Test/RootFolderTests/FreeSpaceOnDrivesFixture.cs +++ b/NzbDrone.Core.Test/RootFolderTests/FreeSpaceOnDrivesFixture.cs @@ -8,6 +8,7 @@ using Moq; using NUnit.Framework; using NzbDrone.Common; +using NzbDrone.Core.Datastore; using NzbDrone.Core.RootFolders; using NzbDrone.Core.Test.Framework; using NzbDrone.Test.Common; @@ -21,7 +22,7 @@ public class FreeSpaceOnDrivesFixture : CoreTest [Test] public void should_return_one_drive_when_only_one_root_dir_exists() { - Mocker.GetMock() + Mocker.GetMock>() .Setup(s => s.All()) .Returns(new List { new RootFolder { OID = 1, Path = @"C:\Test\TV" } }); @@ -41,7 +42,7 @@ public void should_return_one_drive_when_only_one_root_dir_exists() [Test] public void should_return_one_drive_when_two_rootDirs_on_the_same_drive_exist() { - Mocker.GetMock() + Mocker.GetMock>() .Setup(s => s.All()) .Returns(new List { new RootFolder { OID = 1, Path = @"C:\Test\TV" }, new RootFolder { OID = 2, Path = @"C:\Test\TV2" }}); @@ -62,7 +63,7 @@ public void should_return_one_drive_when_two_rootDirs_on_the_same_drive_exist() [Test] public void should_return_two_drives_when_two_rootDirs_on_the_different_drive_exist() { - Mocker.GetMock() + Mocker.GetMock>() .Setup(s => s.All()) .Returns(new List { new RootFolder { OID = 1, Path = @"C:\Test\TV" }, new RootFolder { OID = 2, Path = @"D:\Test\TV" }}); @@ -87,7 +88,7 @@ public void should_return_two_drives_when_two_rootDirs_on_the_different_drive_ex [Test] public void should_skip_rootDir_if_not_found_on_disk() { - Mocker.GetMock() + Mocker.GetMock>() .Setup(s => s.All()) .Returns(new List { new RootFolder { OID = 1, Path = @"C:\Test\TV" } }); diff --git a/NzbDrone.Core.Test/RootFolderTests/RootFolderServiceFixture.cs b/NzbDrone.Core.Test/RootFolderTests/RootFolderServiceFixture.cs index 9af9700139..157f8f0bb1 100644 --- a/NzbDrone.Core.Test/RootFolderTests/RootFolderServiceFixture.cs +++ b/NzbDrone.Core.Test/RootFolderTests/RootFolderServiceFixture.cs @@ -8,6 +8,7 @@ using Moq; using NUnit.Framework; using NzbDrone.Common; +using NzbDrone.Core.Datastore; using NzbDrone.Core.RootFolders; using NzbDrone.Core.Test.Framework; @@ -24,7 +25,7 @@ public void Setup() .Setup(m => m.FolderExists(It.IsAny())) .Returns(true); - Mocker.GetMock() + Mocker.GetMock>() .Setup(s => s.All()) .Returns(new List()); } @@ -44,7 +45,7 @@ public void should_be_able_to_add_root_dir(string path) Subject.Add(root); - Mocker.GetMock().Verify(c => c.Add(root), Times.Once()); + Mocker.GetMock>().Verify(c => c.Add(root), Times.Once()); } [Test] @@ -59,14 +60,14 @@ public void should_throw_if_folder_being_added_doesnt_exist() public void should_be_able_to_remove_root_dir() { Subject.Remove(1); - Mocker.GetMock().Verify(c => c.Delete(1), Times.Once()); + Mocker.GetMock>().Verify(c => c.Delete(1), Times.Once()); } public void None_existing_folder_returns_empty_list() { WithNoneExistingFolder(); - Mocker.GetMock().Setup(c => c.All()).Returns(new List()); + Mocker.GetMock>().Setup(c => c.All()).Returns(new List()); const string path = "d:\\bad folder"; @@ -96,7 +97,7 @@ public void invalid_folder_path_throws_on_add(string path) [Test] public void adding_duplicated_root_folder_should_throw() { - Mocker.GetMock().Setup(c => c.All()).Returns(new List { new RootFolder { Path = "C:\\TV" } }); + Mocker.GetMock>().Setup(c => c.All()).Returns(new List { new RootFolder { Path = "C:\\TV" } }); Assert.Throws(() => Subject.Add(new RootFolder { Path = @"C:\TV" })); } diff --git a/NzbDrone.Core/ContainerExtentions.cs b/NzbDrone.Core/ContainerExtentions.cs index 02bf57731d..e04950fe4d 100644 --- a/NzbDrone.Core/ContainerExtentions.cs +++ b/NzbDrone.Core/ContainerExtentions.cs @@ -85,6 +85,8 @@ private static void InitDatabase(this ContainerBuilder container) return c.Resolve().Create(); }).As().SingleInstance(); + container.RegisterGeneric(typeof(BasicRepository<>)).As(typeof(IBasicRepository<>)); + container.RegisterType().WithParameter(ResolvedParameter.ForNamed("DatabaseTarget")); container.RegisterType().WithParameter(ResolvedParameter.ForNamed("LogProvider")); } diff --git a/NzbDrone.Core/Datastore/BasicRepository.cs b/NzbDrone.Core/Datastore/BasicRepository.cs index 9648514a75..8fdaef4777 100644 --- a/NzbDrone.Core/Datastore/BasicRepository.cs +++ b/NzbDrone.Core/Datastore/BasicRepository.cs @@ -20,14 +20,16 @@ public BasicRepository(IObjectDatabase objectDatabase) protected IObjectDatabase ObjectDatabase { get; private set; } + protected IEnumerable Queryable { get { return ObjectDatabase.AsQueryable(); } } + public List All() { - return ObjectDatabase.AsQueryable().ToList(); + return Queryable.ToList(); } public TModel Get(int id) { - return ObjectDatabase.AsQueryable().Single(c => c.OID == id); + return Queryable.Single(c => c.OID == id); } public TModel Add(TModel model) diff --git a/NzbDrone.Core/Datastore/Migrations/Migration20130203.cs b/NzbDrone.Core/Datastore/Migrations/Migration20130203.cs index 9ba4e24719..29b15c4254 100644 --- a/NzbDrone.Core/Datastore/Migrations/Migration20130203.cs +++ b/NzbDrone.Core/Datastore/Migrations/Migration20130203.cs @@ -12,10 +12,6 @@ protected override void MainDbUpgrade() { var objectDb = GetObjectDb(); - - var rootFolderRepo = new RootFolderRepository(objectDb); - - using (var dataReader = Database.ExecuteQuery("SELECT * from RootDirs")) { var dirs = new List(); @@ -26,6 +22,7 @@ protected override void MainDbUpgrade() } objectDb.InsertMany(dirs); } + //Database.RemoveTable("RootDirs"); } diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 5337846475..cb425b3bcd 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -620,7 +620,6 @@ - diff --git a/NzbDrone.Core/RootFolders/RootFolderRepository.cs b/NzbDrone.Core/RootFolders/RootFolderRepository.cs deleted file mode 100644 index 964e91f30a..0000000000 --- a/NzbDrone.Core/RootFolders/RootFolderRepository.cs +++ /dev/null @@ -1,19 +0,0 @@ -using NzbDrone.Core.Datastore; -using System.Linq; - -namespace NzbDrone.Core.RootFolders -{ - public interface IRootFolderRepository : IBasicRepository - { - - } - - public class RootFolderRepository : BasicRepository, IRootFolderRepository - { - public RootFolderRepository(IObjectDatabase objectDatabase) - : base(objectDatabase) - { - - } - } -} diff --git a/NzbDrone.Core/RootFolders/RootFolderService.cs b/NzbDrone.Core/RootFolders/RootFolderService.cs index 7544cd27ea..e7fd3a6abf 100644 --- a/NzbDrone.Core/RootFolders/RootFolderService.cs +++ b/NzbDrone.Core/RootFolders/RootFolderService.cs @@ -4,8 +4,8 @@ using System.IO; using NLog; using NzbDrone.Common; +using NzbDrone.Core.Datastore; using NzbDrone.Core.Providers; -using NzbDrone.Core.Repository; namespace NzbDrone.Core.RootFolders { @@ -21,11 +21,11 @@ public interface IRootFolderService public class RootFolderService : IRootFolderService { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - private readonly IRootFolderRepository _rootFolderRepository; + private readonly IBasicRepository _rootFolderRepository; private readonly DiskProvider _diskProvider; private readonly SeriesProvider _seriesProvider; - public RootFolderService(IRootFolderRepository rootFolderRepository, SeriesProvider seriesProvider, DiskProvider diskProvider) + public RootFolderService(IBasicRepository rootFolderRepository, SeriesProvider seriesProvider, DiskProvider diskProvider) { _rootFolderRepository = rootFolderRepository; _diskProvider = diskProvider;