Exemplo n.º 1
0
        private static void RegisterServices(IServiceCollection services, Action <LogDashboardOptions> func = null, Assembly currentAssembly = null)
        {
            // razor
            var razorBuilder = new RazorLightEngineBuilder();

            if (currentAssembly != null)
            {
                razorBuilder = razorBuilder.SetOperatingAssembly(currentAssembly);
            }

            services.AddSingleton <IRazorLightEngine>(razorBuilder
                                                      .UseEmbeddedResourcesProject(typeof(LogDashboardMiddleware))
                                                      .UseMemoryCachingProvider()
                                                      .Build());

            services.AddSingleton(typeof(ILogDashboardCacheManager <>), typeof(InMemoryLogDashboardCacheManager <>));

            // options
            var options = new LogDashboardOptions();

            func?.Invoke(options);

            services.AddSingleton(options);

            if (options.DatabaseSource)
            {
                DapperExtensions.DapperAsyncExtensions.DefaultMapper = typeof(LogModelMapper <>);
                DapperExtensions.DapperExtensions.DefaultMapper      = typeof(LogModelMapper <>);

                if (string.IsNullOrWhiteSpace(options.ConnectionString))
                {
                    throw new ArgumentNullException(nameof(options.ConnectionString));
                }

                services.AddTransient(provider => new SqlConnection(options.ConnectionString));

                services.AddTransient(typeof(IRepository <>), typeof(DapperRepository <>));

                services.AddScoped <IUnitOfWork, DapperUnitOfWork>();
            }
            else
            {
                services.AddTransient(typeof(IRepository <>), typeof(FileRepository <>));;

                services.AddScoped(typeof(IUnitOfWork), typeof(FileUnitOfWork <>).MakeGenericType(options.LogModelType));
            }


            //register Handle
            RegisterHandle(services, options);
        }