Projekt

Obecné

Profil

Stáhnout (4.54 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<DomainEventsInterceptor>();
77

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

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

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

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

    
102
        }
103

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

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

    
125
            public StorageContextOptions Value { get; }
126
        }
127
    }
128
}
(1-1/6)