Projekt

Obecné

Profil

Stáhnout (4.69 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
        /// 
48
        /// </summary>
49
        /// <param name="services"></param>
50
        public DatabaseInMemoryFixture(ServiceCollection services)
51
        {
52
            Log.Logger = new LoggerConfiguration()
53
                .WriteTo.Console()
54
                .CreateLogger();
55

    
56
            Utilities.LoadExtensions();
57
            ConfigureServices(services);
58
            Services = services.BuildServiceProvider();
59

    
60
            // Assign shortcuts accessors to registered components
61
            UserManager = Services.GetRequiredService<UserManager<ApplicationUser>>();
62
            RoleManager = Services.GetRequiredService<RoleManager<ApplicationRole>>();
63

    
64
            // Storage: create using registered db context / storage context
65
            Storage = (TestingStorageContext)Services.GetRequiredService<IStorageContext>();
66
        }
67

    
68
        /// <summary>
69
        /// 
70
        /// </summary>
71
        public void Dispose() { }
72

    
73
        private void ConfigureServices(IServiceCollection services)
74
        {
75
            //Interceptors
76
            services.AddScoped<AuditInterceptor>();
77
            services.AddScoped<DomainEventsInterceptor>();
78

    
79
            // DbContext/IStorageContext
80
            services.AddDbContext<TestingStorageContext>((serviceProvider, options) =>
81
            {
82
                options.UseInMemoryDatabase("TestingInMemoryDatabase");
83
                var auditInterceptor = serviceProvider.GetService<AuditInterceptor>();
84
                var domainEventsInterceptor = serviceProvider.GetService<DomainEventsInterceptor>();
85
                options.AddInterceptors(auditInterceptor, domainEventsInterceptor);
86
            });
87

    
88
            // Register UserManager & RoleManager
89
            services.AddIdentity<ApplicationUser, ApplicationRole>()
90
               .AddEntityFrameworkStores<TestingStorageContext>()
91
               .AddDefaultTokenProviders();
92

    
93
            // UserManager & RoleManager require logging and HttpContext dependencies
94
            services.AddLogging();
95
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
96
            services.AddAutoMapper(ExtensionManager.Assemblies);
97
            services.AddValidatorsFromAssemblies(ExtensionManager.Assemblies);
98
            services.AddMediatR(ExtensionManager.Assemblies.ToArray());
99
            services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
100

    
101
            // Register database-specific storage context implementation.
102
            services.AddScoped<IStorageContext, TestingStorageContext>();
103

    
104
        }
105

    
106
        /// <summary>
107
        /// For use by DbContextFactory.
108
        /// </summary>
109
        /// <returns></returns>
110
        public static DbContextOptionsBuilder<TestingStorageContext> GetDbContextOptionsBuilder()
111
        {
112
            var optionsBuilder = new DbContextOptionsBuilder<TestingStorageContext>();
113
            optionsBuilder.UseInMemoryDatabase("TestingInMemoryDatabase");
114
            return optionsBuilder;
115
        }
116

    
117
        /// <summary>
118
        /// Utility class to use the IOptions pattern as required by IStorage implementations constructors.
119
        /// </summary>
120
        private class TestStorageContextOptions : IOptions<StorageContextOptions>
121
        {
122
            public TestStorageContextOptions(StorageContextOptions value_)
123
            {
124
                Value = value_;
125
            }
126

    
127
            public StorageContextOptions Value { get; }
128
        }
129
    }
130
}
(1-1/6)