mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-11-07 18:04:45 +08:00
同步OpenAuth.Core,增加多数据源
This commit is contained in:
@@ -11,11 +11,11 @@ using Z.EntityFramework.Plus;
|
||||
|
||||
namespace OpenAuth.Repository
|
||||
{
|
||||
public class BaseRepository<T> : IRepository<T> where T : BaseEntity
|
||||
public class BaseRepository<T,TDbContext> : IRepository<T,TDbContext> where T : BaseEntity where TDbContext: DbContext
|
||||
{
|
||||
private OpenAuthDBContext _context;
|
||||
private TDbContext _context;
|
||||
|
||||
public BaseRepository(OpenAuthDBContext context)
|
||||
public BaseRepository(TDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
@@ -16,10 +16,11 @@ using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace OpenAuth.Repository.Interface
|
||||
{
|
||||
public interface IRepository<T> where T : class
|
||||
public interface IRepository<T,TDbContext> where T : class where TDbContext: DbContext
|
||||
{
|
||||
/// <summary>
|
||||
/// 返回一个单独的实体,如果记录多于1个则取第一个
|
||||
|
||||
@@ -6,13 +6,14 @@
|
||||
// Last Modified By : yubaolee
|
||||
// Last Modified On : 12-15-2020
|
||||
// Contact : Microsoft
|
||||
// File: IUnitWork.cs
|
||||
// File: IUnitWork<OpenAuthDBContext>.cs
|
||||
// ***********************************************************************
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OpenAuth.Repository.Core;
|
||||
|
||||
namespace OpenAuth.Repository.Interface
|
||||
@@ -25,7 +26,7 @@ namespace OpenAuth.Repository.Interface
|
||||
/// <para>2 需要多表联合查询</para>
|
||||
/// <para>因为架构采用的是EF访问数据库,暂时可以不用考虑采用传统Unit Work的注册机制</para>
|
||||
/// </summary>
|
||||
public interface IUnitWork
|
||||
public interface IUnitWork<TDbContext> where TDbContext:DbContext
|
||||
{
|
||||
/// <summary>
|
||||
/// EF默认情况下,每调用一次SaveChanges()都会执行一个单独的事务
|
||||
@@ -36,7 +37,7 @@ namespace OpenAuth.Repository.Interface
|
||||
/// 返回DbContext,用于多线程等极端情况
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
OpenAuthDBContext GetDbContext();
|
||||
DbContext GetDbContext();
|
||||
/// <summary>
|
||||
/// 返回一个单独的实体,如果记录多于1个则取第一个
|
||||
/// </summary>
|
||||
|
||||
@@ -22,8 +22,8 @@ namespace OpenAuth.Repository.Test
|
||||
var serviceCollection = GetService();
|
||||
serviceCollection.AddMemoryCache();
|
||||
serviceCollection.AddOptions();
|
||||
serviceCollection.AddScoped(typeof(IRepository<>), typeof(BaseRepository<>));
|
||||
serviceCollection.AddScoped(typeof(IUnitWork), typeof(UnitWork));
|
||||
serviceCollection.AddScoped(typeof(IRepository<,>), typeof(BaseRepository<,>));
|
||||
serviceCollection.AddScoped(typeof(IUnitWork<>), typeof(UnitWork<>));
|
||||
|
||||
serviceCollection.AddDbContext<OpenAuthDBContext>(options =>
|
||||
options.UseSqlServer("Data Source=.;Initial Catalog=OpenAuthDB;User=sa;Password=000000;Integrated Security=True"));
|
||||
|
||||
@@ -83,7 +83,7 @@ namespace OpenAuth.Repository.Test
|
||||
|
||||
Console.WriteLine(account);
|
||||
|
||||
var repository = _autofacServiceProvider.GetService<IRepository<User>>();
|
||||
var repository = _autofacServiceProvider.GetService<IRepository<User,OpenAuthDBContext>>();
|
||||
|
||||
//新增
|
||||
repository.Add(new User
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace OpenAuth.Repository.Test
|
||||
[Test]
|
||||
public void NormalSubmit()
|
||||
{
|
||||
var unitWork = _autofacServiceProvider.GetService<IUnitWork>();
|
||||
var unitWork = _autofacServiceProvider.GetService<IUnitWork<OpenAuthDBContext>>();
|
||||
unitWork.ExecuteWithTransaction(() =>
|
||||
{
|
||||
var account = "user_" + DateTime.Now.ToString("yyyy_MM_dd HH:mm:ss");
|
||||
@@ -33,7 +33,7 @@ namespace OpenAuth.Repository.Test
|
||||
[Test]
|
||||
public void SubmitWithRollback()
|
||||
{
|
||||
var unitWork = _autofacServiceProvider.GetService<IUnitWork>();
|
||||
var unitWork = _autofacServiceProvider.GetService<IUnitWork<OpenAuthDBContext>>();
|
||||
var account = "user_" + DateTime.Now.ToString("yyyy_MM_dd HH:mm:ss");
|
||||
try
|
||||
{
|
||||
@@ -57,7 +57,7 @@ namespace OpenAuth.Repository.Test
|
||||
/// <summary>
|
||||
/// 测试添加,单个修改,Z.EntityFramework.Plus条件修改
|
||||
/// </summary>
|
||||
private void AddAndUpdate(string account, IUnitWork unitWork)
|
||||
private void AddAndUpdate(string account, IUnitWork<OpenAuthDBContext> unitWork)
|
||||
{
|
||||
var user = new User
|
||||
{
|
||||
|
||||
@@ -12,11 +12,11 @@ using Z.EntityFramework.Plus;
|
||||
|
||||
namespace OpenAuth.Repository
|
||||
{
|
||||
public class UnitWork: IUnitWork
|
||||
public class UnitWork<TDbContext>: IUnitWork<TDbContext> where TDbContext: DbContext
|
||||
{
|
||||
private OpenAuthDBContext _context;
|
||||
private TDbContext _context;
|
||||
|
||||
public UnitWork(OpenAuthDBContext context)
|
||||
public UnitWork(TDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
@@ -45,7 +45,7 @@ namespace OpenAuth.Repository
|
||||
/// <summary>
|
||||
/// 返回DbContext,用于多线程等极端情况
|
||||
/// </summary>
|
||||
public OpenAuthDBContext GetDbContext()
|
||||
public DbContext GetDbContext()
|
||||
{
|
||||
return _context;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user