Projekt

Obecné

Profil

Stáhnout (4.95 KB) Statistiky
| Větev: | Tag: | Revize:
1
using ExtCore.Data.Abstractions;
2
using ExtCore.Data.EntityFramework;
3
using ExtCore.Infrastructure;
4
using FluentValidation;
5
using Leuze.Core.Application.Behaviors;
6
using Leuze.Core.Application.Identity;
7
using Leuze.Core.Infrastructure.Persistence.Interceptors;
8
using MediatR;
9
using Microsoft.AspNetCore.Http;
10
using Microsoft.AspNetCore.Identity;
11
using Microsoft.EntityFrameworkCore;
12
using Microsoft.Extensions.DependencyInjection;
13
using Microsoft.Extensions.Logging;
14
using Microsoft.Extensions.Options;
15
using Serilog;
16
using System;
17
using System.Linq;
18

    
19
namespace Leuze.Tests.Configuration
20
{
21
    /// <summary>
22
    /// 
23
    /// </summary>
24
    public class DatabaseInMemoryFixture : IDisposable
25
    {
26
        /// <summary>
27
        /// Provider for registered services for fixture
28
        /// </summary>
29
        public IServiceProvider Services { get; private set; }
30

    
31
        /// <summary>
32
        /// Instance of database context
33
        /// </summary>
34
        public TestingStorageContext Storage { get; private set; }
35

    
36
        /// <summary>
37
        /// Instance of user manager
38
        /// </summary>
39
        public UserManager<ApplicationUser> UserManager { get; private set; }
40

    
41
        /// <summary>
42
        /// Instance of role manager
43
        /// </summary>
44
        public RoleManager<ApplicationRole> RoleManager { get; private set; }
45

    
46
        /// <summary>
47
        /// Instance of role manager
48
        /// </summary>
49
        public SignInManager<ApplicationUser> SignInManager { get; private set; }
50

    
51
        /// <summary>
52
        /// 
53
        /// </summary>
54
        /// <param name="services"></param>
55
        public DatabaseInMemoryFixture(ServiceCollection services)
56
        {
57
            Log.Logger = new LoggerConfiguration()
58
                .WriteTo.Console()
59
                .CreateLogger();
60

    
61
            Utilities.LoadExtensions();
62
            ConfigureServices(services);
63
            Services = services.BuildServiceProvider();
64

    
65
            // Assign shortcuts accessors to registered components
66
            UserManager = Services.GetRequiredService<UserManager<ApplicationUser>>();
67
            RoleManager = Services.GetRequiredService<RoleManager<ApplicationRole>>();
68
            SignInManager = Services.GetRequiredService<SignInManager<ApplicationUser>>();
69

    
70
            // Storage: create using registered db context / storage context
71
            Storage = (TestingStorageContext)Services.GetRequiredService<IStorageContext>();
72
        }
73

    
74
        /// <summary>
75
        /// 
76
        /// </summary>
77
        public void Dispose() { }
78

    
79
        private void ConfigureServices(IServiceCollection services)
80
        {
81
            //Interceptors
82
            services.AddScoped<AuditInterceptor>();
83
            services.AddScoped<DomainEventsInterceptor>();
84

    
85
            // DbContext/IStorageContext
86
            services.AddDbContext<TestingStorageContext>((serviceProvider, options) =>
87
            {
88
                options.UseInMemoryDatabase("TestingInMemoryDatabase");
89
                var auditInterceptor = serviceProvider.GetService<AuditInterceptor>();
90
                var domainEventsInterceptor = serviceProvider.GetService<DomainEventsInterceptor>();
91
                options.AddInterceptors(auditInterceptor, domainEventsInterceptor);
92
            });
93

    
94
            // Register UserManager & RoleManager
95
            services.AddIdentity<ApplicationUser, ApplicationRole>()
96
               .AddEntityFrameworkStores<TestingStorageContext>()
97
               .AddDefaultTokenProviders();
98

    
99
            // UserManager & RoleManager require logging and HttpContext dependencies
100
            services.AddLogging();
101
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
102
            services.AddAutoMapper(ExtensionManager.Assemblies);
103
            services.AddValidatorsFromAssemblies(ExtensionManager.Assemblies);
104
            services.AddMediatR(ExtensionManager.Assemblies.ToArray());
105
            services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
106

    
107
            // Register database-specific storage context implementation.
108
            services.AddScoped<IStorageContext, TestingStorageContext>();
109

    
110

    
111

    
112
        }
113

    
114
        /// <summary>
115
        /// For use by DbContextFactory.
116
        /// </summary>
117
        /// <returns></returns>
118
        public static DbContextOptionsBuilder<TestingStorageContext> GetDbContextOptionsBuilder()
119
        {
120
            var optionsBuilder = new DbContextOptionsBuilder<TestingStorageContext>();
121
            optionsBuilder.UseInMemoryDatabase("TestingInMemoryDatabase");
122
            return optionsBuilder;
123
        }
124

    
125
        /// <summary>
126
        /// Utility class to use the IOptions pattern as required by IStorage implementations constructors.
127
        /// </summary>
128
        private class TestStorageContextOptions : IOptions<StorageContextOptions>
129
        {
130
            public TestStorageContextOptions(StorageContextOptions value_)
131
            {
132
                Value = value_;
133
            }
134

    
135
            public StorageContextOptions Value { get; }
136
        }
137
    }
138
}
(1-1/6)