diff --git a/Samples/TardisBank/Src/Suteki.TardisBank.Tasks/IUserService.cs b/Samples/TardisBank/Src/Suteki.TardisBank.Tasks/IUserService.cs index 57e660ce..79255323 100644 --- a/Samples/TardisBank/Src/Suteki.TardisBank.Tasks/IUserService.cs +++ b/Samples/TardisBank/Src/Suteki.TardisBank.Tasks/IUserService.cs @@ -8,7 +8,7 @@ namespace Suteki.TardisBank.Tasks public interface IUserService { Task GetCurrentUser(CancellationToken cancellationToken = default); - Task GetUser(int userId, CancellationToken cancellationToken = default); + Task GetUser(int userId, CancellationToken cancellationToken = default); Task GetUserByUserName(string userName, CancellationToken cancellationToken = default); Task GetUserByActivationKey(string activationKey, CancellationToken cancellationToken = default); Task SaveUser(User user, CancellationToken cancellationToken = default); diff --git a/Samples/TardisBank/Src/Suteki.TardisBank.Tasks/UserService.cs b/Samples/TardisBank/Src/Suteki.TardisBank.Tasks/UserService.cs index e5eb1414..39a4b4d1 100644 --- a/Samples/TardisBank/Src/Suteki.TardisBank.Tasks/UserService.cs +++ b/Samples/TardisBank/Src/Suteki.TardisBank.Tasks/UserService.cs @@ -32,7 +32,7 @@ public UserService(IHttpContextService context, ILinqRepository par return GetUserByUserName(_context.UserName, cancellationToken); } - public Task GetUser(int userId, CancellationToken cancellationToken) + public Task GetUser(int userId, CancellationToken cancellationToken) { return _userRepository.FindOneAsync(userId, cancellationToken); } diff --git a/Samples/TardisBank/Src/Suteki.TardisBank.Tests/Model/TransientDatabaseSetup.cs b/Samples/TardisBank/Src/Suteki.TardisBank.Tests/Model/TransientDatabaseSetup.cs index cd2b74bf..32062db7 100644 --- a/Samples/TardisBank/Src/Suteki.TardisBank.Tests/Model/TransientDatabaseSetup.cs +++ b/Samples/TardisBank/Src/Suteki.TardisBank.Tests/Model/TransientDatabaseSetup.cs @@ -8,7 +8,7 @@ public class TransientDatabaseSetup : TestDatabaseSetup { /// public TransientDatabaseSetup() - : base(typeof(TransientDatabaseSetup).Assembly, new[] + : base(typeof(TransientDatabaseSetup).Assembly, typeof(AutoPersistenceModelGenerator), new[] { typeof(AutoPersistenceModelGenerator).Assembly }) diff --git a/SharpArch.AutoLoad.DotSettings b/SharpArch.AutoLoad.DotSettings index 5706dde3..b574c586 100644 --- a/SharpArch.AutoLoad.DotSettings +++ b/SharpArch.AutoLoad.DotSettings @@ -440,6 +440,7 @@ FluentAssertions.AssertionExtensions.Should($EXPR$).NotBeNull() True True True + True \ No newline at end of file diff --git a/Src/Common/NHibernate/TestDatabaseSetup.cs b/Src/Common/NHibernate/TestDatabaseSetup.cs index 683eecdf..6c758c7e 100644 --- a/Src/Common/NHibernate/TestDatabaseSetup.cs +++ b/Src/Common/NHibernate/TestDatabaseSetup.cs @@ -28,23 +28,51 @@ public class TestDatabaseSetup : IDisposable { readonly string _basePath; readonly Assembly[] _mappingAssemblies; + readonly Type _persistenceModelGenerator; Configuration? _configuration; ISessionFactory? _sessionFactory; + /// + /// Allows to apply custom configuration to new ISession. + /// + /// + /// Callback is executed by default implementation of from . + /// + public Action? SessionConfigurator { get; set; } + + /// + /// Allows to apply custom configuration to new ISession. + /// + /// + /// Callback is executed by default implementation of from . + /// + public Action? StatelessSessionConfigurator { get; set; } + /// /// Initializes a new instance of the class. /// /// Base bath to use when looking for mapping assemblies and default NHibernate configuration file. + /// Persistence model generator, type. Must implement . /// /// List of assemblies containing NHibernate mapping files and persistence model generator. /// /// - /// or is + /// , or is /// null. /// - public TestDatabaseSetup(string basePath, Assembly[] mappingAssemblies) + /// + /// does not implement . + /// + public TestDatabaseSetup( + string basePath, + Type persistenceModelGenerator, + Assembly[] mappingAssemblies) { _basePath = basePath ?? throw new ArgumentNullException(nameof(basePath)); + _persistenceModelGenerator = persistenceModelGenerator ?? throw new ArgumentNullException(nameof(persistenceModelGenerator)); + if (!typeof(IAutoPersistenceModelGenerator).IsAssignableFrom(persistenceModelGenerator)) + throw new ArgumentException($"Type {persistenceModelGenerator.FullName} must implement {nameof(IAutoPersistenceModelGenerator)}."); + if (mappingAssemblies == null) throw new ArgumentNullException(nameof(mappingAssemblies)); _mappingAssemblies = mappingAssemblies.Distinct().ToArray(); } @@ -52,6 +80,7 @@ public TestDatabaseSetup(string basePath, Assembly[] mappingAssemblies) /// /// Initializes a new instance of the class. /// + /// Persistence model generator, type. Must implement . /// /// Assembly to use to determine configuration folder. Typically is it assembly containing /// tests. @@ -61,28 +90,19 @@ public TestDatabaseSetup(string basePath, Assembly[] mappingAssemblies) /// or is /// null. /// - public TestDatabaseSetup(Assembly baseAssembly, Assembly[] mappingAssemblies) + /// + /// does not implement . + /// + public TestDatabaseSetup( + Assembly baseAssembly, + Type persistenceModelGenerator, + Assembly[] mappingAssemblies) : this(CodeBaseLocator.GetAssemblyCodeBasePath(baseAssembly), + persistenceModelGenerator, mappingAssemblies) { } - /// - /// Allows to apply custom configuration to new ISession. - /// - /// - /// Callback is executed by default implementation of from . - /// - public Action? SessionConfigurator { get; set; } - - /// - /// Allows to apply custom configuration to new ISession. - /// - /// - /// Callback is executed by default implementation of from . - /// - public Action? StatelessSessionConfigurator { get; set; } - /// /// Disposes SessionFactory. /// @@ -94,64 +114,15 @@ public virtual void Dispose() GC.SuppressFinalize(this); } - /// - /// Generates auto-persistence model. - /// - /// List of assemblies to look for auto-persistence model generators. - /// - /// - /// - /// - /// This method will load and scan assemblies for . - /// Only first generated model is returned. - /// - /// is - /// - /// Only one implementation of is - /// allowed. - /// - /// Unable to instantiate AutoPersistenceModelGenerator. - - public static AutoPersistenceModel GenerateAutoPersistenceModel(Assembly[] assemblies) - { - if (assemblies == null) throw new ArgumentNullException(nameof(assemblies)); - var persistenceGeneratorTypes = assemblies.SelectMany(a => - a.GetTypes().Where(t => - !t.IsAbstract && typeof(IAutoPersistenceModelGenerator).IsAssignableFrom(t))) - .ToArray(); - if (persistenceGeneratorTypes.Length > 1) - throw new InvalidOperationException($"Found multiple classes implementing {nameof(IAutoPersistenceModelGenerator)}. " + - "Only one persistence model generator is supported.") - { - Data = - { - [nameof(IAutoPersistenceModelGenerator) + "s"] = persistenceGeneratorTypes, - ["Assemblies"] = assemblies - } - }; - if (persistenceGeneratorTypes.Length == 0) - throw new InvalidOperationException($"No classes implementing {nameof(IAutoPersistenceModelGenerator)} were found. " + - $"{nameof(TestDatabaseSetup)} requires persistence model generator to create test database.") - { - Data = - { - ["Assemblies"] = assemblies - } - }; - var generator = (IAutoPersistenceModelGenerator) Activator.CreateInstance(persistenceGeneratorTypes[0])!; - return generator.Generate(); - } - /// /// Returns NHibernate . /// Configuration instance is cached, all subsequent calls will return the same instance. /// - public Configuration GetConfiguration() { if (_configuration != null) return _configuration; - var autoPersistenceModel = GenerateAutoPersistenceModel(_mappingAssemblies); + var autoPersistenceModel = GenerateAutoPersistenceModel(); var builder = new NHibernateSessionFactoryBuilder() .AddMappingAssemblies(_mappingAssemblies) @@ -171,6 +142,19 @@ public Configuration GetConfiguration() return _configuration; } + /// + /// Generates auto-persistence model. + /// + /// + /// + /// + /// Unable to instantiate AutoPersistenceModelGenerator. + public AutoPersistenceModel GenerateAutoPersistenceModel() + { + var generator = (IAutoPersistenceModelGenerator)Activator.CreateInstance(_persistenceModelGenerator)!; + return generator.Generate(); + } + /// /// Override this method to customize NHibernate configuration. /// @@ -256,7 +240,7 @@ public static void Close(ISession? session) /// /// The session factory. /// - /// Dispose will destroy Session Factory associated with this instance. + /// Disposing will destroy Session Factory associated with this instance. /// public static void Shutdown(ISessionFactory? sessionFactory) => sessionFactory?.Dispose(); diff --git a/Src/SharpArch.Testing.NUnit/NHibernate/RepositoryTestsBase.cs b/Src/SharpArch.Testing.NUnit/NHibernate/RepositoryTestsBase.cs index 9f2eb606..5f08c751 100644 --- a/Src/SharpArch.Testing.NUnit/NHibernate/RepositoryTestsBase.cs +++ b/Src/SharpArch.Testing.NUnit/NHibernate/RepositoryTestsBase.cs @@ -8,6 +8,7 @@ using global::NUnit.Framework; using JetBrains.Annotations; using SharpArch.NHibernate; + using SharpArch.NHibernate.FluentNHibernate; using Testing.NHibernate; diff --git a/Src/SharpArch.Testing.Xunit.NHibernate/LiveDatabaseTests.cs b/Src/SharpArch.Testing.Xunit.NHibernate/LiveDatabaseTests.cs index 0fd389ae..6303e41c 100644 --- a/Src/SharpArch.Testing.Xunit.NHibernate/LiveDatabaseTests.cs +++ b/Src/SharpArch.Testing.Xunit.NHibernate/LiveDatabaseTests.cs @@ -4,6 +4,7 @@ using global::NHibernate; using global::Xunit; using JetBrains.Annotations; + using SharpArch.NHibernate.FluentNHibernate; using SharpArch.Testing.NHibernate; diff --git a/Src/SharpArch.Testing.Xunit.NHibernate/TransientDatabaseTests.cs b/Src/SharpArch.Testing.Xunit.NHibernate/TransientDatabaseTests.cs index 0e8f7042..94752571 100644 --- a/Src/SharpArch.Testing.Xunit.NHibernate/TransientDatabaseTests.cs +++ b/Src/SharpArch.Testing.Xunit.NHibernate/TransientDatabaseTests.cs @@ -21,6 +21,7 @@ /// such as a SQL Server instance, then use instead. /// /// + /// Database setup class. [PublicAPI] public abstract class TransientDatabaseTests : IClassFixture, IDisposable, IAsyncLifetime where TDatabaseSetup : TestDatabaseSetup, new() diff --git a/Src/Tests/SharpArch.Tests.NHibernate/HasUniqieDomainSignatureTestsBase.cs b/Src/Tests/SharpArch.Tests.NHibernate/HasUniqieDomainSignatureTestsBase.cs index 16d2eb5d..ae8f5ca7 100644 --- a/Src/Tests/SharpArch.Tests.NHibernate/HasUniqieDomainSignatureTestsBase.cs +++ b/Src/Tests/SharpArch.Tests.NHibernate/HasUniqieDomainSignatureTestsBase.cs @@ -17,7 +17,9 @@ class HasUniqueDomainSignatureTestsBase : RepositoryTestsBase public HasUniqueDomainSignatureTestsBase() : base(new TestDatabaseSetup( - TestContext.CurrentContext.TestDirectory, new[] {typeof(TestsPersistenceModelGenerator).Assembly} + TestContext.CurrentContext.TestDirectory, + typeof(TestsPersistenceModelGenerator), + new[] {typeof(TestsPersistenceModelGenerator).Assembly} )) { } diff --git a/Src/Tests/SharpArch.XunitTests.NHibernate/HasUniqieDomainSignatureTestsBase.cs b/Src/Tests/SharpArch.XunitTests.NHibernate/HasUniqieDomainSignatureTestsBase.cs index b080f05c..4f67ebba 100644 --- a/Src/Tests/SharpArch.XunitTests.NHibernate/HasUniqieDomainSignatureTestsBase.cs +++ b/Src/Tests/SharpArch.XunitTests.NHibernate/HasUniqieDomainSignatureTestsBase.cs @@ -5,6 +5,7 @@ using global::SharpArch.Domain.PersistenceSupport; using global::SharpArch.NHibernate; using global::SharpArch.Testing.Xunit.NHibernate; + using Mappings; using Moq; diff --git a/Src/Tests/SharpArch.XunitTests.NHibernate/NHibernateRepositoryTests.cs b/Src/Tests/SharpArch.XunitTests.NHibernate/NHibernateRepositoryTests.cs index 99ff6fd0..5819203f 100644 --- a/Src/Tests/SharpArch.XunitTests.NHibernate/NHibernateRepositoryTests.cs +++ b/Src/Tests/SharpArch.XunitTests.NHibernate/NHibernateRepositoryTests.cs @@ -6,6 +6,7 @@ using FluentAssertions; using global::SharpArch.NHibernate; using global::SharpArch.Testing.Xunit.NHibernate; + using Mappings; using Xunit; diff --git a/Src/Tests/SharpArch.XunitTests.NHibernate/NHibernateTestsSetup.cs b/Src/Tests/SharpArch.XunitTests.NHibernate/NHibernateTestsSetup.cs index 6bcee281..78dc4444 100644 --- a/Src/Tests/SharpArch.XunitTests.NHibernate/NHibernateTestsSetup.cs +++ b/Src/Tests/SharpArch.XunitTests.NHibernate/NHibernateTestsSetup.cs @@ -14,10 +14,11 @@ public class NHibernateTestsSetup : TestDatabaseSetup { public NHibernateTestsSetup() : base(Assembly.GetExecutingAssembly().Location, + typeof(TestsPersistenceModelGenerator), new[] { typeof(ObjectWithGuidId).Assembly, - typeof(TestsPersistenceModelGenerator).Assembly + //typeof(TestsPersistenceModelGenerator).Assembly }) { } @@ -27,7 +28,7 @@ protected override void Customize(NHibernateSessionFactoryBuilder builder) { base.Customize(builder); builder.UsePersistenceConfigurer(new SQLiteConfiguration().InMemory()); - builder.UseProperties(new SortedList() + builder.UseProperties(new SortedList { [Environment.ReleaseConnections] = "on_close", [Environment.Hbm2ddlAuto] = "create"