Skip to content

Commit 07b8dc8

Browse files
author
yingtingxu
committed
feat: support custom repository & TransactionScope
1. Support get custom repository from UnitOfWork 2. Using TransactionScope to support distributed transaction; close arch#19 arch#74 arch#78
1 parent 594a381 commit 07b8dc8

13 files changed

+115
-81
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public void ConfigureServices(IServiceCollection services)
2121
// use in memory for testing.
2222
services
2323
.AddDbContext<QuickStartContext>(opt => opt.UseInMemoryDatabase())
24-
.AddUnitOfWork<QuickStartContext>();
24+
.AddUnitOfWork<QuickStartContext>()
25+
.AddCustomRepository<Blog, CustomBlogRepository>();
2526
}
2627

2728
private readonly IUnitOfWork _unitOfWork;

src/Host/Controllers/ValuesController.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public ValuesController(IUnitOfWork unitOfWork, ILogger<ValuesController> logger
2323
_logger = logger;
2424

2525
// seeding
26-
var repo = _unitOfWork.GetRepository<Blog>();
26+
var repo = _unitOfWork.GetRepository<Blog>(hasCustomRepository: true);
2727
if (repo.Count() == 0)
2828
{
2929
repo.Insert(new Blog

src/Host/Host.csproj

+13-13
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
<OutputType>Exe</OutputType>
66
</PropertyGroup>
77
<ItemGroup>
8-
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.2" />
9-
<PackageReference Include="Microsoft.AspNetCore.Routing" Version="2.0.1" />
10-
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.0.1" />
11-
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.0.1" />
12-
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.0" />
13-
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" />
14-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
15-
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
16-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
17-
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
18-
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.0.1" />
19-
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0" />
20-
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.0.1" />
8+
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.2" />
9+
<PackageReference Include="Microsoft.AspNetCore.Routing" Version="2.1.1" />
10+
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.1.1" />
11+
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.1.3" />
12+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.1.1" />
13+
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.1.1" />
14+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
15+
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
16+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.1" />
17+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.1.1" />
18+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.1.2" />
19+
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.1" />
20+
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.2" />
2121
</ItemGroup>
2222
<ItemGroup>
2323
<ProjectReference Include="..\Microsoft.EntityFrameworkCore.UnitOfWork\Microsoft.EntityFrameworkCore.UnitOfWork.csproj" />
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace Host.Models
4+
{
5+
public class CustomBlogRepository : Repository<Blog>, IRepository<Blog>
6+
{
7+
public CustomBlogRepository(BloggingContext dbContext) : base(dbContext)
8+
{
9+
10+
}
11+
}
12+
}

src/Host/Startup.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ public void ConfigureServices(IServiceCollection services)
3232
{
3333
// use in memory for testing.
3434
services
35-
.AddDbContext<BloggingContext>(opt => opt.UseMySql("Server=localhost;database=unitofwork;uid=root;pwd=p@ssword;"))
35+
.AddDbContext<BloggingContext>(opt => opt.UseMySql("Server=10.125.54.139;database=uow;uid=root;pwd=root1234;"))
3636
//.AddDbContext<BloggingContext>(opt => opt.UseInMemoryDatabase("UnitOfWork"))
37-
.AddUnitOfWork<BloggingContext>();
37+
.AddUnitOfWork<BloggingContext>()
38+
.AddCustomRepository<Blog, CustomBlogRepository>();
3839

3940
services.AddMvc();
4041
}

src/Microsoft.EntityFrameworkCore.UnitOfWork/IRepositoryFactory.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ public interface IRepositoryFactory
1010
/// <summary>
1111
/// Gets the specified repository for the <typeparamref name="TEntity"/>.
1212
/// </summary>
13+
/// <param name="hasCustomRepository"><c>True</c> if providing custom repositry</param>
1314
/// <typeparam name="TEntity">The type of the entity.</typeparam>
1415
/// <returns>An instance of type inherited from <see cref="IRepository{TEntity}"/> interface.</returns>
15-
IRepository<TEntity> GetRepository<TEntity>() where TEntity : class;
16+
IRepository<TEntity> GetRepository<TEntity>(bool hasCustomRepository = false) where TEntity : class;
1617
}
1718
}

src/Microsoft.EntityFrameworkCore.UnitOfWork/IUnitOfWork.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ public interface IUnitOfWork : IDisposable
2424
/// <summary>
2525
/// Gets the specified repository for the <typeparamref name="TEntity"/>.
2626
/// </summary>
27+
/// <param name="hasCustomRepository"><c>True</c> if providing custom repositry</param>
2728
/// <typeparam name="TEntity">The type of the entity.</typeparam>
2829
/// <returns>An instance of type inherited from <see cref="IRepository{TEntity}"/> interface.</returns>
29-
IRepository<TEntity> GetRepository<TEntity>() where TEntity : class;
30+
IRepository<TEntity> GetRepository<TEntity>(bool hasCustomRepository = false) where TEntity : class;
3031

3132
/// <summary>
3233
/// Saves all changes made in this context to the database.

src/Microsoft.EntityFrameworkCore.UnitOfWork/Microsoft.EntityFrameworkCore.UnitOfWork.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Description>A plugin for Microsoft.EntityFrameworkCore to support repository, unit of work patterns, and multiple database with distributed transaction supported.</Description>
4-
<VersionPrefix>2.0.3</VersionPrefix>
4+
<VersionPrefix>2.1.0</VersionPrefix>
55
<Authors>rigofunc;[email protected];</Authors>
66
<TargetFramework>netstandard2.0</TargetFramework>
77
<NoWarn>$(NoWarn);CS1591</NoWarn>
@@ -16,7 +16,7 @@
1616
<RepositoryUrl>https://github.com/arch/UnitOfWork.git</RepositoryUrl>
1717
</PropertyGroup>
1818
<ItemGroup>
19-
<PackageReference Include="Microsoft.EntityFrameworkCore.AutoHistory" Version="2.0.3" />
20-
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.0.1" />
19+
<PackageReference Include="Microsoft.EntityFrameworkCore.AutoHistory" Version="2.1.0" />
20+
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.1.2" />
2121
</ItemGroup>
2222
</Project>

0 commit comments

Comments
 (0)