EF: refactoring

This commit is contained in:
pavelbannov 2019-12-10 18:06:51 +03:00
parent aed98bea9a
commit 5d7f58cc2c
10 changed files with 25 additions and 63 deletions

View File

@ -1,8 +1,7 @@
using ASC.Core.Common.EF.Model; using ASC.Core.Common.EF.Model;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace ASC.Core.Common.EF.Context namespace ASC.Core.Common.EF.Context
{ {
@ -20,11 +19,7 @@ namespace ASC.Core.Common.EF.Context
{ {
public static IServiceCollection AddAccountLinkContextService(this IServiceCollection services) public static IServiceCollection AddAccountLinkContextService(this IServiceCollection services)
{ {
services.TryAddScoped<DbContextManager<AccountLinkContext>>(); return services.AddDbContextManagerService<AccountLinkContext>();
services.TryAddScoped<IConfigureOptions<AccountLinkContext>, ConfigureDbContext>();
services.TryAddScoped<AccountLinkContext>();
return services;
} }
} }
} }

View File

@ -1,7 +1,5 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace ASC.Core.Common.EF namespace ASC.Core.Common.EF
{ {
@ -29,11 +27,7 @@ namespace ASC.Core.Common.EF
{ {
public static IServiceCollection AddCoreDbContextService(this IServiceCollection services) public static IServiceCollection AddCoreDbContextService(this IServiceCollection services)
{ {
services.TryAddScoped<DbContextManager<CoreDbContext>>(); return services.AddDbContextManagerService<CoreDbContext>();
services.TryAddScoped<IConfigureOptions<CoreDbContext>, ConfigureDbContext>();
services.TryAddScoped<CoreDbContext>();
return services;
} }
} }
} }

View File

@ -1,6 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
namespace ASC.Core.Common.EF namespace ASC.Core.Common.EF
@ -53,4 +55,16 @@ namespace ASC.Core.Common.EF
} }
} }
} }
public static class DbContextManagerExtension
{
public static IServiceCollection AddDbContextManagerService<T>(this IServiceCollection services) where T : BaseDbContext, new()
{
services.TryAddScoped<DbContextManager<T>>();
services.TryAddScoped<IConfigureOptions<T>, ConfigureDbContext>();
services.TryAddScoped<T>();
return services;
}
}
} }

View File

@ -2,8 +2,6 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace ASC.Core.Common.EF.Context namespace ASC.Core.Common.EF.Context
{ {
@ -26,11 +24,7 @@ namespace ASC.Core.Common.EF.Context
{ {
public static IServiceCollection AddFeedDbService(this IServiceCollection services) public static IServiceCollection AddFeedDbService(this IServiceCollection services)
{ {
services.TryAddScoped<DbContextManager<FeedDbContext>>(); return services.AddDbContextManagerService<FeedDbContext>();
services.TryAddScoped<IConfigureOptions<FeedDbContext>, ConfigureDbContext>();
services.TryAddScoped<FeedDbContext>();
return services;
} }
} }
} }

View File

@ -2,8 +2,6 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace ASC.Core.Common.EF.Context namespace ASC.Core.Common.EF.Context
{ {
@ -21,11 +19,7 @@ namespace ASC.Core.Common.EF.Context
{ {
public static IServiceCollection AddFilesDbContextService(this IServiceCollection services) public static IServiceCollection AddFilesDbContextService(this IServiceCollection services)
{ {
services.TryAddScoped<DbContextManager<FilesDbContext>>(); return services.AddDbContextManagerService<FilesDbContext>();
services.TryAddScoped<IConfigureOptions<FilesDbContext>, ConfigureDbContext>();
services.TryAddScoped<FilesDbContext>();
return services;
} }
} }
} }

View File

@ -1,8 +1,7 @@
using ASC.Core.Common.EF.Model; using ASC.Core.Common.EF.Model;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace ASC.Core.Common.EF.Context namespace ASC.Core.Common.EF.Context
{ {
@ -23,11 +22,7 @@ namespace ASC.Core.Common.EF.Context
{ {
public static IServiceCollection AddMessagesContextService(this IServiceCollection services) public static IServiceCollection AddMessagesContextService(this IServiceCollection services)
{ {
services.TryAddScoped<DbContextManager<MessagesContext>>(); return services.AddDbContextManagerService<MessagesContext>();
services.TryAddScoped<IConfigureOptions<MessagesContext>, ConfigureDbContext>();
services.TryAddScoped<MessagesContext>();
return services;
} }
} }
} }

View File

@ -2,8 +2,6 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace ASC.Core.Common.EF.Context namespace ASC.Core.Common.EF.Context
{ {
@ -17,11 +15,7 @@ namespace ASC.Core.Common.EF.Context
{ {
public static IServiceCollection AddNotifyDbContext(this IServiceCollection services) public static IServiceCollection AddNotifyDbContext(this IServiceCollection services)
{ {
services.TryAddScoped<DbContextManager<NotifyDbContext>>(); return services.AddDbContextManagerService<NotifyDbContext>();
services.TryAddScoped<IConfigureOptions<NotifyDbContext>, ConfigureDbContext>();
services.TryAddScoped<NotifyDbContext>();
return services;
} }
} }
} }

View File

@ -2,8 +2,6 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace ASC.Core.Common.EF.Context namespace ASC.Core.Common.EF.Context
{ {
@ -36,11 +34,7 @@ namespace ASC.Core.Common.EF.Context
{ {
public static IServiceCollection AddTenantDbContextService(this IServiceCollection services) public static IServiceCollection AddTenantDbContextService(this IServiceCollection services)
{ {
services.TryAddScoped<DbContextManager<TenantDbContext>>(); return services.AddDbContextManagerService<TenantDbContext>();
services.TryAddScoped<IConfigureOptions<TenantDbContext>, ConfigureDbContext>();
services.TryAddScoped<TenantDbContext>();
return services;
} }
} }
} }

View File

@ -1,7 +1,5 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace ASC.Core.Common.EF namespace ASC.Core.Common.EF
{ {
@ -34,11 +32,7 @@ namespace ASC.Core.Common.EF
{ {
public static IServiceCollection AddUserDbContextService(this IServiceCollection services) public static IServiceCollection AddUserDbContextService(this IServiceCollection services)
{ {
services.TryAddScoped<DbContextManager<UserDbContext>>(); return services.AddDbContextManagerService<UserDbContext>();
services.TryAddScoped<IConfigureOptions<UserDbContext>, ConfigureDbContext>();
services.TryAddScoped<UserDbContext>();
return services;
} }
} }
} }

View File

@ -2,8 +2,6 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace ASC.Core.Common.EF.Context namespace ASC.Core.Common.EF.Context
{ {
@ -24,11 +22,7 @@ namespace ASC.Core.Common.EF.Context
{ {
public static IServiceCollection AddWebstudioDbContextService(this IServiceCollection services) public static IServiceCollection AddWebstudioDbContextService(this IServiceCollection services)
{ {
services.TryAddScoped<DbContextManager<WebstudioDbContext>>(); return services.AddDbContextManagerService<WebstudioDbContext>();
services.TryAddScoped<IConfigureOptions<WebstudioDbContext>, ConfigureDbContext>();
services.TryAddScoped<WebstudioDbContext>();
return services;
} }
} }
} }