-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitOfWorkServiceCollectionExtensions.cs
119 lines (109 loc) · 6.04 KB
/
UnitOfWorkServiceCollectionExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EFCore.UnitOfWork
{
public static class UnitOfWorkServiceCollectionExtensions
{
/// <summary>
/// Registers the unit of work given context as a service in the <see cref="IServiceCollection"/>.
/// </summary>
/// <typeparam name="TContext">The type of the db context.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
/// <returns>The same service collection so that multiple calls can be chained.</returns>
/// <remarks>
/// This method only support one db context, if been called more than once, will throw exception.
/// </remarks>
public static IServiceCollection AddUnitOfWork<TContext>(this IServiceCollection services) where TContext : DbContext
{
services.AddScoped<IRepositoryFactory, UnitOfWork<TContext>>();
// Following has a issue: IUnitOfWork cannot support multiple dbcontext/database,
// that means cannot call AddUnitOfWork<TContext> multiple times.
// Solution: check IUnitOfWork whether or null
services.AddScoped<IUnitOfWork, UnitOfWork<TContext>>();
services.AddScoped<IUnitOfWork<TContext>, UnitOfWork<TContext>>();
return services;
}
/// <summary>
/// Registers the unit of work given context as a service in the <see cref="IServiceCollection"/>.
/// </summary>
/// <typeparam name="TContext1">The type of the db context.</typeparam>
/// <typeparam name="TContext2">The type of the db context.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
/// <returns>The same service collection so that multiple calls can be chained.</returns>
/// <remarks>
/// This method only support one db context, if been called more than once, will throw exception.
/// </remarks>
public static IServiceCollection AddUnitOfWork<TContext1, TContext2>(this IServiceCollection services)
where TContext1 : DbContext
where TContext2 : DbContext
{
services.AddScoped<IUnitOfWork<TContext1>, UnitOfWork<TContext1>>();
services.AddScoped<IUnitOfWork<TContext2>, UnitOfWork<TContext2>>();
return services;
}
/// <summary>
/// Registers the unit of work given context as a service in the <see cref="IServiceCollection"/>.
/// </summary>
/// <typeparam name="TContext1">The type of the db context.</typeparam>
/// <typeparam name="TContext2">The type of the db context.</typeparam>
/// <typeparam name="TContext3">The type of the db context.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
/// <returns>The same service collection so that multiple calls can be chained.</returns>
/// <remarks>
/// This method only support one db context, if been called more than once, will throw exception.
/// </remarks>
public static IServiceCollection AddUnitOfWork<TContext1, TContext2, TContext3>(this IServiceCollection services)
where TContext1 : DbContext
where TContext2 : DbContext
where TContext3 : DbContext
{
services.AddScoped<IUnitOfWork<TContext1>, UnitOfWork<TContext1>>();
services.AddScoped<IUnitOfWork<TContext2>, UnitOfWork<TContext2>>();
services.AddScoped<IUnitOfWork<TContext3>, UnitOfWork<TContext3>>();
return services;
}
/// <summary>
/// Registers the unit of work given context as a service in the <see cref="IServiceCollection"/>.
/// </summary>
/// <typeparam name="TContext1">The type of the db context.</typeparam>
/// <typeparam name="TContext2">The type of the db context.</typeparam>
/// <typeparam name="TContext3">The type of the db context.</typeparam>
/// <typeparam name="TContext4">The type of the db context.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
/// <returns>The same service collection so that multiple calls can be chained.</returns>
/// <remarks>
/// This method only support one db context, if been called more than once, will throw exception.
/// </remarks>
public static IServiceCollection AddUnitOfWork<TContext1, TContext2, TContext3, TContext4>(this IServiceCollection services)
where TContext1 : DbContext
where TContext2 : DbContext
where TContext3 : DbContext
where TContext4 : DbContext
{
services.AddScoped<IUnitOfWork<TContext1>, UnitOfWork<TContext1>>();
services.AddScoped<IUnitOfWork<TContext2>, UnitOfWork<TContext2>>();
services.AddScoped<IUnitOfWork<TContext3>, UnitOfWork<TContext3>>();
services.AddScoped<IUnitOfWork<TContext4>, UnitOfWork<TContext4>>();
return services;
}
/// <summary>
/// Registers the custom repository as a service in the <see cref="IServiceCollection"/>.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <typeparam name="TRepository">The type of the custom repositry.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
/// <returns>The same service collection so that multiple calls can be chained.</returns>
public static IServiceCollection AddCustomRepository<TEntity, TRepository>(this IServiceCollection services)
where TEntity : class
where TRepository : class, IRepository<TEntity>
{
services.AddScoped<IRepository<TEntity>, TRepository>();
return services;
}
}
}