mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-04-05 17:38:01 +08:00
1 增加UnitWork
2 修改UnitTest项目为C# 6.0
This commit is contained in:
parent
606452b2e7
commit
40666863d9
67
OpenAuth.Domain/Interface/IUnitWork.cs
Normal file
67
OpenAuth.Domain/Interface/IUnitWork.cs
Normal file
@ -0,0 +1,67 @@
|
||||
// ***********************************************************************
|
||||
// Assembly : OpenAuth.Domain
|
||||
// Author : yubaolee
|
||||
// Created : 04-29-2016
|
||||
//
|
||||
// Last Modified By : yubaolee
|
||||
// Last Modified On : 04-29-2016
|
||||
// Contact : Microsoft
|
||||
// File: IUnitWork.cs
|
||||
// ***********************************************************************
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace OpenAuth.Domain.Interface
|
||||
{
|
||||
/// <summary>
|
||||
/// 工作单元接口
|
||||
/// <para> 适合在一下情况使用:</para>
|
||||
/// <para>1 在同一事务中进行多表操作</para>
|
||||
/// <para>2 需要多表联合查询</para>
|
||||
/// <para>因为架构采用的是EF访问数据库,暂时可以不用考虑采用传统Unit Work的注册机制</para>
|
||||
/// </summary>
|
||||
public interface IUnitWork
|
||||
{
|
||||
T FindSingle<T>(Expression<Func<T, bool>> exp = null) where T:class;
|
||||
bool IsExist<T>(Expression<Func<T, bool>> exp) where T:class;
|
||||
IQueryable<T> Find<T>(Expression<Func<T, bool>> exp = null) where T:class;
|
||||
|
||||
IQueryable<T> Find<T>(int pageindex = 1, int pagesize = 10, string orderby = "",
|
||||
Expression<Func<T, bool>> exp = null) where T:class;
|
||||
|
||||
int GetCount<T>(Expression<Func<T, bool>> exp = null) where T:class;
|
||||
|
||||
void Add<T>(T entity) where T:class;
|
||||
|
||||
void BatchAdd<T>(T[] entities) where T:class;
|
||||
|
||||
/// <summary>
|
||||
/// 更新一个实体的所有属性
|
||||
/// </summary>
|
||||
void Update<T>(T entity) where T:class;
|
||||
|
||||
void Delete<T>(T entity) where T:class;
|
||||
|
||||
/// <summary>
|
||||
/// 按指定的ID进行批量更新
|
||||
/// </summary>
|
||||
void Update<T>(Expression<Func<T, object>> identityExp, T entity) where T:class;
|
||||
|
||||
/// <summary>
|
||||
/// 实现按需要只更新部分更新
|
||||
/// <para>如:Update<T>(u =>u.Id==1,u =>new User{Name="ok"}) where T:class;</para>
|
||||
/// </summary>
|
||||
/// <param name="where">更新条件</param>
|
||||
/// <param name="entity">更新后的实体</param>
|
||||
void Update<T>(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity) where T:class;
|
||||
/// <summary>
|
||||
/// 批量删除
|
||||
/// </summary>
|
||||
void Delete<T>(Expression<Func<T, bool>> exp) where T:class;
|
||||
|
||||
void Save();
|
||||
|
||||
}
|
||||
}
|
@ -54,6 +54,7 @@
|
||||
<Compile Include="Interface\IRoleRepository.cs" />
|
||||
<Compile Include="Interface\IRelevanceRepository.cs" />
|
||||
<Compile Include="Interface\IStockRepository.cs" />
|
||||
<Compile Include="Interface\IUnitWork.cs" />
|
||||
<Compile Include="Interface\IUserRepository.cs" />
|
||||
<Compile Include="Module.cs" />
|
||||
<Compile Include="ModuleElement.cs" />
|
||||
|
@ -32,21 +32,17 @@ namespace OpenAuth.Mvc
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>
|
||||
builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IRepository<>));
|
||||
builder.RegisterType(typeof (UnitWork)).As(typeof (IUnitWork));
|
||||
|
||||
//Ӧ<>ò<EFBFBD>ע<EFBFBD><D7A2>
|
||||
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
|
||||
builder.RegisterType<LoginApp>();
|
||||
builder.RegisterType<OrgManagerApp>();
|
||||
builder.RegisterType<UserManagerApp>();
|
||||
builder.RegisterType<RoleManagerApp>();
|
||||
builder.RegisterType<ModuleManagerApp>();
|
||||
builder.RegisterType<ModuleElementManagerApp>();
|
||||
builder.RegisterType<CategoryManagerApp>();
|
||||
builder.RegisterType<ResourceManagerApp>();
|
||||
builder.RegisterType<StockManagerApp>();
|
||||
builder.RegisterType<RevelanceManagerApp>();
|
||||
builder.RegisterType<AuthoriseService>();
|
||||
builder.RegisterType<StockManagerService>();
|
||||
|
||||
//注册app层
|
||||
builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof (LoginApp)));
|
||||
|
||||
//注册领域服务
|
||||
builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof(AuthoriseService)))
|
||||
.Where(u =>u.Namespace== "OpenAuth.Domain.Service");
|
||||
|
||||
// Register your MVC controllers.
|
||||
builder.RegisterControllers(typeof(MvcApplication).Assembly);
|
||||
|
@ -602,7 +602,9 @@
|
||||
<None Include="Properties\PublishProfiles\default.pubxml" />
|
||||
<None Include="Views\Error\NoAccess.cshtml" />
|
||||
<Content Include="Views\Home\git.cshtml" />
|
||||
<Content Include="Web.config" />
|
||||
<Content Include="Web.config">
|
||||
<SubType>Designer</SubType>
|
||||
</Content>
|
||||
<Content Include="Web.Debug.config">
|
||||
<DependentUpon>Web.config</DependentUpon>
|
||||
</Content>
|
||||
|
@ -55,6 +55,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="UnitWork.cs" />
|
||||
<Compile Include="BaseRepository.cs" />
|
||||
<Compile Include="Models\Mapping\CategoryMap.cs" />
|
||||
<Compile Include="Models\Mapping\DicDetailMap.cs" />
|
||||
|
135
OpenAuth.Repository/UnitWork.cs
Normal file
135
OpenAuth.Repository/UnitWork.cs
Normal file
@ -0,0 +1,135 @@
|
||||
using System;
|
||||
using System.Data.Entity;
|
||||
using System.Data.Entity.Migrations;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using EntityFramework.Extensions;
|
||||
using OpenAuth.Domain.Interface;
|
||||
using OpenAuth.Repository.Models;
|
||||
using Infrastructure;
|
||||
|
||||
namespace OpenAuth.Repository
|
||||
{
|
||||
public class UnitWork: IUnitWork
|
||||
{
|
||||
protected OpenAuthDBContext Context = new OpenAuthDBContext();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 根据过滤条件,获取记录
|
||||
/// </summary>
|
||||
/// <param name="exp">The exp.</param>
|
||||
public IQueryable<T> Find<T>(Expression<Func<T, bool>> exp = null) where T : class
|
||||
{
|
||||
return Filter(exp);
|
||||
}
|
||||
|
||||
public bool IsExist<T>(Expression<Func<T, bool>> exp) where T : class
|
||||
{
|
||||
return Context.Set<T>().Any(exp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找单个
|
||||
/// </summary>
|
||||
public T FindSingle<T>(Expression<Func<T, bool>> exp) where T:class
|
||||
{
|
||||
return Context.Set<T>().AsNoTracking().FirstOrDefault(exp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到分页记录
|
||||
/// </summary>
|
||||
/// <param name="pageindex">The pageindex.</param>
|
||||
/// <param name="pagesize">The pagesize.</param>
|
||||
/// <param name="orderby">排序,格式如:"Id"/"Id descending"</param>
|
||||
public IQueryable<T> Find<T>(int pageindex, int pagesize, string orderby = "", Expression<Func<T, bool>> exp = null) where T : class
|
||||
{
|
||||
if (pageindex < 1) pageindex = 1;
|
||||
if (string.IsNullOrEmpty(orderby))
|
||||
orderby = "Id descending";
|
||||
|
||||
return Filter(exp).OrderBy(orderby).Skip(pagesize * (pageindex - 1)).Take(pagesize);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据过滤条件获取记录数
|
||||
/// </summary>
|
||||
public int GetCount<T>(Expression<Func<T, bool>> exp = null) where T : class
|
||||
{
|
||||
return Filter(exp).Count();
|
||||
}
|
||||
|
||||
public void Add<T>(T entity) where T : class
|
||||
{
|
||||
Context.Set<T>().Add(entity);
|
||||
Save();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量添加
|
||||
/// </summary>
|
||||
/// <param name="entities">The entities.</param>
|
||||
public void BatchAdd<T>(T[] entities) where T : class
|
||||
{
|
||||
Context.Set<T>().AddRange(entities);
|
||||
Save();
|
||||
}
|
||||
|
||||
public void Update<T>(T entity) where T:class
|
||||
{
|
||||
var entry = this.Context.Entry(entity);
|
||||
//todo:如果状态没有任何更改,会报错
|
||||
entry.State = EntityState.Modified;
|
||||
|
||||
Save();
|
||||
}
|
||||
|
||||
public void Delete<T>(T entity) where T:class
|
||||
{
|
||||
Context.Set<T>().Remove(entity);
|
||||
Save();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按指定id更新实体,会更新整个实体
|
||||
/// </summary>
|
||||
/// <param name="identityExp">The identity exp.</param>
|
||||
/// <param name="entity">The entity.</param>
|
||||
public void Update<T>(Expression<Func<T, object>> identityExp, T entity) where T:class
|
||||
{
|
||||
Context.Set<T>().AddOrUpdate(identityExp, entity);
|
||||
Save();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 实现按需要只更新部分更新
|
||||
/// <para>如:Update(u =>u.Id==1,u =>new User{Name="ok"});</para>
|
||||
/// </summary>
|
||||
/// <param name="where">The where.</param>
|
||||
/// <param name="entity">The entity.</param>
|
||||
public void Update<T>(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity) where T:class
|
||||
{
|
||||
Context.Set<T>().Where(where).Update(entity);
|
||||
}
|
||||
|
||||
public virtual void Delete<T>(Expression<Func<T, bool>> exp) where T : class
|
||||
{
|
||||
Context.Set<T>().Where(exp).Delete();
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
Context.SaveChanges();
|
||||
}
|
||||
|
||||
private IQueryable<T> Filter<T>(Expression<Func<T, bool>> exp) where T : class
|
||||
{
|
||||
var dbSet = Context.Set<T>().AsQueryable();
|
||||
if (exp != null)
|
||||
dbSet = dbSet.Where(exp);
|
||||
return dbSet;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -29,6 +29,7 @@
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
<LangVersion>6</LangVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
@ -61,7 +62,9 @@
|
||||
</When>
|
||||
<Otherwise>
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
|
||||
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
</Otherwise>
|
||||
</Choose>
|
||||
@ -72,6 +75,7 @@
|
||||
<Compile Include="TestModuleApp.cs" />
|
||||
<Compile Include="TestRepository.cs" />
|
||||
<Compile Include="TestRoleApp.cs" />
|
||||
<Compile Include="TestUnitWork.cs" />
|
||||
<Compile Include="TestUserApp.cs" />
|
||||
<Compile Include="TestOrgApp.cs" />
|
||||
</ItemGroup>
|
||||
|
38
OpenAuth.UnitTest/TestUnitWork.cs
Normal file
38
OpenAuth.UnitTest/TestUnitWork.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using OpenAuth.Domain;
|
||||
using OpenAuth.Domain.Interface;
|
||||
using OpenAuth.Repository;
|
||||
|
||||
namespace OpenAuth.UnitTest
|
||||
{
|
||||
/// <summary>
|
||||
/// TestUnitWork 的摘要说明
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class TestUnitWork
|
||||
{
|
||||
IUnitWork _unit = new UnitWork();
|
||||
/// <summary>
|
||||
/// 测试UnitWork用于联表查询
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void GetDynamic()
|
||||
{
|
||||
var usersInOrg = from user in _unit.Find<User>(null)
|
||||
join relevance in _unit.Find<Relevance>(u => u.Key == "UserOrg") on user.Id equals relevance.FirstId
|
||||
join org in _unit.Find<Org>(null) on relevance.SecondId equals org.Id
|
||||
select new
|
||||
{
|
||||
user.Name,
|
||||
OrgName = org.Name
|
||||
};
|
||||
|
||||
foreach (var user in usersInOrg)
|
||||
{
|
||||
Debug.WriteLine($"{user.Name} :{user.OrgName}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user