如何进行电子商务网站建设,实体店做团购有那些网站,微博问答网站开发,莱芜市住房和城乡建设厅网站写在前面 上篇文章简单介绍了项目的结构#xff0c;这篇文章将实现用户的注册。当然关于漂亮的ui#xff0c;这在追后再去添加了#xff0c;先将功能实现。也许代码中有不合适的地方#xff0c;也只有在之后慢慢去优化了。 系列文章 [EF]vs15ef6mysql code first方式 [实战…写在前面 上篇文章简单介绍了项目的结构这篇文章将实现用户的注册。当然关于漂亮的ui这在追后再去添加了先将功能实现。也许代码中有不合适的地方也只有在之后慢慢去优化了。 系列文章 [EF]vs15ef6mysql code first方式 [实战]MVC5EF6MySql企业网盘实战(1) [实战]MVC5EF6MySql企业网盘实战(2)——用户注册 实现 Model层 UserInfo实体模型 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using Wolfy.NetDisk.Utilities;namespace Wolfy.NetDisk.Model
{/// summary/// 用户信息类/// /summarypublic class UserInfo{/// summary/// 编号/// /summary[Key]public int Id { set; get; }/// summary/// 用户头像地址/// /summary[StringLength(512)][Display(Name 头像)]public string Header { set; get; }/// summary/// 姓名/// /summary[MaxLength(64, ErrorMessage 网名长度不得超过32个字符)][Required(ErrorMessage 请填写您的名称)][Display(Name 姓名)]public string Name { set; get; }/// summary/// 网名/// /summary[MaxLength(64, ErrorMessage 网名长度不得超过32个字符)][Required(ErrorMessage 请填写您的网名)][Display(Name 网名)]public string DisplayName { set; get; }/// summary/// 邮箱/// /summary[RegularExpression(^(\w)(\.\w)*(\w)((\.\w))$, ErrorMessage 请输入正确的邮箱地址)][MaxLength(2048, ErrorMessage 网名长度不得超过2048个字符)][Required][Display(Name 邮箱)]public string Email { set; get; }/// summary/// 婚姻情况/// /summary[Display(Name 婚姻状况)]public MarriageType Marriage { set; get; }/// summary/// 政治面貌/// /summary[Display(Name 政治面貌)]public PoliticalStatus PoliticalStatus { set; get; }/// summary/// 学历/// /summary[Display(Name 学历)]public XueliType Xueli { set; get; }/// summary/// 职位/// /summary[MaxLength(128)][Display(Name 职位)]public string Position { set; get; }/// summary/// 电话/// /summary[MaxLength(32)][Display(Name 电话)]public string Tel { set; get; }/// summary/// 密码/// /summary[StringLength(32, ErrorMessage 密码长度不得超多32位)][Required][Display(Name 密码)]public string Pwd { set; get; }/// summary/// 生日/// /summary[Display(Name 生日)]public DateTime Birthday { set; get; } DateTime.Now;/// summary/// 性别/// /summary[Display(Name 性别)]public GenderType Gender { set; get; }/// summary/// 住址/// /summary[MaxLength(32)][Display(Name 地址)]public string Address { set; get; }/// summary/// 籍贯/// /summary[MaxLength(32)][Display(Name 籍贯)]public string Hometown { set; get; }/// summary/// 公司/// /summary[StringLength(512, ErrorMessage 公司名称超过了512字符)][Display(Name 公司名称)]public string Company { set; get; }/// summary/// 所属部门id/// /summary[Display(Name 部门)]public int DepartmentId { set; get; }/// summary/// 添加时间/// /summary[Display(Name 日期)]public DateTime Dt { set; get; } DateTime.Now;}
} UserInfo IDAL层 添加泛型接口IBaseRepositoryTEntity存放一些常用的操作增删改查等。 /// summary/// 仓储基类泛型接口/// /summary/// typeparam nameTEntity/typeparampublic interface IBaseRepositoryTEntity : IDisposable{/// summary/// 添加实体/// /summary/// param nameentity/param/// returns/returnsTEntity Add(TEntity entity);/// summary/// 计数/// /summary/// param namewhere/param/// returns/returnsint Count(ExpressionFuncTEntity, bool where);/// summary/// 更新/// /summary/// param nameentity/param/// returns/returnsTEntity Update(TEntity entity);bool Delete(TEntity entity);/// summary/// 是否存在/// /summary/// param namewhere/param/// returns/returnsbool Exist(ExpressionFuncTEntity, bool where);/// summary/// 条件查询/// /summary/// param namewhere/param/// returns/returnsTEntity Find(ExpressionFuncTEntity, bool where);/// summary/// 查询集合/// /summary/// param namewhere/param/// returns/returnsIQueryableTEntity FindAll(ExpressionFuncTEntity, bool where);/// summary/// 条件查询/// /summary/// typeparam nameSEntity/typeparam/// param namewhere/param/// param nameisAsc是否升序/param/// param nameorderlanbda排序表达式/param/// returns/returnsIQueryableTEntity FindAllSEntity(ExpressionFuncTEntity, bool where, bool isAsc, ExpressionFuncTEntity, SEntity orderlanbda);/// summary/// 分页查询/// /summary/// typeparam nameSEntity/typeparam/// param namepageIndex/param/// param namepageSize/param/// param nametotalRecord/param/// param namewhere/param/// param nameisAsc/param/// param nameorderLambda/param/// returns/returnsIQueryableTEntity FindPagedSEntity(int pageIndex, int pageSize, out int totalRecord, ExpressionFuncTEntity, bool where, bool isAsc, ExpressionFuncTEntity, SEntity orderLambda);/// summary/// 保存/// /summary/// returns/returnsint SaveChanges();} IBaseRepository IUserInfoRepositoryUserInfo操作接口。 /// summary/// 用户信息仓储接口/// /summarypublic interface IUserInfoRepository : IBaseRepositoryUserInfo{} IUserInfoRepository DAL层 添加数据库上下文NetDiskContext类。关于ef6mysql code first的具体使用可以参考前面的文章。 /// summary/// 数据库上下文/// /summarypublic class NetDiskContext : DbContext{/// summary/// name:数据库连接字符串/// /summarypublic NetDiskContext(): base(nameNetDiskContext){}public DbSetUserInfo UserInfos { set; get; }//public DbSetDepartment Deparments { set; get; }//public DbSetModel.NetDisk NetDisks { set; get; }} NetDiskContext ContextFactory用来获取数据上下文的工厂代码如下第一次使用ef也不知道有没有更好的实现方式先这样实现吧以后发现更好的方式再进行优化。 /// summary/// 数据上下文工厂类/// /summarypublic static class ContextFactory{/// summary/// 获取数据库上下文/// /summary/// returns/returnspublic static DbContext GetDbContext(){NetDiskContext _netDiskContext CallContext.GetData(NetDiskContext) as NetDiskContext;if (_netDiskContext null){_netDiskContext new NetDiskContext();IDatabaseInitializerNetDiskContext dbInitializer null;if (_netDiskContext.Database.Exists()){//如果数据库已经存在dbInitializer new DropCreateDatabaseIfModelChangesNetDiskContext();}else{//总是先删除然后再创建dbInitializer new DropCreateDatabaseAlwaysNetDiskContext();}dbInitializer.InitializeDatabase(_netDiskContext);CallContext.SetData(NetDiskContext, _netDiskContext);return _netDiskContext;}return _netDiskContext;}} ContextFactory BaseRepository泛型基类实现接口IBaseRepository using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Wolfy.NetDisk.IDAL;namespace Wolfy.NetDisk.DAL
{/// summary/// 仓储基类/// /summarypublic class BaseRepositoryTEntity : IBaseRepositoryTEntity where TEntity : class{protected NetDiskContext netDiskContext ContextFactory.GetDbContext() as NetDiskContext;public TEntity Add(TEntity entity){netDiskContext.EntryTEntity(entity).State System.Data.Entity.EntityState.Added;return entity;}public int Count(ExpressionFuncTEntity, bool where){return netDiskContext.SetTEntity().Count(where);}public bool Delete(TEntity entity){netDiskContext.EntryTEntity(entity).State System.Data.Entity.EntityState.Deleted;return this.SaveChanges() 0;}public void Dispose(){if (netDiskContext ! null){netDiskContext.Dispose();GC.SuppressFinalize(netDiskContext);}}public bool Exist(ExpressionFuncTEntity, bool where){return netDiskContext.SetTEntity().Any(where);}public TEntity Find(ExpressionFuncTEntity, bool where){return netDiskContext.SetTEntity().FirstOrDefault(where);}public IQueryableTEntity FindAll(ExpressionFuncTEntity, bool where){return netDiskContext.SetTEntity().Where(where);}public IQueryableTEntity FindAllSEntity(ExpressionFuncTEntity, bool where, bool isAsc, ExpressionFuncTEntity, SEntity orderlanbda){var lst netDiskContext.SetTEntity().WhereTEntity(where);if (!isAsc){lst lst.OrderByDescendingTEntity, SEntity(orderlanbda);}return lst;}public IQueryableTEntity FindPagedSEntity(int pageIndex, int pageSize, out int totalRecord, ExpressionFuncTEntity, bool where, bool isAsc, ExpressionFuncTEntity, SEntity orderLambda){var lst netDiskContext.SetTEntity().WhereTEntity(where);totalRecord lst.Count();if (!isAsc){lst lst.OrderByDescendingTEntity, SEntity(orderLambda);}return lst.SkipTEntity((pageIndex - 1) * pageIndex).Take(pageSize);}public int SaveChanges(){return netDiskContext.SaveChanges();}public TEntity Update(TEntity entity){TEntity tentity netDiskContext.SetTEntity().Attach(entity);netDiskContext.EntryTEntity(entity).State System.Data.Entity.EntityState.Modified;return tentity;}}
} BaseRepository /// summary/// 用户数据操作dal层 /// /summarypublic class UserInfoRepository:BaseRepositoryUserInfo, IUserInfoRepository{} UserInfoRepository 仓储工厂用来获取具体的仓储接口。 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wolfy.NetDisk.IDAL;
using Wolfy.NetDisk.Model;namespace Wolfy.NetDisk.DAL
{/// summary/// 仓储工厂/// /summarypublic static class RepositoryFactory{public static IUserInfoRepository UserInfoRepository { get { return new UserInfoRepository(); } }}
} RepositoryFactory IBLL层 using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;namespace Wolfy.NetDisk.IBLL
{public interface IBaseServiceRepositoryTEntity{/// summary/// 添加实体/// /summary/// param nameentity/param/// returns/returnsTEntity Add(TEntity entity);/// summary/// 计数/// /summary/// param namewhere/param/// returns/returnsint Count(ExpressionFuncTEntity, bool where);/// summary/// 更新/// /summary/// param nameentity/param/// returns/returnsTEntity Update(TEntity entity);bool Delete(TEntity entity);/// summary/// 是否存在/// /summary/// param namewhere/param/// returns/returnsbool Exist(ExpressionFuncTEntity, bool where);/// summary/// 条件查询/// /summary/// param namewhere/param/// returns/returnsTEntity Find(ExpressionFuncTEntity, bool where);/// summary/// 查询集合/// /summary/// param namewhere/param/// returns/returnsIQueryableTEntity FindAll(ExpressionFuncTEntity, bool where);/// summary/// 条件查询/// /summary/// typeparam nameSEntity/typeparam/// param namewhere/param/// param nameisAsc是否升序/param/// param nameorderlanbda排序表达式/param/// returns/returnsIQueryableTEntity FindAllSEntity(ExpressionFuncTEntity, bool where, bool isAsc, ExpressionFuncTEntity, SEntity orderlanbda);/// summary/// 分页查询/// /summary/// typeparam nameSEntity/typeparam/// param namepageIndex/param/// param namepageSize/param/// param nametotalRecord/param/// param namewhere/param/// param nameisAsc/param/// param nameorderLambda/param/// returns/returnsIQueryableTEntity FindPagedSEntity(int pageIndex, int pageSize, out int totalRecord, ExpressionFuncTEntity, bool where, bool isAsc, ExpressionFuncTEntity, SEntity orderLambda);int SaveChanges();}
} IBaseServiceRepository using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wolfy.NetDisk.Model;namespace Wolfy.NetDisk.IBLL
{public interface IUserInfoServiceRepository:IBaseServiceRepositoryUserInfo{}
} IUserInfoServiceRepository BLL层 using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Wolfy.NetDisk.IBLL;
using Wolfy.NetDisk.IDAL;namespace Wolfy.NetDisk.BLL
{public class BaseServiceRepositoryTEntity : IBaseServiceRepositoryTEntity where TEntity : class,new(){protected IBaseRepositoryTEntity currentRepository { set; get; }public BaseServiceRepository(IBaseRepositoryTEntity currentRepository){this.currentRepository currentRepository;}public TEntity Add(TEntity entity){return currentRepository.Add(entity);}public int Count(ExpressionFuncTEntity, bool where){return currentRepository.Count(where);}public bool Delete(TEntity entity){return currentRepository.Delete(entity);}public bool Exist(ExpressionFuncTEntity, bool where){return currentRepository.Exist(where);}public TEntity Find(ExpressionFuncTEntity, bool where){return currentRepository.Find(where);}public IQueryableTEntity FindAll(ExpressionFuncTEntity, bool where){return currentRepository.FindAll(where);}public IQueryableTEntity FindAllSEntity(ExpressionFuncTEntity, bool where, bool isAsc, ExpressionFuncTEntity, SEntity orderlanbda){return currentRepository.FindAllSEntity(where, isAsc, orderlanbda);}public IQueryableTEntity FindPagedSEntity(int pageIndex, int pageSize, out int totalRecord, ExpressionFuncTEntity, bool where, bool isAsc, ExpressionFuncTEntity, SEntity orderLambda){return currentRepository.FindPagedSEntity(pageIndex, pageSize, out totalRecord, where, isAsc, orderLambda);}public int SaveChanges(){return currentRepository.SaveChanges();}public TEntity Update(TEntity entity){return currentRepository.Update(entity);}}
} BaseServiceRepository using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wolfy.NetDisk.Model;
using Wolfy.NetDisk.DAL;
using Wolfy.NetDisk.IBLL;
using Wolfy.NetDisk.IDAL;namespace Wolfy.NetDisk.BLL
{public class UserInfoServiceRepository : BaseServiceRepositoryUserInfo, IUserInfoServiceRepository{/// summary/// 构造函数通过仓储工厂调用dal中的具体的仓储/// /summary/// param namecurrentRepository/parampublic UserInfoServiceRepository(): base(RepositoryFactory.UserInfoRepository){}}
} UserInfoServiceRepository UI层 添加UserInfo控制器 using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Wolfy.NetDisk.Model;
using Wolfy.NetDisk.IBLL;
using Wolfy.NetDisk.BLL;
namespace Wolfy.NetDisk.Site.Controllers
{public class UserInfoController : AsyncController{private IUserInfoServiceRepository _userInfoServiceRepository new UserInfoServiceRepository();/// summary/// 用户信息列表/// /summary/// returns/returnspublic ActionResult Users(){var users _userInfoServiceRepository.FindAll(x x.DisplayName ! );return View(users);}[HttpGet]public ActionResult Register(){return View();}[HttpPost]public ActionResult Register(UserInfo userInfo){if (ModelState.IsValid){_userInfoServiceRepository.Add(userInfo);_userInfoServiceRepository.SaveChanges();}return RedirectToAction(Users);}}
} UserInfoController 添加视图 先不管界面的美与丑先试下能否正确的添加数据。如果成功下一步再进行美化。 总结 下面将完善注册的过程用户名是否存在验证密码加密验证码等操作。 转载于:https://www.cnblogs.com/wolf-sun/p/4823280.html