作者微信 bishe2022

代码功能演示视频在页面下方,请先观看;如需定制开发,联系页面右侧客服
开源:ASP.NET MVC+EF6+Bootstrap开发框架

Custom Tab

  

框架解决方案:

 

解决方案简介:

1、NFine.Code 底层核心类(开发时不涉及,可编绎成dll提供)。

2、NFine.Data 数据层(开发时不涉及,可编绎成dll提供)。

3、NFine.Application  应用(有点类似业务逻辑层) 

4、NFine.Domain 领域层。

5、NFine.Mapping 数据库映射。

6、NFine.Repository 数据访问。

7、NFine.Web 前端视图及控制器。

框架主要运用技术:

框架代码风格:

数据库、仓库代码

1 using NFine.Code; 
2 using System; 
3 using System.Collections.Generic; 
4 using System.Data.Common; 
5 using System.Linq; 
6 using System.Linq.Expressions; 
7  
8 namespace NFine.Data 
9 {
10     /// <summary>
11     /// 仓储接口
12     /// </summary>
13     /// <typeparam name="TEntity">实体类型</typeparam>
14     public interface IRepositoryBase<TEntity> where TEntity : class,new()
15     {
16         int Insert(TEntity entity);
17         int Insert(List<TEntity> entitys);
18         int Update(TEntity entity);
19         int Delete(TEntity entity);
20         int Delete(Expression<Func<TEntity, bool>> predicate);
21         TEntity FindEntity(object keyValue);
22         TEntity FindEntity(Expression<Func<TEntity, bool>> predicate);
23         IQueryable<TEntity> IQueryable();
24         IQueryable<TEntity> IQueryable(Expression<Func<TEntity, bool>> predicate);
25         List<TEntity> FindList(string strSql);
26         List<TEntity> FindList(string strSql, DbParameter[] dbParameter);
27         List<TEntity> FindList(Pagination pagination);
28         List<TEntity> FindList(Expression<Func<TEntity, bool>> predicate, Pagination pagination);
29     }
30 }
using NFine.Code;using System;using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Linq;using System.Linq.Expressions;using System.Reflection;
using System.Text.RegularExpressions;namespace NFine.Data
{    /// <summary>
    /// 仓储实现    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class RepositoryBase<TEntity> : IRepositoryBase<TEntity> where TEntity : class,new()
    {        public NFineDbContext dbcontext = new NFineDbContext();        
    public int Insert(TEntity entity)
        {
            dbcontext.Entry<TEntity>(entity).State = EntityState.Added;            
            return dbcontext.SaveChanges();
        }        public int Insert(List<TEntity> entitys)
        {            foreach (var entity in entitys)
            {
                dbcontext.Entry<TEntity>(entity).State = EntityState.Added;
            }            return dbcontext.SaveChanges();
        }        public int Update(TEntity entity)
        {
            dbcontext.Set<TEntity>().Attach(entity);
            PropertyInfo[] props = entity.GetType().GetProperties();            
            foreach (PropertyInfo prop in props)
            {                if (prop.GetValue(entity, null) != null)
                {                    if (prop.GetValue(entity, null).ToString() == "&nbsp;")
                        dbcontext.Entry(entity).Property(prop.Name).CurrentValue = null;
                    dbcontext.Entry(entity).Property(prop.Name).IsModified = true;
                }
            }            return dbcontext.SaveChanges();
        }        public int Delete(TEntity entity)
        {
            dbcontext.Set<TEntity>().Attach(entity);
            dbcontext.Entry<TEntity>(entity).State = EntityState.Deleted;            
            return dbcontext.SaveChanges();
        }        public int Delete(Expression<Func<TEntity, bool>> predicate)
        {            var entitys = dbcontext.Set<TEntity>().Where(predicate).ToList();
            entitys.ForEach(m => dbcontext.Entry<TEntity>(m).State = EntityState.Deleted);            
            return dbcontext.SaveChanges();
        }        public TEntity FindEntity(object keyValue)
        {            return dbcontext.Set<TEntity>().Find(keyValue);
        }        public TEntity FindEntity(Expression<Func<TEntity, bool>> predicate)
        {            return dbcontext.Set<TEntity>().FirstOrDefault(predicate);
        }        public IQueryable<TEntity> IQueryable()
        {            return dbcontext.Set<TEntity>();
        }        public IQueryable<TEntity> IQueryable(Expression<Func<TEntity, bool>> predicate)
        {            return dbcontext.Set<TEntity>().Where(predicate);
        }        public List<TEntity> FindList(string strSql)
        {            return dbcontext.Database.SqlQuery<TEntity>(strSql).ToList<TEntity>();
        }        public List<TEntity> FindList(string strSql, DbParameter[] dbParameter)
        {            return dbcontext.Database.SqlQuery<TEntity>(strSql, dbParameter).ToList<TEntity>();
        }        public List<TEntity> FindList(Pagination pagination)
        {            bool isAsc = pagination.sord.ToLower() == "asc" ? true : false;            
        string[] _order = pagination.sidx.Split(',');
            MethodCallExpression resultExp = null;            
            var tempData = dbcontext.Set<TEntity>().AsQueryable();            
            foreach (string item in _order)
            {                string _orderPart = item;
                _orderPart = Regex.Replace(_orderPart, @"\s+", " ");                
                string[] _orderArry = _orderPart.Split(' ');                
                string _orderField = _orderArry[0];                
                bool sort = isAsc;                
                if (_orderArry.Length == 2)
                {
                    isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
                }                var parameter = Expression.Parameter(typeof(TEntity), "t");                
                var property = typeof(TEntity).GetProperty(_orderField);                
                var propertyAccess = Expression.MakeMemberAccess(parameter, property);                
                var orderByExp = Expression.Lambda(propertyAccess, parameter);
                resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", 
                new Type[] { typeof(TEntity), property.PropertyType }, 
                tempData.Expression, Expression.Quote(orderByExp));
            }
            tempData = tempData.Provider.CreateQuery<TEntity>(resultExp);
            pagination.records = tempData.Count();
            tempData = tempData.Skip<TEntity>(pagination.rows * (pagination.page - 1)).
            Take<TEntity>(pagination.rows).AsQueryable();            
            return tempData.ToList();
        }        public List<TEntity> FindList(Expression<Func<TEntity, bool>> predicate, 
        Pagination pagination)
        {            bool isAsc = pagination.sord.ToLower() == "asc" ? true : false;            
        string[] _order = pagination.sidx.Split(',');
            MethodCallExpression resultExp = null;            
            var tempData = dbcontext.Set<TEntity>().Where(predicate);            
            foreach (string item in _order)
            {                string _orderPart = item;
                _orderPart = Regex.Replace(_orderPart, @"\s+", " ");                
                string[] _orderArry = _orderPart.Split(' ');                
                string _orderField = _orderArry[0];                
                bool sort = isAsc;                
                if (_orderArry.Length == 2)
                {
                    isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
                }                var parameter = Expression.Parameter(typeof(TEntity), "t");                
                var property = typeof(TEntity).GetProperty(_orderField);                
                var propertyAccess = Expression.MakeMemberAccess(parameter, property);                
                var orderByExp = Expression.Lambda(propertyAccess, parameter);
                resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", 
                new Type[] { typeof(TEntity), property.PropertyType }, tempData.Expression, 
                Expression.Quote(orderByExp));
            }
            tempData = tempData.Provider.CreateQuery<TEntity>(resultExp);
            pagination.records = tempData.Count();
            tempData = tempData.Skip<TEntity>(pagination.rows * (pagination.page - 1)).
            Take<TEntity>(pagination.rows).AsQueryable();            
            return tempData.ToList();
        }
    }
}

 

复制代码  

自动映射对象实体

 

 

 

框架界面展示:

支持多皮肤切换

 

 

下一篇给大家讲解下如何实现动态皮肤切换

总结:

1:本文并没有详细讲解实现机制。

2:本文并没有详细讲解开发方式。

但,至少你可以:看源码、看API、看Demo,还可以加入技术交流群进行交流分享。

当然,后续我会补充相关文章,更加细化和完善的机制及开发方式。

如果您支持开源精神,在精神层面可以点赞以示鼓励

另外补充:有Bug及漏洞,请私下提交 


1、源码下载地址:http://download.csdn.net/download/mss359681091/9947159

2、配套T4模版生成地址:

 

出处:  http://www.cnblogs.com/huanglin/




转载自:http://blog.csdn.net/mss359681091/article/details/52278863

Home