[实战]MVC5+EF6+MySql企业网盘实战(2)——用户注册

写在前面

上篇文章简单介绍了项目的结构,这篇文章将实现用户的注册。当然关于漂亮的ui,这在追后再去添加了,先将功能实现。也许代码中有不合适的地方,也只有在之后慢慢去优化了。

系列文章

[EF]vs15+ef6+mysql code first方式

[实战]MVC5+EF6+MySql企业网盘实战(1)

[实战]MVC5+EF6+MySql企业网盘实战(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>/// 用户信息类/// </summary>public 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层

添加泛型接口IBaseRepository<TEntity>,存放一些常用的操作,增删改查等。

  /// <summary>/// 仓储基类泛型接口/// </summary>/// <typeparam name="TEntity"></typeparam>public interface IBaseRepository<TEntity> : IDisposable{/// <summary>/// 添加实体/// </summary>/// <param name="entity"></param>/// <returns></returns>
        TEntity Add(TEntity entity);/// <summary>/// 计数/// </summary>/// <param name="where"></param>/// <returns></returns>int Count(Expression<Func<TEntity, bool>> where);/// <summary>/// 更新/// </summary>/// <param name="entity"></param>/// <returns></returns>
        TEntity Update(TEntity entity);bool Delete(TEntity entity);/// <summary>/// 是否存在/// </summary>/// <param name="where"></param>/// <returns></returns>bool Exist(Expression<Func<TEntity, bool>> where);/// <summary>/// 条件查询/// </summary>/// <param name="where"></param>/// <returns></returns>TEntity Find(Expression<Func<TEntity, bool>> where);/// <summary>/// 查询集合/// </summary>/// <param name="where"></param>/// <returns></returns>IQueryable<TEntity> FindAll(Expression<Func<TEntity, bool>> where);/// <summary>/// 条件查询/// </summary>/// <typeparam name="SEntity"></typeparam>/// <param name="where"></param>/// <param name="isAsc">是否升序</param>/// <param name="orderlanbda">排序表达式</param>/// <returns></returns>IQueryable<TEntity> FindAll<SEntity>(Expression<Func<TEntity, bool>> where, bool isAsc, Expression<Func<TEntity, SEntity>> orderlanbda);/// <summary>/// 分页查询/// </summary>/// <typeparam name="SEntity"></typeparam>/// <param name="pageIndex"></param>/// <param name="pageSize"></param>/// <param name="totalRecord"></param>/// <param name="where"></param>/// <param name="isAsc"></param>/// <param name="orderLambda"></param>/// <returns></returns>IQueryable<TEntity> FindPaged<SEntity>(int pageIndex, int pageSize, out int totalRecord, Expression<Func<TEntity, bool>> where, bool isAsc, Expression<Func<TEntity, SEntity>> orderLambda);/// <summary>/// 保存/// </summary>/// <returns></returns>int SaveChanges();}
IBaseRepository

IUserInfoRepository:UserInfo操作接口。

    /// <summary>/// 用户信息仓储接口/// </summary>public interface IUserInfoRepository : IBaseRepository<UserInfo>{}
IUserInfoRepository

DAL层

添加数据库上下文NetDiskContext类。关于ef6+mysql code first的具体使用可以参考前面的文章。

    /// <summary>/// 数据库上下文/// </summary>public class NetDiskContext : DbContext{/// <summary>/// name:数据库连接字符串/// </summary>public NetDiskContext(): base("name=NetDiskContext"){}public DbSet<UserInfo> UserInfos { set; get; }//public DbSet<Department> Deparments { set; get; }//public DbSet<Model.NetDisk> NetDisks { set; get; }}
NetDiskContext

 ContextFactory:用来获取数据上下文的工厂,代码如下,第一次使用ef,也不知道有没有更好的实现方式,先这样实现吧,以后发现更好的方式,再进行优化。

    /// <summary>/// 数据上下文工厂类/// </summary>public static class ContextFactory{/// <summary>/// 获取数据库上下文/// </summary>/// <returns></returns>public static DbContext GetDbContext(){NetDiskContext _netDiskContext = CallContext.GetData("NetDiskContext") as NetDiskContext;if (_netDiskContext == null){_netDiskContext = new NetDiskContext();IDatabaseInitializer<NetDiskContext> dbInitializer = null;if (_netDiskContext.Database.Exists()){//如果数据库已经存在dbInitializer = new DropCreateDatabaseIfModelChanges<NetDiskContext>();}else{//总是先删除然后再创建dbInitializer = new DropCreateDatabaseAlways<NetDiskContext>();}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>/// 仓储基类/// </summary>public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class{protected NetDiskContext netDiskContext = ContextFactory.GetDbContext() as NetDiskContext;public TEntity Add(TEntity entity){netDiskContext.Entry<TEntity>(entity).State = System.Data.Entity.EntityState.Added;return entity;}public int Count(Expression<Func<TEntity, bool>> where){return netDiskContext.Set<TEntity>().Count(where);}public bool Delete(TEntity entity){netDiskContext.Entry<TEntity>(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(Expression<Func<TEntity, bool>> where){return netDiskContext.Set<TEntity>().Any(where);}public TEntity Find(Expression<Func<TEntity, bool>> where){return netDiskContext.Set<TEntity>().FirstOrDefault(where);}public IQueryable<TEntity> FindAll(Expression<Func<TEntity, bool>> where){return netDiskContext.Set<TEntity>().Where(where);}public IQueryable<TEntity> FindAll<SEntity>(Expression<Func<TEntity, bool>> where, bool isAsc, Expression<Func<TEntity, SEntity>> orderlanbda){var lst = netDiskContext.Set<TEntity>().Where<TEntity>(where);if (!isAsc){lst = lst.OrderByDescending<TEntity, SEntity>(orderlanbda);}return lst;}public IQueryable<TEntity> FindPaged<SEntity>(int pageIndex, int pageSize, out int totalRecord, Expression<Func<TEntity, bool>> where, bool isAsc, Expression<Func<TEntity, SEntity>> orderLambda){var lst = netDiskContext.Set<TEntity>().Where<TEntity>(where);totalRecord = lst.Count();if (!isAsc){lst = lst.OrderByDescending<TEntity, SEntity>(orderLambda);}return lst.Skip<TEntity>((pageIndex - 1) * pageIndex).Take(pageSize);}public int SaveChanges(){return netDiskContext.SaveChanges();}public TEntity Update(TEntity entity){TEntity tentity = netDiskContext.Set<TEntity>().Attach(entity);netDiskContext.Entry<TEntity>(entity).State = System.Data.Entity.EntityState.Modified;return tentity;}}
}
BaseRepository
    /// <summary>/// 用户数据操作dal层 /// </summary>public class UserInfoRepository:BaseRepository<UserInfo>, 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>/// 仓储工厂/// </summary>public 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 IBaseServiceRepository<TEntity>{/// <summary>/// 添加实体/// </summary>/// <param name="entity"></param>/// <returns></returns>
        TEntity Add(TEntity entity);/// <summary>/// 计数/// </summary>/// <param name="where"></param>/// <returns></returns>int Count(Expression<Func<TEntity, bool>> where);/// <summary>/// 更新/// </summary>/// <param name="entity"></param>/// <returns></returns>
        TEntity Update(TEntity entity);bool Delete(TEntity entity);/// <summary>/// 是否存在/// </summary>/// <param name="where"></param>/// <returns></returns>bool Exist(Expression<Func<TEntity, bool>> where);/// <summary>/// 条件查询/// </summary>/// <param name="where"></param>/// <returns></returns>TEntity Find(Expression<Func<TEntity, bool>> where);/// <summary>/// 查询集合/// </summary>/// <param name="where"></param>/// <returns></returns>IQueryable<TEntity> FindAll(Expression<Func<TEntity, bool>> where);/// <summary>/// 条件查询/// </summary>/// <typeparam name="SEntity"></typeparam>/// <param name="where"></param>/// <param name="isAsc">是否升序</param>/// <param name="orderlanbda">排序表达式</param>/// <returns></returns>IQueryable<TEntity> FindAll<SEntity>(Expression<Func<TEntity, bool>> where, bool isAsc, Expression<Func<TEntity, SEntity>> orderlanbda);/// <summary>/// 分页查询/// </summary>/// <typeparam name="SEntity"></typeparam>/// <param name="pageIndex"></param>/// <param name="pageSize"></param>/// <param name="totalRecord"></param>/// <param name="where"></param>/// <param name="isAsc"></param>/// <param name="orderLambda"></param>/// <returns></returns>IQueryable<TEntity> FindPaged<SEntity>(int pageIndex, int pageSize, out int totalRecord, Expression<Func<TEntity, bool>> where, bool isAsc, Expression<Func<TEntity, 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:IBaseServiceRepository<UserInfo>{}
}
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 BaseServiceRepository<TEntity> : IBaseServiceRepository<TEntity> where TEntity : class,new(){protected IBaseRepository<TEntity> currentRepository { set; get; }public BaseServiceRepository(IBaseRepository<TEntity> currentRepository){this.currentRepository = currentRepository;}public TEntity Add(TEntity entity){return currentRepository.Add(entity);}public int Count(Expression<Func<TEntity, bool>> where){return currentRepository.Count(where);}public bool Delete(TEntity entity){return currentRepository.Delete(entity);}public bool Exist(Expression<Func<TEntity, bool>> where){return currentRepository.Exist(where);}public TEntity Find(Expression<Func<TEntity, bool>> where){return currentRepository.Find(where);}public IQueryable<TEntity> FindAll(Expression<Func<TEntity, bool>> where){return currentRepository.FindAll(where);}public IQueryable<TEntity> FindAll<SEntity>(Expression<Func<TEntity, bool>> where, bool isAsc, Expression<Func<TEntity, SEntity>> orderlanbda){return currentRepository.FindAll<SEntity>(where, isAsc, orderlanbda);}public IQueryable<TEntity> FindPaged<SEntity>(int pageIndex, int pageSize, out int totalRecord, Expression<Func<TEntity, bool>> where, bool isAsc, Expression<Func<TEntity, SEntity>> orderLambda){return currentRepository.FindPaged<SEntity>(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 : BaseServiceRepository<UserInfo>, IUserInfoServiceRepository{/// <summary>/// 构造函数,通过仓储工厂调用dal中的具体的仓储/// </summary>/// <param name="currentRepository"></param>public 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></returns>public 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544182.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Calico IP_AUTODETECTION_METHOD

在 Calico 中&#xff0c;IP_AUTODETECTION_METHOD 的配置项用于指定 Calico 如何检测容器的 IP 地址。 一、kubernetes-internal-ip模式 其中&#xff0c;kubernetes-internal-ip 是一种特殊的模式&#xff0c;用于在 Kubernetes 环境中检测容器的 IP 地址。具体作用如下&…

下个十年高性能 JSON 库来了:fastjson2!

作者 | 磊哥来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;fastjson2 是 fastjson 项目的重要升级&#xff0c;目标是为下一个十年提供一个高性能的 JSON 库&#xff0c;同一套 API 支…

ascii非打印控制字符表_C程序打印ASCII表/图表

ascii非打印控制字符表什么是ASCII码&#xff1f; (What are ASCII Codes?) ASCII stands for American Standard Code for Information Interchange; it is a character encoding standards for information interchange in electronics communication. Each alphabets, spec…

THEOS的第一个TWeak的成功创建

THEOS的第一个TWeak的成功创建THEOS的第一个TWeak的成功创建参考资料:成功的创建一个TWeak的弹出步骤1:安装Xcode和Xcode command line步骤2:安装theosa:下载theos前,设置保存的路径:环境变量b:下载theosc:下载头文件d:下载ldid签名工具e:配置MoblieSubstrate环境f:安装dpkg步骤…

查询中,有没有可能多个索引一起用呢?

其实我们之前所讲的回表&#xff0c;就是两个索引树同时使用&#xff0c;先在二级索引树中搜索到对应的主键值&#xff0c;然后在再去主键索引树中查询完整的记录。但是我今天的问题是&#xff0c;两个不同的二级索引树&#xff0c;会同时生效吗&#xff1f;理论上来说&#xf…

ruby array_Ruby中带有示例的Array.sample()方法

ruby arrayArray.sample()方法 (Array.sample() Method) In this article, we will study about Array.sample() method. You all must be thinking the method must be doing something which is quite different from all those methods which we have studied. It is not as…

ThreadLocal夺命11连问

前言前一段时间&#xff0c;有同事使用ThreadLocal踩坑了&#xff0c;正好引起了我的兴趣。所以近期&#xff0c;我抽空把ThreadLocal的源码再研究了一下&#xff0c;越看越有意思&#xff0c;发现里面的东西还真不少。我把精华浓缩了一下&#xff0c;汇集成了下面11个问题&…

关于静态库、动态库的区别汇总

real framework中不可以使用类别 或 不可以不包含类文件real framework 中直接调用NSClassFromString函数会返回null 需要强制加载指定类 或 直接通过类名引用linux中静态库和动态库的区别一、不同库从本质上来说是一种可执行代码的二进制格式&#xff0c;可以被载入内存中执行…

PHP array_pad()函数与示例

PHP array_pad()函数 (PHP array_pad() function) array_pad() function is used to pad an array to given size with a specified value and returns a new array with a specified value. array_pad()函数用于将数组填充到具有指定值的给定大小&#xff0c;并返回具有指定值…

Spring Boot 优雅配置多数据源

大约在19年的这个时候&#xff0c;老同事公司在做医疗系统&#xff0c;需要和HIS系统对接一些信息&#xff0c;比如患者、医护、医嘱、科室等信息。但是起初并不知道如何与HIS无缝对接&#xff0c;于是向我取经。最终经过讨论采用了视图对接的方式&#xff0c;大致就是HIS系统提…

(转)新ITC提交APP常见问题与解决方法(Icon Alpha,Build version,AppIcon120x120)(2014-11-17)...

1&#xff09;ICON无法上传&#xff0c;提示图片透明&#xff08;有Alpha通道&#xff09;苹果现在不接受png里的Alpha了&#xff0c;提交的图标带有Alpha通道就提示&#xff1a;简单处理&#xff1a;用自带的预览打开&#xff0c;导出时不勾选Alpha&#xff0c;仍保存为png格式…

python 向量取整数_随机整数向量| 使用Python的线性代数

python 向量取整数Prerequisite: 先决条件&#xff1a; Defining a Vector using list 使用列表定义向量 Defining Vector using Numpy 使用Numpy定义向量 Random Integer Vector is a type of vector having a random integer value at each entry. Such types of vectors ha…

Spring 夺命 35 问!

有人说&#xff0c;“Java程序员都是Spring程序员”&#xff0c;可以看出Spring在Java世界里举足轻重的作用。基础1.Spring是什么&#xff1f;特性&#xff1f;有哪些模块&#xff1f;Spring Logo一句话概括&#xff1a;Spring 是一个轻量级、非入侵式的控制反转 (IoC) 和面向切…

Android百度地图开发03之地图控制 + 定位

前两篇关于百度地图的blog写的是&#xff0c;一些基本图层的展示 和 覆盖物的添加地理编码和反地理编码。 接下来&#xff0c;这篇blog主要说一些关于地图控制方面的内容和定位功能。 百度地图提供的关于地图的操作主要有&#xff1a;单击、双击、长按、缩放、旋转、俯视等。 地…

软件工程需要学什么_为什么我们需要软件工程?

软件工程需要学什么Software engineering is the application of the set of pre-defined procedures while developing any project. But why do we need Software engineering? What factors made us implement these predefined set of procedures and protocols while dev…

IDEA 版 Postman 面世了,功能真心强大!

IDEA是最常用的开发工具&#xff0c;很多程序员都想把它打造成一站式开发平台&#xff0c;于是安装了各种各样的插件。最近发现了一款IDEA插件RestfulFastRequest&#xff0c;细节做的真心不错&#xff0c;说它是IDEA版的Postman也不为过&#xff0c;推荐给大家&#xff01;Res…

DNS子域授权

转载于:https://blog.51cto.com/changeflyhigh/1697257

mongo数据库插入数据_深入研究Mongo数据库

mongo数据库插入数据More popularly known as "mongoDB". It is a no-sql based database. 俗称“ mongoDB” 。 这是一个基于无SQL的数据库。 BASIC STRUCTURE OF MONGO DB MONGO DB的基本结构 A COLLECTION IN MONGODB having 3 DOCUMENTS MONGODB中有3个文档的集…

java方法重载和重载方法_我们可以在Java中重载main()方法吗?

java方法重载和重载方法The question is that "can we overload main() method in Java?" 问题是“我们可以在Java中重载main()方法吗&#xff1f;” Yes, We can overload the main() method in Java. 是的&#xff0c;我们可以重载Java中的main()方法 。 JVM cal…

五分钟,手撸一个Spring容器!

Spring是我们最常用的开源框架&#xff0c;经过多年发展&#xff0c;Spring已经发展成枝繁叶茂的大树&#xff0c;让我们难以窥其全貌。这节&#xff0c;我们回归Spring的本质&#xff0c;五分钟手撸一个Spring容器&#xff0c;揭开Spring神秘的面纱&#xff01;从什么是IOC开始…