VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 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相关项目!


相关教程
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号