Skip to content

Commit f4c64a8

Browse files
authored
Merge pull request #5 from Calabonga/6.2.0-beta.1
release 6.2.0 (net8.0, net9.0)
2 parents 55e4690 + c9a087b commit f4c64a8

File tree

7 files changed

+145
-6
lines changed

7 files changed

+145
-6
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88

99
## Версии
1010

11+
### 6.2.0 2025-11-21
12+
13+
* `ServiceLifetime` for `IUnitOfWork` added as a parameter during DI-container registration.
14+
1115
### 6.1.0 2025-10-13
1216

1317
* `FromSqlRawInterpolated` implemented in `IUnitOfWork` from `DbContext`.
14-
* * New release `6.1.0` published on the nuget.org.
1518

1619
### 6.0.0 2025-03-05
1720

1821
* Deprecated methods were removed. Please use `TrackingType` parameter instead of `disableTracking`.
19-
* New release `6.0.0` published.
2022

2123
### 5.0.0 2024-11-25
2224

src/Calabonga.UnitOfWork/Calabonga.UnitOfWork.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
5-
<Version>6.1.0</Version>
5+
<Version>6.2.0</Version>
66
<Authors>Calabonga</Authors>
77
<Company>Calabonga SOFT</Company>
88
<Copyright>Calabonga SOFT © 2001-$([System.DateTime]::Now.ToString(yyyy))</Copyright>
99
<Description>Unit of Work implementation for EntityFramework Core. For more information please see Calabonga.UnitOfWork package.</Description>
1010
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1111
<PackageIcon>logo.png</PackageIcon>
1212
<PackageTags>Calabonga EntityFrameworkCore UnitOfWork Repository Extension Helper unitofowrk ORM pagination pattern changes Tracking entites</PackageTags>
13-
<PackageReleaseNotes>FromSqlRawInterpolated implemented in IUnitOfWork from DbContext.</PackageReleaseNotes>
13+
<PackageReleaseNotes>ServiceLifetime IUnitOfWork added as a parameter during DI-container registration.</PackageReleaseNotes>
1414
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1515
<PackageProjectUrl>https://www.calabonga.net</PackageProjectUrl>
1616
<Nullable>enable</Nullable>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace Calabonga.UnitOfWork;
4+
5+
public interface IUnitOfWorkFactory<out TContext> : IUnitOfWorkFactory
6+
{
7+
TContext DbContext { get; }
8+
}
9+
10+
public interface IUnitOfWorkFactory : IDisposable
11+
{
12+
IUnitOfWork CreateUnitOfWork();
13+
}

src/Calabonga.UnitOfWork/Repository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,4 +1195,4 @@ public Task<IPagedList<TResult>> GetPagedListAsync<TResult>(Expression<Func<TEnt
11951195
? await orderBy(query).Select(selector).FirstOrDefaultAsync()
11961196
: await query.Select(selector).FirstOrDefaultAsync();
11971197
}
1198-
}
1198+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace Calabonga.UnitOfWork;
4+
5+
/// <summary>
6+
/// CALABONGA Warning: do not remove sealed
7+
/// Represents the default implementation of the <see cref="T:IUnitOfWork"/> and <see cref="T:IUnitOfWork{TContext}"/> interface.
8+
/// </summary>
9+
/// <typeparam name="TContext">The type of the db context.</typeparam>
10+
public sealed class UnitOfWorkFactory<TContext> : IUnitOfWorkFactory<TContext>
11+
where TContext : DbContext
12+
{
13+
public UnitOfWorkFactory(IDbContextFactory<TContext> factory) => DbContext = factory.CreateDbContext();
14+
15+
public TContext DbContext { get; }
16+
17+
public IUnitOfWork CreateUnitOfWork() => new UnitOfWork<TContext>(DbContext);
18+
19+
public void Dispose() => DbContext.Dispose();
20+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.DependencyInjection.Extensions;
4+
using System;
5+
6+
namespace Calabonga.UnitOfWork;
7+
8+
/// <summary>
9+
/// Extension methods for setting up unit of work related services in an <see cref="IServiceCollection"/>.
10+
/// </summary>
11+
public static class UnitOfWorkFactoryServiceCollectionExtensions
12+
{
13+
/// <summary>
14+
/// Registers the unit of work given context as a service in the <see cref="IServiceCollection"/>.
15+
/// </summary>
16+
/// <typeparam name="TContext">The type of the db context.</typeparam>
17+
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
18+
/// <param name="lifetime"></param>
19+
/// <returns>The same service collection so that multiple calls can be chained.</returns>
20+
/// <remarks>
21+
/// This method only support one db context, if been called more than once, will throw exception.
22+
/// </remarks>
23+
public static IServiceCollection AddUnitOfWorkFactory<TContext>(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Scoped)
24+
where TContext : DbContext
25+
{
26+
switch (lifetime)
27+
{
28+
case ServiceLifetime.Singleton:
29+
services.TryAddSingleton<IRepositoryFactory, UnitOfWork<TContext>>();
30+
// Following has an issue: IUnitOfWork cannot support multiple dbContext/database,
31+
// that means cannot call AddUnitOfWork<TContext> multiple times.
32+
// Solution: check IUnitOfWork whether or null
33+
services.TryAddSingleton<IUnitOfWork, UnitOfWork<TContext>>();
34+
services.TryAddSingleton<IUnitOfWork<TContext>, UnitOfWork<TContext>>();
35+
services.TryAddSingleton<IUnitOfWorkFactory, UnitOfWorkFactory<TContext>>();
36+
services.TryAddSingleton<IUnitOfWorkFactory<TContext>, UnitOfWorkFactory<TContext>>();
37+
break;
38+
case ServiceLifetime.Scoped:
39+
services.TryAddScoped<IRepositoryFactory, UnitOfWork<TContext>>();
40+
// Following has an issue: IUnitOfWork cannot support multiple dbContext/database,
41+
// that means cannot call AddUnitOfWork<TContext> multiple times.
42+
// Solution: check IUnitOfWork whether or null
43+
services.TryAddScoped<IUnitOfWork, UnitOfWork<TContext>>();
44+
services.TryAddScoped<IUnitOfWork<TContext>, UnitOfWork<TContext>>();
45+
services.TryAddScoped<IUnitOfWorkFactory, UnitOfWorkFactory<TContext>>();
46+
services.TryAddScoped<IUnitOfWorkFactory<TContext>, UnitOfWorkFactory<TContext>>();
47+
break;
48+
case ServiceLifetime.Transient:
49+
services.TryAddTransient<IRepositoryFactory, UnitOfWork<TContext>>();
50+
// Following has an issue: IUnitOfWork cannot support multiple dbContext/database,
51+
// that means cannot call AddUnitOfWork<TContext> multiple times.
52+
// Solution: check IUnitOfWork whether or null
53+
services.TryAddTransient<IUnitOfWork, UnitOfWork<TContext>>();
54+
services.TryAddTransient<IUnitOfWork<TContext>, UnitOfWork<TContext>>();
55+
services.TryAddTransient<IUnitOfWorkFactory, UnitOfWorkFactory<TContext>>();
56+
services.TryAddTransient<IUnitOfWorkFactory<TContext>, UnitOfWorkFactory<TContext>>();
57+
break;
58+
default:
59+
throw new ArgumentOutOfRangeException(nameof(lifetime), lifetime, null);
60+
}
61+
62+
services.AddScoped<IRepositoryFactory, UnitOfWork<TContext>>();
63+
// Following has an issue: IUnitOfWork cannot support multiple dbContext/database,
64+
// that means cannot call AddUnitOfWork<TContext> multiple times.
65+
// Solution: check IUnitOfWork whether or null
66+
services.AddScoped<IUnitOfWork, UnitOfWork<TContext>>();
67+
services.AddScoped<IUnitOfWork<TContext>, UnitOfWork<TContext>>();
68+
69+
return services;
70+
}
71+
72+
}

src/Calabonga.UnitOfWork/UnitOfWorkServiceCollectionExtensions.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.EntityFrameworkCore;
22
using Microsoft.Extensions.DependencyInjection;
3+
using System;
34

45
namespace Calabonga.UnitOfWork;
56

@@ -13,13 +14,44 @@ public static class UnitOfWorkServiceCollectionExtensions
1314
/// </summary>
1415
/// <typeparam name="TContext">The type of the db context.</typeparam>
1516
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
17+
/// <param name="lifetime"></param>
1618
/// <returns>The same service collection so that multiple calls can be chained.</returns>
1719
/// <remarks>
1820
/// This method only support one db context, if been called more than once, will throw exception.
1921
/// </remarks>
20-
public static IServiceCollection AddUnitOfWork<TContext>(this IServiceCollection services)
22+
public static IServiceCollection AddUnitOfWork<TContext>(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Scoped)
2123
where TContext : DbContext
2224
{
25+
switch (lifetime)
26+
{
27+
case ServiceLifetime.Singleton:
28+
services.AddSingleton<IRepositoryFactory, UnitOfWork<TContext>>();
29+
// Following has an issue: IUnitOfWork cannot support multiple dbContext/database,
30+
// that means cannot call AddUnitOfWork<TContext> multiple times.
31+
// Solution: check IUnitOfWork whether or null
32+
services.AddSingleton<IUnitOfWork, UnitOfWork<TContext>>();
33+
services.AddSingleton<IUnitOfWork<TContext>, UnitOfWork<TContext>>();
34+
break;
35+
case ServiceLifetime.Scoped:
36+
services.AddScoped<IRepositoryFactory, UnitOfWork<TContext>>();
37+
// Following has an issue: IUnitOfWork cannot support multiple dbContext/database,
38+
// that means cannot call AddUnitOfWork<TContext> multiple times.
39+
// Solution: check IUnitOfWork whether or null
40+
services.AddScoped<IUnitOfWork, UnitOfWork<TContext>>();
41+
services.AddScoped<IUnitOfWork<TContext>, UnitOfWork<TContext>>();
42+
break;
43+
case ServiceLifetime.Transient:
44+
services.AddTransient<IRepositoryFactory, UnitOfWork<TContext>>();
45+
// Following has an issue: IUnitOfWork cannot support multiple dbContext/database,
46+
// that means cannot call AddUnitOfWork<TContext> multiple times.
47+
// Solution: check IUnitOfWork whether or null
48+
services.AddTransient<IUnitOfWork, UnitOfWork<TContext>>();
49+
services.AddTransient<IUnitOfWork<TContext>, UnitOfWork<TContext>>();
50+
break;
51+
default:
52+
throw new ArgumentOutOfRangeException(nameof(lifetime), lifetime, null);
53+
}
54+
2355
services.AddScoped<IRepositoryFactory, UnitOfWork<TContext>>();
2456
// Following has an issue: IUnitOfWork cannot support multiple dbContext/database,
2557
// that means cannot call AddUnitOfWork<TContext> multiple times.

0 commit comments

Comments
 (0)