当前位置:
首页 > Python基础教程 >
-
C#教程之【.NET架构】BIM软件架构02:Web管控平台后(2)
genericParameters))
{
isMatch = true;
break;
}
}
}
if (isMatch && !genericParameters.Any())
{
genericParameters = type.GetGenericArguments();
}
return isMatch;
}
private bool IsTypeOf(Type type, Type interfaceDefinition)
{
bool isMatch = false;
if (type.BaseType != null)
{
isMatch = IsTypeOf(type.BaseType, interfaceDefinition);
}
if (!isMatch && interfaceDefinition.IsInterface && type.GetInterfaces().Any())
{
foreach (var i in type.GetInterfaces())
{
if (IsTypeOf(i, interfaceDefinition))
{
isMatch = true;
break;
}
}
}
return isMatch;
}
}
//定义抽象基类,实现两个接口。 public abstract class EFRepository<TEntity> : IEntityRepository, IRepository<TEntity> where TEntity : class { private IEFRepositoryContext mobjContext = null; public IRepositoryContext Context { get { return mobjContext; } } public EFRepository(string contextName = "Default") { IRepositoryContext context = ServiceLocator.Instance.GetService<IRepositoryContext>(contextName) ; if (context is IEFRepositoryContext) { mobjContext = context as IEFRepositoryContext; } else { // throw new NotSupportedException(); } } public void Add(TEntity entity) { mobjContext.Add<TEntity>(entity); } public void Add(IEnumerable<TEntity> entities) { foreach (TEntity entity in entities) { Add(entity); } } public void Update(TEntity entity) { mobjContext.Update<TEntity>(entity); } public void Update(IEnumerable<TEntity> entities) { foreach (TEntity entity in entities) { Update(entity); } } public void DeleteByKey(params object[] keyValues) { TEntity defaultEntity = this.FindByKeyValues(keyValues); if (defaultEntity != null) mobjContext.Delete<TEntity>(defaultEntity); } public void Delete(TEntity entity) { mobjContext.Delete<TEntity>(entity); } public void Delete(IEnumerable<TEntity> entities) { foreach (TEntity entity in entities) { Delete(entity); } } /// <summary> /// /// </summary> /// <param name="expression"></param> /// <param name="includePath">to get related data at one time, EF use latency loading as default</param> /// <returns></returns> public IQueryable<TEntity> Query(Expression<Func<TEntity, bool>> expression = null, params string[] includePath) { IQueryable<TEntity> defaultQuery = mobjContext.Context.Set<TEntity>(); if (includePath != null) { foreach (string path in includePath) { if (!string.IsNullOrEmpty(path)) { defaultQuery = defaultQuery.Include(path); } } } if (expression != null) defaultQuery = defaultQuery.Where(expression); return defaultQuery; } public TEntity FirstOrDefault(Expression<Func<TEntity, bool>> expression = null, params string[] includePath) { IQueryable<TEntity> defaultQuery = Query(expression, includePath); return defaultQuery.FirstOrDefault(); } public TEntity FindByKeyValues(params object[] keyValues) { return mobjContext.Context.Set<TEntity>().Find(keyValues); } public IList<TEntity> FindList(Expression<Func<TEntity, bool>> expression = null, params string[] includePath) { IQueryable<TEntity> defaultQuery = Query(expression, includePath); return defaultQuery.ToList(); } public IList<TEntity> FindDistinctList(Expression<Func<TEntity, bool>> expression = null, params string[] includePath) { IQueryable<TEntity> defaultQuery = Query(expression, includePath); return defaultQuery.Distinct().ToList(); } public IList<TEntity> FindListByOrder<TKey>(Expression<Func<TEntity, bool>> expression = null, Expression<Func<TEntity, TKey>> orderBy = null, bool ascending = true, params string[] includePath) { IQueryable<TEntity> defaultQuery = Query(expression, includePath); if (orderBy != null) { if (ascending) defaultQuery = defaultQuery.OrderBy(orderBy); else defaultQuery = defaultQuery.OrderByDescending(orderBy); } return defaultQuery.ToList(); } public IList<TEntity> LoadPageList<TKey>(out long count, int pageIndex, int pageSize, Expression<Func<TEntity, bool>> expression = null, Expression<Func<TEntity, TKey>> orderBy = null, bool ascending = true, params string[] includePath) { IQueryable<TEntity> defaultQuery = Query(expression, includePath); if (orderBy != null) { if (ascending) defaultQuery = defaultQuery.OrderBy(orderBy); else defaultQuery = defaultQuery.OrderByDescending(orderBy); } count = defaultQuery.Count(); defaultQuery = defaultQuery.Skip(pageIndex).Take(pageSize); return defaultQuery.ToList(); } public IList<TEntity> SqlQueryList(string sqlQueryScript, params object[] parameters) { return mobjContext.Context.Set<TEntity>().SqlQuery(sqlQueryScript, parameters).ToList(); } public IEnumerable<object> QueryEntities(string sqlQueryScript, params object[] parameters) { ServerLogger.Info(string.Format("Query entity by sql {0}", sqlQueryScript)); return SqlQueryList(sqlQueryScript, parameters); } }
//实例化EF上下文操作接口 public abstract class EFRepositoryContext : IEFRepositoryContext { protected abstract System.Data.Entity.DbContext GetContext(); public System.Data.Entity.DbContext Context { get { return GetContext(); } } public virtual void Initialize() { GetContext(); } public virtual void Add<T>(T entity) where T : class { if (Context != null) { Context.Set<T>().Add(entity); } else { ServerLogger.Warn("Missing DB Context"); } } public virtual void Update<T>(T entity) where T : class { if (Context != null) { Context.Set<T>().Attach(entity); Context.Entry<T>(entity).State = System.Data.Entity.EntityState.Modified; } else { ServerLogger.Warn("Missing DB Context"); } } public virtual void Delete<T>(T entity) where T : class { if (Context != null) { Context.Set<T>().Remove(entity); } else { ServerLogger.Warn("Missing DB Context"); } } public virtual void Save() { if (Context != null) { Context.SaveChanges(); } else { ServerLogger.Warn("Missing DB Context"); } } public virtual void BeginTransaction() { if (Context != null && Context.Database.CurrentTransaction == null) { ServerLogger.Info("Begin Transaction"); Context.Database.BeginTransaction(); ServerLogger.Info("Transaction started"); } } public virtual void Commit() { if (Context != null && Context.Database.CurrentTransaction != null) { ServerLogger.Info("Start to Commit"); Context.Database.CurrentTransaction.Commit(); ServerLogger.Info("Committed"); } } public virtual void Rollback() { if (Context != null && Context.Database.CurrentTransaction != null) { ServerLogger.Info("Start to rollback"); Context.Database.CurrentTransaction.Rollback(); ServerLogger.Info("Rollback"); } } public virtual void Dispose() { try { if (Context != null) { if (Context.Database.CurrentTransaction != null) { Context.Database.CurrentTransaction.Dispose(); } if (Context.Database.Connection.State != System.Data.ConnectionState.Closed) { Context.Database.Connection.Close(); } Context.Dispose(); } } catch(Exception ex) { ServerLogger.Error("Faile to dispose DB context", ex); } } }
四、.Infrastructure.Mn与.Module.Mn
.Infrastructure.Mn
当我们完成.Infrastructure项目之后就可以开始写其它模块了。
.Infrastructure.Mn需要引用.Infrastructure与Database.EFModel项目。
最简单的形式如下:
public interface IModuleOneRepository : IRepository<ModuleOneItem> { //ModuleOneItem为EF的ORM对象! }
public interface IModuleOneService { void functionOne(); void functionTwo(); }
.Module.Mn
.Module.Mn需要引用.Infrastructure、Database.EFModel以及.Infrastructure.Mn项目
最简单的形式如下:
public class ModuleOneRepository : EFRepository<ModuleOneItem>, IModuleOneRepository { public ModuleOneRepository(): base() { } }
using Microsoft.Practices.Unity; public class ModuleOneService:BaseModuleService, IModuleOneService { [Dependency] public IModuleOneRepository moduleOneRepository { get; set; } public void functionOne(){} public void functionTwo(){} } //[Dependency]是Unity依赖注入的属性注入标签
五、结语
本篇主要叙述Web项目所需要引用的CoreService相关项目,有了业务核心底层dlls,剩下的就可以在Web项目中进行使用了。Web项目主要使用.Net MVC模式,在我进入项目组时,MVC框架层进行过多次修改。当我有一次拆分完一个模块的js代码时,我和另一位Tech Leader以及我们的总架构师都意识到需要进一步优化我们的MVC框架。下篇将带来Web项目的MVC架构方案以及我们是如何引用本篇的CoreService相关项目!
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式