Skip to content

Commit

Permalink
feat: support custom repository & TransactionScope
Browse files Browse the repository at this point in the history
1. Support get custom repository from UnitOfWork
2. Using TransactionScope to support distributed transaction;

close #19 #74 #78
  • Loading branch information
yingtingxu committed Sep 7, 2018
1 parent 594a381 commit 07b8dc8
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 81 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public void ConfigureServices(IServiceCollection services)
// use in memory for testing.
services
.AddDbContext<QuickStartContext>(opt => opt.UseInMemoryDatabase())
.AddUnitOfWork<QuickStartContext>();
.AddUnitOfWork<QuickStartContext>()
.AddCustomRepository<Blog, CustomBlogRepository>();
}

private readonly IUnitOfWork _unitOfWork;
Expand Down
2 changes: 1 addition & 1 deletion src/Host/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ValuesController(IUnitOfWork unitOfWork, ILogger<ValuesController> logger
_logger = logger;

// seeding
var repo = _unitOfWork.GetRepository<Blog>();
var repo = _unitOfWork.GetRepository<Blog>(hasCustomRepository: true);
if (repo.Count() == 0)
{
repo.Insert(new Blog
Expand Down
26 changes: 13 additions & 13 deletions src/Host/Host.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Routing" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Routing" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.1.2" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.1" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Microsoft.EntityFrameworkCore.UnitOfWork\Microsoft.EntityFrameworkCore.UnitOfWork.csproj" />
Expand Down
12 changes: 12 additions & 0 deletions src/Host/Models/CustomBlogRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.EntityFrameworkCore;

namespace Host.Models
{
public class CustomBlogRepository : Repository<Blog>, IRepository<Blog>
{
public CustomBlogRepository(BloggingContext dbContext) : base(dbContext)
{

}
}
}
5 changes: 3 additions & 2 deletions src/Host/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public void ConfigureServices(IServiceCollection services)
{
// use in memory for testing.
services
.AddDbContext<BloggingContext>(opt => opt.UseMySql("Server=localhost;database=unitofwork;uid=root;pwd=p@ssword;"))
.AddDbContext<BloggingContext>(opt => opt.UseMySql("Server=10.125.54.139;database=uow;uid=root;pwd=root1234;"))
//.AddDbContext<BloggingContext>(opt => opt.UseInMemoryDatabase("UnitOfWork"))
.AddUnitOfWork<BloggingContext>();
.AddUnitOfWork<BloggingContext>()
.AddCustomRepository<Blog, CustomBlogRepository>();

services.AddMvc();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ public interface IRepositoryFactory
/// <summary>
/// Gets the specified repository for the <typeparamref name="TEntity"/>.
/// </summary>
/// <param name="hasCustomRepository"><c>True</c> if providing custom repositry</param>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <returns>An instance of type inherited from <see cref="IRepository{TEntity}"/> interface.</returns>
IRepository<TEntity> GetRepository<TEntity>() where TEntity : class;
IRepository<TEntity> GetRepository<TEntity>(bool hasCustomRepository = false) where TEntity : class;
}
}
3 changes: 2 additions & 1 deletion src/Microsoft.EntityFrameworkCore.UnitOfWork/IUnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ public interface IUnitOfWork : IDisposable
/// <summary>
/// Gets the specified repository for the <typeparamref name="TEntity"/>.
/// </summary>
/// <param name="hasCustomRepository"><c>True</c> if providing custom repositry</param>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <returns>An instance of type inherited from <see cref="IRepository{TEntity}"/> interface.</returns>
IRepository<TEntity> GetRepository<TEntity>() where TEntity : class;
IRepository<TEntity> GetRepository<TEntity>(bool hasCustomRepository = false) where TEntity : class;

/// <summary>
/// Saves all changes made in this context to the database.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>A plugin for Microsoft.EntityFrameworkCore to support repository, unit of work patterns, and multiple database with distributed transaction supported.</Description>
<VersionPrefix>2.0.3</VersionPrefix>
<VersionPrefix>2.1.0</VersionPrefix>
<Authors>rigofunc;[email protected];</Authors>
<TargetFramework>netstandard2.0</TargetFramework>
<NoWarn>$(NoWarn);CS1591</NoWarn>
Expand All @@ -16,7 +16,7 @@
<RepositoryUrl>https://github.com/arch/UnitOfWork.git</RepositoryUrl>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.AutoHistory" Version="2.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.AutoHistory" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.1.2" />
</ItemGroup>
</Project>
Loading

0 comments on commit 07b8dc8

Please sign in to comment.