写在前面

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

系列文章

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

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

实现

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()]
[Display(Name = "头像")]
public string Header { set; get; }
/// <summary>
/// 姓名
/// </summary>
[MaxLength(, ErrorMessage = "网名长度不得超过32个字符")]
[Required(ErrorMessage = "请填写您的名称")]
[Display(Name = "姓名")]
public string Name { set; get; }
/// <summary>
/// 网名
/// </summary>
[MaxLength(, ErrorMessage = "网名长度不得超过32个字符")]
[Required(ErrorMessage = "请填写您的网名")]
[Display(Name = "网名")]
public string DisplayName { set; get; }
/// <summary>
/// 邮箱
/// </summary>
[RegularExpression(@"^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$", ErrorMessage = "请输入正确的邮箱地址")]
[MaxLength(, 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()]
[Display(Name = "职位")]
public string Position { set; get; }
/// <summary>
/// 电话
/// </summary>
[MaxLength()]
[Display(Name = "电话")]
public string Tel { set; get; } /// <summary>
/// 密码
/// </summary>
[StringLength(, 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()]
[Display(Name = "地址")]
public string Address { set; get; }
/// <summary>
/// 籍贯
/// </summary>
[MaxLength()]
[Display(Name = "籍贯")]
public string Hometown { set; get; }
/// <summary>
/// 公司
/// </summary>
[StringLength(, 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() > ;
} 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 - ) * 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

添加视图

先不管界面的美与丑,先试下能否正确的添加数据。如果成功下一步,再进行美化。

总结

下面将完善注册的过程,用户名是否存在验证,密码加密,验证码等操作。

[实战]MVC5+EF6+MySql企业网盘实战(2)——用户注册的更多相关文章

  1. [实战]MVC5+EF6+MySql企业网盘实战(28)——其他列表

    写在前面 本篇文章将实现,其他文件类型的列表. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网盘实战(1) [实战]MVC5+EF ...

  2. [实战]MVC5+EF6+MySql企业网盘实战(27)——应用列表

    写在前面 本篇文章将实现应用列表,同样和其他列表的不同之处,在于查询条件的不同. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网盘 ...

  3. [实战]MVC5+EF6+MySql企业网盘实战(4)——上传头像

    写在前面 最近又开始忙了,工期紧比较赶,另外明天又要去驾校,只能一个功能一个功能的添加了,也许每次完成的功能确实不算什么,等将功能都实现了,然后在找一个好点的ui对前端重构一下. 系列文章 [EF]v ...

  4. [实战]MVC5+EF6+MySql企业网盘实战(2)——验证码

    写在前面 断断续续,今天算是把验证码的东东弄出来了. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网盘实战(1) [实战]MVC5 ...

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

    写在前面 不久前,一个朋友让帮他弄一个单位的企业网盘的管理站点,一直忙,最近抽出了点时间,也想琢磨琢磨mvc,ef,mysql,这算是边琢磨,边实践吧. 系列文章 [实战]MVC5+EF6+MySql ...

  6. [实战]MVC5+EF6+MySql企业网盘实战(26)——音乐列表

    写在前面 本篇文章将实现,音乐列表,同样和其他列表的不同之处,在于查询条件的不同. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网 ...

  7. [实战]MVC5+EF6+MySql企业网盘实战(25)——种子列表

    写在前面 上篇文章实现了视频列表,本篇文章继续实现其他的文件列表.功能相似.这里就不再赘述. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MyS ...

  8. [实战]MVC5+EF6+MySql企业网盘实战(24)——视频列表

    写在前面 上篇文章实现了文档列表,所以实现视频列表就依葫芦画瓢就行了. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网盘实战(1) ...

  9. [实战]MVC5+EF6+MySql企业网盘实战(23)——文档列表

    写在前面 上篇文章实现了图片列表,这篇文章实现文档列表将轻车熟路,因为逻辑基本相似,只是查询条件的不同.这里将txt,doc,docx,ppt,pptx,xls,xlsx的文件都归为文档列表中. 系列 ...

随机推荐

  1. Tensorflow同时加载使用多个模型

    在Tensorflow中,所有操作对象都包装到相应的Session中的,所以想要使用不同的模型就需要将这些模型加载到不同的Session中并在使用的时候申明是哪个Session,从而避免由于Sessi ...

  2. css3-rem

    http://www.w3cplus.com/css3/define-font-size-with-css3-rem https://mp.weixin.qq.com/s/DpLXJhfCHsgrbg ...

  3. mysql5.7系列修改root默认密码

    操作系统为centos7 64 1.修改 /etc/my.cnf,在 [mysqld] 小节下添加一行:skip-grant-tables=1 这一行配置让 mysqld 启动时不对密码进行验证 2. ...

  4. tooltips插件

    摘要: 继‘带箭头提示框’,本文将分享几款带箭头提示框. qtipqTip是一种先进的提示插件,基于jQuery框架.以用户友好,而且功能丰富,qTip为您提供不一般的功能,如圆角和语音气泡提示,并且 ...

  5. 高可用(HA)架构

    http://aokunsang.iteye.com/blog/2053719   浅谈web应用的负载均衡.集群.高可用(HA)解决方案 http://zhuanlan.51cto.com/art/ ...

  6. web -- 前端访问后台跨区问题解决

    package com.xindatai.ibs.web.filter; import java.io.IOException; import javax.servlet.Filter; import ...

  7. CMake区分MSVC版本

    MSVC++ 4.x _MSC_VER == 1000 MSVC++ 5.0 _MSC_VER == 1100 MSVC++ 6.0 _MSC_VER == 1200 MSVC++ 7.0 _MSC_ ...

  8. Ansible的快速入门

    Ansible 是一个简单的自动化引擎,可完成配置管理,应用部署,服务编排等各种IT需求. Ansible使用python语言开发实现的开源软件,依赖于Jinjia2,paramiko和PyYAML这 ...

  9. vc 使用ShellExecut来启动控制面板中功能模块的操作

    文件夹,文件,网址可以创建快捷方式,控制面板 中的设置也可以创建快捷方式,下面是快捷方式的命令,使用方法:在桌面或文件夹的空白处点右键,选择新建,快捷方式,在“请键入项目的位置”输入下面的命 令,然后 ...

  10. codeforces水题100道 第二十一题 Codeforces Beta Round #65 (Div. 2) A. Way Too Long Words (strings)

    题目链接:http://www.codeforces.com/problemset/problem/71/A题意:将长字符串改成简写格式.C++代码: #include <string> ...