MVC5 网站开发实践 2、后台管理

目录

MVC5 网站开发实践 概述

MVC5 网站开发实践 1、建立项目

 

从这一部分开始做后台管理,首先是基本框架的

一、Data项目

1、项目添加EntityFramework引用

在Data项目的引用上右键-> 管理NuGet程序包。

1

在弹出的窗口中选择 EntityFramework 安装

2

2、添加排序枚举

在Data项目上点右键新建文件夹Type。在文件夹内添加类【OrderType】,代码如下

//创建2014.12.8



namespace Data.Types

{

    /// <summary>

    /// 排序类型

    /// </summary>

    public enum OrderType

    {

        /// <summary>

        /// 不排序

        /// </summary>

        No,

        /// <summary>

        /// 正序

        /// </summary>

        Asc,

        /// <summary>

        /// 倒序

        /// </summary>

        Desc

    }

}

3、添加数据访问类

在Data项目上点右键新建类输入类名【BaseRepository】,这个类主要负责数数据的存取。

类内添加私有成员DbContext _baseDbContext用来在数据库进行增、删、改和查询。

BaseRepository的构造函数public BaseRepository(DbContext dbContext)中dbContext为数据上下文。在该类中写了一些数据的增删改和查询方法。整个代码如下。

using Ninesky.Data.Types;

using System;

using System.Collections.Generic;

using System.Data.Entity;

using System.Linq;

using System.Linq.Expressions;



namespace Ninesky.Data

{

    /// <summary>

    /// 数据仓储基类

    /// <remarks>

    /// 创建:2014.12.13

    /// </remarks>

    /// </summary>

    /// <typeparam name="T"></typeparam>

    public class BaseRepository<T> where T : class

    {

        private DbContext _baseDbContext;

        /// <summary>

        /// 构造函数

        /// </summary>

        /// <param name="dbContext">数据上下文</param>

        public BaseRepository(DbContext dbContext)

        {

            _baseDbContext = dbContext;

        }



        /// <summary>

        /// 添加

        /// </summary>

        /// <param name="entity">数据实体</param>

        /// <param name="isSave">是否立即保存</param>

        /// <returns></returns>

        public T Add(T entity, bool isSave = true)

        {

            _baseDbContext.Set<T>().Add(entity);

            if (isSave) _baseDbContext.SaveChanges();

            return entity;

        }



        /// <summary>

        /// 批量添加【立即执行】

        /// </summary>

        /// <param name="entities">数据列表</param>

        /// <returns>添加的记录数</returns>

        public int AddRange(IEnumerable<T> entities, bool isSave = true)

        {

            _baseDbContext.Set<T>().AddRange(entities);

            return isSave ? _baseDbContext.SaveChanges() : 0;

        }



        /// <summary>

        /// 修改

        /// </summary>

        /// <param name="entity">数据实体</param>

        /// <param name="isSave">是否立即保存</param>

        /// <returns></returns>

        public bool Update(T entity, bool isSave = true)

        {

            _baseDbContext.Set<T>().Attach(entity);

            _baseDbContext.Entry<T>(entity).State = EntityState.Modified;

            return isSave ? _baseDbContext.SaveChanges() > 0 : true;

        }



        /// <summary>

        /// 删除

        /// </summary>

        /// <param name="entity">数据实体</param>

        /// <param name="isSave">是否立即保存</param>

        /// <returns></returns>

        public bool Delete(T entity, bool isSave = true)

        {

            _baseDbContext.Set<T>().Attach(entity);

            _baseDbContext.Entry<T>(entity).State = EntityState.Deleted;

            return isSave ? _baseDbContext.SaveChanges() > 0 : true;

        }



        /// <summary>

        /// 批量删除

        /// </summary>

        /// <param name="entities">数据列表</param>

        /// <param name="isSave">是否立即保存</param>

        /// <returns>删除的记录数</returns>

        public int DeleteRange(IEnumerable<T> entities, bool isSave = true)

        {

            _baseDbContext.Set<T>().RemoveRange(entities);

            return isSave ? _baseDbContext.SaveChanges() : 0;

        }



        /// <summary>

        /// 保存

        /// </summary>

        /// <returns>受影响的记录数</returns>

        public int Save()

        {

            return _baseDbContext.SaveChanges();

        }



        /// <summary>

        /// 是否有满足条件的记录

        /// </summary>

        /// <param name="anyLamdba">条件表达式</param>

        /// <returns></returns>

        public bool Any(Expression<Func<T, bool>> anyLamdba)

        {

            return _baseDbContext.Set<T>().Any(anyLamdba);

        }



        /// <summary>

        /// 查询记录数

        /// </summary>

        /// <param name="countLamdba">查询表达式</param>

        /// <returns>记录数</returns>

        public int Count(Expression<Func<T, bool>> countLamdba)

        {

            return _baseDbContext.Set<T>().Count(countLamdba);

        }





        /// <summary>

        /// 查找实体

        /// </summary>

        /// <param name="ID">实体ID</param>

        /// <returns></returns>

        public T Find(int ID)

        {

            return _baseDbContext.Set<T>().Find(ID);

        }



        /// <summary>

        /// 查找实体 

        /// </summary>

        /// <param name="findLambda">Lambda表达式</param>

        /// <returns></returns>

        public T Find(Expression<Func<T, bool>> findLambda)

        {

            return _baseDbContext.Set<T>().SingleOrDefault(findLambda);

        }



        /// <summary>

        /// 查找所有列表

        /// </summary>

        /// <returns></returns>

        public IQueryable<T> FindAll()

        {

            return FindList<int>(0, T => true, OrderType.No, null);

        }



        /// <summary>

        /// 查找数据列表

        /// </summary>

        /// <param name="number">返回的记录数【0-返回所有】</param>

        /// <param name="whereLandba">查询条件</param>

        /// <param name="orderType">排序方式</param>

        /// <param name="orderLandba">排序条件</param>

        /// <returns></returns>

        public IQueryable<T> FindList<TKey>(int number, Expression<Func<T, bool>> whereLandba, OrderType orderType, Expression<Func<T, TKey>> orderLandba)

        {

            IQueryable<T> _tIQueryable = _baseDbContext.Set<T>().Where(whereLandba);

            switch (orderType)

            {

                case OrderType.Asc:

                    _tIQueryable = _tIQueryable.OrderBy(orderLandba);

                    break;

                case OrderType.Desc:

                    _tIQueryable = _tIQueryable.OrderByDescending(whereLandba);

                    break;

            }

            if (number > 0) _tIQueryable = _tIQueryable.Take(number);

            return _tIQueryable;

        }



        /// <summary>

        /// 

        /// </summary>

        /// <typeparam name="TKey">排序字段类型</typeparam>

        /// <param name="pageIndex">页码【从1开始】</param>

        /// <param name="pageNumber">每页记录数</param>

        /// <param name="totalNumber">总记录数</param>

        /// <param name="whereLandba">查询表达式</param>

        /// <param name="orderType">排序类型</param>

        /// <param name="orderLandba">排序表达式</param>

        /// <returns></returns>

        public IQueryable<T> FindPageList<TKey>(int pageIndex, int pageNumber, out int totalNumber, Expression<Func<T, bool>> whereLandba, OrderType orderType, Expression<Func<T, TKey>> orderLandba)

        {

            IQueryable<T> _tIQueryable = _baseDbContext.Set<T>().Where(whereLandba);

            totalNumber = _tIQueryable.Count();

            switch (orderType)

            {

                case OrderType.Asc:

                    _tIQueryable = _tIQueryable.OrderBy(orderLandba);

                    break;

                case OrderType.Desc:

                    _tIQueryable = _tIQueryable.OrderByDescending(whereLandba);

                    break;

            }

            _tIQueryable = _tIQueryable.Skip((pageIndex - 1) * pageNumber).Take(pageNumber);

            return _tIQueryable;

        }

    }

}

4、添加业务逻辑基类

在data项目右键添加【BaseService】,此类通过调用BaseRepository进行数据处理,作为业务逻辑的基类,其他业务类继承自此类。这个类的方法与BaseRepository比较相似,建立此类的主要体现分层的思路,一些数据访问的异常处理应该放在此类中。代码如下:

using Ninesky.Data.Types;

using System;

using System.Collections.Generic;

using System.Data.Entity;

using System.Linq;

using System.Linq.Expressions;



namespace Ninesky.Data

{

    /// <summary>

    /// 业务逻辑基类

    /// <remarks>

    /// 创建:2014.12.13

    /// </remarks>

    /// </summary>

    public class BaseService<T> where T : class

    {

        private BaseRepository<T> _baseRepository;



        /// <summary>

        /// 创建业务逻辑类

        /// </summary>

        /// <param name="dbContext">数据上下文</param>

        public BaseService(DbContext dbContext)

        {

            _baseRepository = new BaseRepository<T>(dbContext);

        }



        /// <summary>

        /// 添加

        /// </summary>

        /// <param name="entity">数据实体</param>

        /// <returns>添加后的数据实体</returns>

        public T Add(T entity, bool isSave = true)

        {

            return _baseRepository.Add(entity, isSave);

        }



        ///// <summary>

        ///// 添加【必须先实例化才能使用】

        ///// </summary>

        ///// <param name="entity">数据实体</param>

        ///// <returns>添加后的记录ID</returns>

        //public virtual int Add(T entity) { return 0; }



        /// <summary>

        /// 批量添加

        /// </summary>

        /// <param name="entities">数据列表</param>

        /// <param name="isSave">是否立即保存</param>

        /// <returns></returns>

        public int AddRange(IEnumerable<T> entities, bool isSave = true)

        {

            return _baseRepository.AddRange(entities, isSave);

        }



        /// <summary>

        /// 修改

        /// </summary>

        /// <param name="entity">数据实体</param>

        /// <param name="isSave">是否立即保存</param>

        /// <returns></returns>

        public bool Update(T entity, bool isSave = true)

        {

            return _baseRepository.Update(entity, isSave);

        }



        /// <summary>

        /// 删除

        /// </summary>

        /// <param name="entity">数据实体</param>

        /// <param name="isSave">是否立即保存</param>

        /// <returns></returns>

        public bool Delete(T entity, bool isSave = true)

        {

            return _baseRepository.Delete(entity, isSave);

        }



        /// <summary>

        /// 批量删除

        /// </summary>

        /// <param name="entities">数据列表</param>

        /// <param name="isSave">是否立即保存</param>

        /// <returns>删除的记录数</returns>

        public int DeleteRange(IEnumerable<T> entities, bool isSave = true)

        {

            return _baseRepository.DeleteRange(entities, isSave);

        }



        /// <summary>

        /// 保存

        /// </summary>

        /// <returns>受影响的记录数</returns>

        public int Save()

        {

            return _baseRepository.Save();

        }



        /// <summary>

        /// 查询记录数

        /// </summary>

        /// <param name="countLamdba">查询表达式</param>

        /// <returns>记录数</returns>

        public int Count(Expression<Func<T, bool>> countLamdba)

        {

            return _baseRepository.Count(countLamdba);

        }





        /// <summary>

        /// 查找实体

        /// </summary>

        /// <param name="ID">实体ID</param>

        /// <returns></returns>

        public T Find(int ID)

        {

            return _baseRepository.Find(ID);

        }



        /// <summary>

        /// 查找实体 

        /// </summary>

        /// <param name="findLambda">Lambda表达式</param>

        /// <returns></returns>

        public T Find(Expression<Func<T, bool>> findLambda)

        {

            return _baseRepository.Find(findLambda);

        }



        /// <summary>

        /// 查找所有列表

        /// </summary>

        /// <returns></returns>

        public IQueryable<T> FindAll()

        {

            return _baseRepository.FindAll();

        }



        /// <summary>

        /// 查找数据列表

        /// </summary>

        /// <param name="number">返回的记录数【0-返回所有】</param>

        /// <param name="whereLandba">查询条件</param>

        /// <param name="orderType">排序方式</param>

        /// <param name="orderLandba">排序条件</param>

        /// <returns></returns>

        public IQueryable<T> FindList<TKey>(int number, Expression<Func<T, bool>> whereLandba, OrderType orderType, Expression<Func<T, TKey>> orderLandba)

        {

            return _baseRepository.FindList<TKey>(number, whereLandba, orderType, orderLandba);

        }



        /// <summary>

        /// 

        /// </summary>

        /// <typeparam name="TKey">排序字段类型</typeparam>

        /// <param name="pageIndex">页码【从1开始】</param>

        /// <param name="pageNumber">每页记录数</param>

        /// <param name="totalNumber">总记录数</param>

        /// <param name="whereLandba">查询表达式</param>

        /// <param name="orderType">排序类型</param>

        /// <param name="orderLandba">排序表达式</param>

        /// <returns></returns>

        public IQueryable<T> FindPageList<TKey>(int pageIndex, int pageNumber, out int totalNumber, Expression<Func<T, bool>> whereLandba, OrderType orderType, Expression<Func<T, TKey>> orderLandba)

        {

            return FindPageList<TKey>(pageIndex, pageNumber, out totalNumber, whereLandba, orderType, orderLandba);

        }

    }

}

二、User项目

1、建立管理员模型

在User项目的引用上点右键添加System.ComponentModel.DataAnnotations的引用和Ninesky.Data项目、E的引用。

添加引用

在User项目上点右键新建文件夹Models,在文件夹中添加类Administrator。代码如下:

using System;

using System.ComponentModel.DataAnnotations;



namespace Ninesky.User.Models

{

    /// <summary>

    /// 管理员模型

    /// <remarks>

    /// 创建:2014.12.13

    /// </remarks>

    /// </summary>

    public class Administrator

    {

        [Key]

        public int AdministratorID { get; set; }



        /// <summary>

        /// 管理员账号

        /// </summary>

        [Required(ErrorMessage = "必须输入{0}")]

        [Display(Name = "管理员账号")]

        public string Account { get; set; }



        /// <summary>

        /// 管理员名称

        /// </summary>

        [Required(ErrorMessage = "必须输入{0}")]

        [Display(Name = "管理员名称")]

        public string Name { get; set; }

        /// <summary>

        /// 密码

        /// </summary>

        [Required(ErrorMessage = "必须输入{0}")]

        [Display(Name = "密码")]

        [DataType(DataType.Password)]

        public string Password { get; set; }



        /// <summary>

        /// 创建时间

        /// </summary>

        public DateTime CreateDate { get; set; }



        /// <summary>

        /// 上次登陆Ip

        /// </summary>

        public string LoginIp { get; set; }



        /// <summary>

        /// 上次登陆日期

        /// </summary>

        public Nullable<DateTime> LoginTime { get; set; }

    }

}

2、建立管理员业务逻辑类

在User项目上点右键添加AdministratorService类,该类继承自BaseService。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Ninesky.Data;

using Ninesky.User.Models;



namespace Ninesky.User

{

    /// <summary>

    /// 管理员业务逻辑类

    /// <remarks>

    /// 创建:2014.12.13

    /// </remarks>

    /// </summary>

    public class AdministratorService:BaseService<Administrator>

    {  public AdministratorService(DbContext dbContext) : base(dbContext) { }        

    }

}

三、Website项目

1、添加User项目的引用上右键添加对User项目的引用。

2、建立数据上下文

在Site项目上点右键添加类【NineskyDbContext】,继承自DbContext

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Data.Entity;

using Ninesky.User.Models;



namespace Ninesky.Website

{

    /// <summary>

    /// 网站数据上下文

    /// <remarks>

    /// 创建:2014.12.13

    /// </remarks>

    /// </summary>

    public class NineskyDbContext : DbContext

    {

        public DbSet<Administrator> Administrators { get; set; }



        public NineskyDbContext()

            : base("name=NineskyConnection")

        {

            Database.SetInitializer<NineskyDbContext>(new CreateDatabaseIfNotExists<NineskyDbContext>());

        }

    }

}

3、建立数据库连接

打开Web.config文件,在</configSections><appSettings>之间添加数据库的连接

<connectionStrings>

    <add name="NineskyConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\Ninesky.mdf;Initial Catalog=Ninesky;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

  </connectionStrings>

4、建立简单工厂

在Website项目上点右键添加类【ContextFactory】,代码如下:

using System.Runtime.Remoting.Messaging;



namespace Ninesky.Website

{

    /// <summary>

    /// 上下文简单工厂

    /// <remarks>

    /// 创建:2014.12.13

    /// </remarks>

    /// </summary>

    public class ContextFactory

    {

        /// <summary>

        /// 获取当前数据上下文

        /// </summary>

        /// <returns>数据上下文</returns>

        public static NineskyDbContext GetCurrentContext()

        {

            NineskyDbContext _nineskyDbContext = CallContext.GetData("NineskyDbContext") as NineskyDbContext;

            if (_nineskyDbContext == null)

            {

                _nineskyDbContext = new NineskyDbContext();

                CallContext.SetData("NineskyDbContext", _nineskyDbContext);

            }

            return _nineskyDbContext;

        }

    }

}

5、Config区域主界面

在Site项目的Config区域的Controller文件夹上点右键添加Home控制器,类型选择【MVC5控制器-空】。在添加的控制器中添加【Index】 action

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;



namespace Ninesky.Website.Areas.Config.Controllers

{

    /// <summary>

    /// 主界面

    /// <remarks>

    /// 创建:2014.12.13

    /// </remarks>

    /// </summary>

    public class HomeController : Controller

    {

        // GET: Config/Home

        public ActionResult Index()

        {

            return View();

        }

    }

}

在Config区域Views/Share文件夹添加布局页【_Layout.cshtml】

在Index action上右键添加一个空视图

image

===========================

代码下载:http://pan.baidu.com/s/1sj6Ya2D

你可能感兴趣的:(网站开发)