diff --git a/OpenAuth.Domain/Interface/IUnitWork.cs b/OpenAuth.Domain/Interface/IUnitWork.cs
new file mode 100644
index 00000000..a356d4ab
--- /dev/null
+++ b/OpenAuth.Domain/Interface/IUnitWork.cs
@@ -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
+{
+ ///
+ /// 工作单元接口
+ /// 适合在一下情况使用:
+ /// 1 在同一事务中进行多表操作
+ /// 2 需要多表联合查询
+ /// 因为架构采用的是EF访问数据库,暂时可以不用考虑采用传统Unit Work的注册机制
+ ///
+ public interface IUnitWork
+ {
+ T FindSingle(Expression> exp = null) where T:class;
+ bool IsExist(Expression> exp) where T:class;
+ IQueryable Find(Expression> exp = null) where T:class;
+
+ IQueryable Find(int pageindex = 1, int pagesize = 10, string orderby = "",
+ Expression> exp = null) where T:class;
+
+ int GetCount(Expression> exp = null) where T:class;
+
+ void Add(T entity) where T:class;
+
+ void BatchAdd(T[] entities) where T:class;
+
+ ///
+ /// 更新一个实体的所有属性
+ ///
+ void Update(T entity) where T:class;
+
+ void Delete(T entity) where T:class;
+
+ ///
+ /// 按指定的ID进行批量更新
+ ///
+ void Update(Expression> identityExp, T entity) where T:class;
+
+ ///
+ /// 实现按需要只更新部分更新
+ /// 如:Update(u =>u.Id==1,u =>new User{Name="ok"}) where T:class;
+ ///
+ /// 更新条件
+ /// 更新后的实体
+ void Update(Expression> where, Expression> entity) where T:class;
+ ///
+ /// 批量删除
+ ///
+ void Delete(Expression> exp) where T:class;
+
+ void Save();
+
+ }
+}
diff --git a/OpenAuth.Domain/OpenAuth.Domain.csproj b/OpenAuth.Domain/OpenAuth.Domain.csproj
index 1c6f3011..4c837888 100644
--- a/OpenAuth.Domain/OpenAuth.Domain.csproj
+++ b/OpenAuth.Domain/OpenAuth.Domain.csproj
@@ -54,6 +54,7 @@
+
diff --git a/OpenAuth.Mvc/AutofacExt.cs b/OpenAuth.Mvc/AutofacExt.cs
index 4f720627..4354cbff 100644
--- a/OpenAuth.Mvc/AutofacExt.cs
+++ b/OpenAuth.Mvc/AutofacExt.cs
@@ -32,21 +32,17 @@ namespace OpenAuth.Mvc
//����ע��
builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IRepository<>));
+ builder.RegisterType(typeof (UnitWork)).As(typeof (IUnitWork));
//Ӧ�ò�ע��
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
- builder.RegisterType();
- builder.RegisterType();
- builder.RegisterType();
- builder.RegisterType();
- builder.RegisterType();
- builder.RegisterType();
- builder.RegisterType();
- builder.RegisterType();
- builder.RegisterType();
- builder.RegisterType();
- builder.RegisterType();
- builder.RegisterType();
+
+ //注册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);
diff --git a/OpenAuth.Mvc/OpenAuth.Mvc.csproj b/OpenAuth.Mvc/OpenAuth.Mvc.csproj
index 6b1f590a..da6385f4 100644
--- a/OpenAuth.Mvc/OpenAuth.Mvc.csproj
+++ b/OpenAuth.Mvc/OpenAuth.Mvc.csproj
@@ -602,7 +602,9 @@
-
+
+ Designer
+
Web.config
diff --git a/OpenAuth.Repository/OpenAuth.Repository.csproj b/OpenAuth.Repository/OpenAuth.Repository.csproj
index 6c35b2d8..5671e77f 100644
--- a/OpenAuth.Repository/OpenAuth.Repository.csproj
+++ b/OpenAuth.Repository/OpenAuth.Repository.csproj
@@ -55,6 +55,7 @@
+
diff --git a/OpenAuth.Repository/UnitWork.cs b/OpenAuth.Repository/UnitWork.cs
new file mode 100644
index 00000000..c44a1ca3
--- /dev/null
+++ b/OpenAuth.Repository/UnitWork.cs
@@ -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();
+
+
+ ///
+ /// 根据过滤条件,获取记录
+ ///
+ /// The exp.
+ public IQueryable Find(Expression> exp = null) where T : class
+ {
+ return Filter(exp);
+ }
+
+ public bool IsExist(Expression> exp) where T : class
+ {
+ return Context.Set().Any(exp);
+ }
+
+ ///
+ /// 查找单个
+ ///
+ public T FindSingle(Expression> exp) where T:class
+ {
+ return Context.Set().AsNoTracking().FirstOrDefault(exp);
+ }
+
+ ///
+ /// 得到分页记录
+ ///
+ /// The pageindex.
+ /// The pagesize.
+ /// 排序,格式如:"Id"/"Id descending"
+ public IQueryable Find(int pageindex, int pagesize, string orderby = "", Expression> 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);
+ }
+
+ ///
+ /// 根据过滤条件获取记录数
+ ///
+ public int GetCount(Expression> exp = null) where T : class
+ {
+ return Filter(exp).Count();
+ }
+
+ public void Add(T entity) where T : class
+ {
+ Context.Set().Add(entity);
+ Save();
+ }
+
+ ///
+ /// 批量添加
+ ///
+ /// The entities.
+ public void BatchAdd(T[] entities) where T : class
+ {
+ Context.Set().AddRange(entities);
+ Save();
+ }
+
+ public void Update(T entity) where T:class
+ {
+ var entry = this.Context.Entry(entity);
+ //todo:如果状态没有任何更改,会报错
+ entry.State = EntityState.Modified;
+
+ Save();
+ }
+
+ public void Delete(T entity) where T:class
+ {
+ Context.Set().Remove(entity);
+ Save();
+ }
+
+ ///
+ /// 按指定id更新实体,会更新整个实体
+ ///
+ /// The identity exp.
+ /// The entity.
+ public void Update(Expression> identityExp, T entity) where T:class
+ {
+ Context.Set().AddOrUpdate(identityExp, entity);
+ Save();
+ }
+
+ ///
+ /// 实现按需要只更新部分更新
+ /// 如:Update(u =>u.Id==1,u =>new User{Name="ok"});
+ ///
+ /// The where.
+ /// The entity.
+ public void Update(Expression> where, Expression> entity) where T:class
+ {
+ Context.Set().Where(where).Update(entity);
+ }
+
+ public virtual void Delete(Expression> exp) where T : class
+ {
+ Context.Set().Where(exp).Delete();
+ }
+
+ public void Save()
+ {
+ Context.SaveChanges();
+ }
+
+ private IQueryable Filter(Expression> exp) where T : class
+ {
+ var dbSet = Context.Set().AsQueryable();
+ if (exp != null)
+ dbSet = dbSet.Where(exp);
+ return dbSet;
+ }
+
+ }
+}
diff --git a/OpenAuth.UnitTest/OpenAuth.UnitTest.csproj b/OpenAuth.UnitTest/OpenAuth.UnitTest.csproj
index f2523834..76a6122c 100644
--- a/OpenAuth.UnitTest/OpenAuth.UnitTest.csproj
+++ b/OpenAuth.UnitTest/OpenAuth.UnitTest.csproj
@@ -29,6 +29,7 @@
prompt
4
false
+ 6
pdbonly
@@ -61,7 +62,9 @@
-
+
+ False
+
@@ -72,6 +75,7 @@
+
diff --git a/OpenAuth.UnitTest/TestUnitWork.cs b/OpenAuth.UnitTest/TestUnitWork.cs
new file mode 100644
index 00000000..16b1e4ac
--- /dev/null
+++ b/OpenAuth.UnitTest/TestUnitWork.cs
@@ -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
+{
+ ///
+ /// TestUnitWork 的摘要说明
+ ///
+ [TestClass]
+ public class TestUnitWork
+ {
+ IUnitWork _unit = new UnitWork();
+ ///
+ /// 测试UnitWork用于联表查询
+ ///
+ [TestMethod]
+ public void GetDynamic()
+ {
+ var usersInOrg = from user in _unit.Find(null)
+ join relevance in _unit.Find(u => u.Key == "UserOrg") on user.Id equals relevance.FirstId
+ join org in _unit.Find(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}");
+ }
+ }
+ }
+}