MVC学习笔记_ORM_框架_解耦

Top

ORM (Object Relational Mapping)

  • EF
    匿名表达式: 筛选部分列
db.UserInfo.Where(u=>u.Id==1).Select(c=>new{Name=c.UserName,Pwd=c.UserPwd});

MVC

  • HTMLHelper
<%=Html.ActionLink("首页","Index")%>

点击跳转

//等号输出=渲染Html标签
<%=Html.ActionLink("首页","Index")%>
//冒号输出:Server.HtmlEncode 操作 (纯文本输出)
//可避免XSS攻击
<%:Html.ActionLink("首页","Index")%>
  • 自带的Ajax方法
<%using(Ajax.BeginForm("Index",new AjaxOptions{Confirm="确定?",InsertionMode=System.Web.Mvc.Ajax.InsertionMode.Replace,UpdateTargetId="div1"}))%>
  • 分页
    • HTmlHelper 扩展:分页 //可简单引入样式
        HtmlHelper的扩展:
        注意点:扩展方法必须是静态方法,所在的类必须是静态类,所在的命名空间改成System.Web.MVC则能省略页面中必须添加命名空间的约束。
        //主要就是输出分页的超级链接的标签
        //自定义分页Helper扩展
        public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount)
        {
            var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
            pageSize = pageSize == 0 ? 3 : pageSize;
            var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
            var output = new StringBuilder();
            if (totalPages > 1)
            {
                //if (currentPage != 1)
                {//处理首页连接
                    output.AppendFormat("首页 ", redirectTo, pageSize);
                }
                if (currentPage > 1)
                {//处理上一页的连接
                    output.AppendFormat("上一页 ", redirectTo, currentPage - 1, pageSize);
                }
                else
                {
                   // output.Append("上一页");
                }

                output.Append(" ");
                int currint = 5;
                for (int i = 0; i <= 10; i++)
                {//一共最多显示10个页码,前面5个,后面5个
                    if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
                    {
                        if (currint == i)
                        {//当前页处理
                            //output.Append(string.Format("[{0}]", currentPage));
                            output.AppendFormat("{3} ", redirectTo, currentPage , pageSize, currentPage );
                        }
                        else
                        {//一般页处理
                            output.AppendFormat("{3} ", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint);
                        }
                    }
                    output.Append(" ");
                }
                if (currentPage < totalPages)
                {//处理下一页的链接
                    output.AppendFormat("下一页 ", redirectTo, currentPage + 1, pageSize);
                }
                else
                {
                    //output.Append("下一页");
                }
                output.Append(" ");
                if (currentPage != totalPages)
                {
                    output.AppendFormat("末页 ", redirectTo, totalPages, pageSize);
                }
                output.Append(" ");
            }
            output.AppendFormat("第{0}页 / 共{1}页", currentPage, totalPages);//这个统计加不加都行

            return new HtmlString(output.ToString());
        }

        //样式部分:
        body
{
}

.paginator
{
    font: 12px Arial, Helvetica, sans-serif;
    padding: 10px 20px 10px 0;
    margin: 0px;
}

.paginator a
{
    border: solid 1px #ccc;
    color: #0063dc;
    cursor: pointer;
    text-decoration: none;
}

.paginator a:visited
{
    padding: 1px 6px;
    border: solid 1px #ddd;
    background: #fff;
    text-decoration: none;
}

.paginator .cpb
{
    border: 1px solid #F50;
    font-weight: 700;
    color: #F50;
    background-color: #ffeee5;
}

.paginator a:hover
{
    border: solid 1px #F50;
    color: #f60;
    text-decoration: none;
}

.paginator a, .paginator a:visited, .paginator .cpb, .paginator a:hover
{
    float: left;
    height: 16px;
    line-height: 16px;
    min-width: 10px;
    _width: 10px;
    margin-right: 5px;
    text-align: center;
    white-space: nowrap;
    font-size: 12px;
    font-family: Arial,SimSun;
    padding: 0 3px;
}

点击跳转Top

架构构建

  • 引用 Model 需引用 EntityFramework & Date.Entity;
  • DBSession
    • 数据访问会话层,负责创建所有的数据操作类对象,业务层制药获取到DBSession,就拿到了所有的数据操作类的实例,让业务层与数据层进行解耦。
    • ExecuteSql 执行特殊sql语句
    • 数据层不再执行SaveChanges
  public class DBSession:IDBSession
    {
        DbContext db = new ReportServerEntities();

        private Users _usersDAL;

        public Users UsersDAL
        {
            get
            {
                if (_usersDAL == null)
                {
                    _usersDAL = AbstractFactory.CreateUsers();//抽象工程

                }
                return _usersDAL;
            }
            set { _usersDAL = value; }
        }
        /// 
        /// 执行特殊sql语句
        /// 
        /// 
        /// 
        /// 
        public int ExecuteSql(string sql, params SqlParameter pars)
        {
            return db.Database.ExecuteSqlCommand(sql, pars);
        }
        public int SaveChanges()
        {
            return db.SaveChanges();
        }
    }

点击跳转

DbContextFactory

  • 负责创建EF上下文对象、保证线程内唯一、多次请求只创建一个对象
    public class DbContextFactory
    {

        public static DbContext GetCurrentDbContext()
        {
            DbContext dbContext = (DbContext)CallContext.GetData("DbContext");
            if (dbContext==null)
            {
                dbContext =  new ReportServerEntities();
                CallContext.SetData("DbContext", dbContext);
            } 
            return dbContext;
        } 
    }

点击跳转

封装Base注意项_IEnumerable

  • System.Linq.Expressions.Expression<>
  • IEnumerable 直接获取数据
  • IQueryable 转换为Sql语句 、使用时再进行获取数据
//默认直接获取  IEnumerable
  IQueryable LoadEntities(Funcbool> whereLambda);

//获取         IQueryable
  IQueryable LoadEntities(System.Linq.Expressions.Expressionbool>> whereLambda);

点击跳转

你可能感兴趣的:(学习,IEnumerabl,面向对象)