登录注册的小项目对比.Net Core与 .Net Framework的一些区别
一、需求:
1、功能只有登录、注册。
二、架构:
1、分别为
UserSys.IServices:主要有实体和对实体的配置,还有对实体的操作接口
UserSys.Services :主要是对自IService层中的接口实现
UserSys.DTO :主要是我们Web层中需要什么实体就给传递什么实体
UserSys.Common:一些通用的组件封装到该类库中
UserSys.Web:Asp.Net MVC

2、Web层采用Asp.Net MVC
3、数据库访问通过EF
三、具体实现
1、写User实体

public class User
{
public long Id { get; set; }
public string PhoneNum { get; set; }
public string PasswordHash { get; set; }
public bool IsDeleted { get; set; }
}

分别通过Nuget安装EF
.Net Freamwork中 Install-Package EntityFramework
.Net Core中 Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 2.0.0 一定要写上版本号
2、对实体中的字段进行配置 UserConfig
.Net Freamwork中 UserConfig需要继承EntityTypeConfiguration<User>

public class UserConfig : EntityTypeConfiguration<User>
{
public UserConfig()
{
this.ToTable("Users");
this.Property(o => o.PhoneNum).HasMaxLength(200).IsRequired();
this.Property(o => o.PasswordHash).HasMaxLength(200).IsRequired();
}
}

.Net Core中内置了IEntityTypeConfiguration

public class UserConfig : IEntityTypeConfiguration<User>
{
public void Configure(EntityTypeBuilder<User> builder)
{
builder.ToTable("Users");
builder.Property(u => u.PasswordHash).IsRequired().HasMaxLength(200);
builder.Property(u => u.PhoneNum).IsRequired().HasMaxLength(200);
}
}

3、封装一个MyDbContext
.Net Freamwork中我们继承自DbContext ,并重写该OnModelCreating方法

public class MyDbContext : DbContext
{
public MyDbContext() : base("constr") //base中的参数为数据库的连接字符串
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//这样就得到当前运行的
//这句话的意思就是 加载我们这句话所在的程序集加载所有的继承自EntityTypeConfiguration 为模型配置类。
modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());
}
public DbSet<Users> User { get; set; }
}

下面这段代码代表从这句话所在的程序集加载所有的继承自 EntityTypeConfiguration 为模型配置类
modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());
.Net Core中 同样也是该类继承DbContext,但是需要分别重写OnConfiguring和OnModelCreating方法

public class MyDbContext:DbContext
{
public DbSet<Users> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
var builder =
new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())//SetBasePath设置配置文件所在路径
.AddJsonFile("appsettings.json");
var configRoot = builder.Build();
var connString =
configRoot.GetSection("db").GetSection("ConnectionString").Value;
optionsBuilder.UseSqlServer(connString);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//这段代表表示,加载我们当前实体Users类所有的程序集
Assembly asmServices = Assembly.Load(new AssemblyName("UserSys.Services"));
modelBuilder.ApplyConfigurationsFromAssembly(asmServices);
}
}

注意:
1)、 .Net Core中没有内置像EF 中的modelBuilder.Configurations.AddFromAssembly()方法 ,但是不用担心,杨老师已经给我们封装了一个和EF中相同作用的
Nuget包,我们只需要安装就行了https://www.nuget.org/packages/RuPeng.EFCore.Ext
2)、.Net Core中的数据库连接等信息以键值对的形式是放在 json文件中的,与.Net Framework中不同,.Net Framework中是配置为Web.Config中的

4、开始写对Users类的操作接口IUserService .Net Core和.Net FrameWork中是相同的
public interface IUserService
{
void Add(string phoneNum, string password);
UserDTO GetByPhoneNum(string phoneNum);
bool CheckLogin(string phoneNum, string password);
}
5、写实现类UserService

public class UserService : IUserService
{
public void AddNew(string phoneNum, string password)
{
using (MyDbContext ctx = new MyDbContext())
{
if(ctx.Users.Any(u => u.PhoneNum == phoneNum))
{
throw new ApplicationException("手机号已经存在");
}
User user = new User();
user.PasswordHash = MD5Helper.Md5(password);
user.PhoneNum = phoneNum;
user.IsDeleted = false;
ctx.Users.Add(user);
ctx.SaveChanges();
}
}
public bool CheckLogin(string phoneNum, string password)
{
using (MyDbContext ctx = new MyDbContext())
{
User user = ctx.Users.SingleOrDefault(u => u.PhoneNum == phoneNum);
if(user==null)
{
return false;
}
string inputPwdHash = MD5Helper.Md5(password);
return user.PasswordHash == inputPwdHash;
}
}
public UserDTO GetByPhoneNum(string phoneNum)
{
using (MyDbContext ctx = new MyDbContext())
{
User user = ctx.Users.SingleOrDefault(u => u.PhoneNum == phoneNum);
if(user==null)
{
return null;
}
return new UserDTO { Id=user.Id,PasswordHash=user.PasswordHash,PhoneNum=phoneNum};
}
}
}

到这就剩下Web层的登录和注册了,So Easy,所以就不写具体怎么写了。
需要说下的是:依赖注入的问题:
1、.Net FrameWork中我们通过IOC对类进行注入,怎么注入自行百度,方法很多,我主要说下.Net Core中怎么注入
因为在.Net Core中已经内置了IOC 容器 ,不再需要 autofac 等,当然 autofac 也是支持.net core的( http://www.open-en.com/lib/view/open1454127071933.html)。
内置 IOC 是通过构造函数注入,而不是属性注入。
2、.Net Core中有内置的IOC有三种生命周期,我们采用Singleton 的方式注入,ingleton 生命能够周期服务在第一被请求时创建,在后续的每个请求都会使用同一个实例。
具体实现: 在Controller中使用构造函数注入(不是属性注入)
1)、首先需要在UserSys.IService层中,写一个通用的接口,该接口中不需要定义任何的方法,但是该类库中需要用到的接口都需要继承自IServiceTag接口
2)、如果我们注入单个类的话,可以直接在Startup.cs中的ConfigureServices方法中,直接这样注入
services.AddSingleton(typeof(IMyService),typeof(MyService));
但是如果我们有多个接口需要注入呢?我们需要封装一个方法来实现,就是通过我们下面的这样,通过反射来实现

public void ConfigureServices(IServiceCollection services)
{
//Filter
services.AddMvc(options=> {
options.Filters.Add(new ModelStateValidationFilter());
});
services.AddSession();
//注册服务和实现类
Assembly asmServices = Assembly.Load("UserSys.Services");
var serviceTypes = asmServices.GetTypes().Where(t => t.IsAbstract == false && typeof(IServiceTag).IsAssignableFrom(t));
foreach(var serviceType in serviceTypes)
{
var intfTypes = serviceType.GetInterfaces()
.Where(t => typeof(IServiceTag).IsAssignableFrom(t));
foreach (var intfType in intfTypes)
{
services.AddSingleton(intfType, serviceType);
}
}
}

3)、在需要用到该接口的实现方法的地方,我们只需要通过构造函数的形式注入就行了

public class HomeController : Controller
{
private IUserService userService;
public HomeController(IUserService userService)
{
this.userService = userService;
}
}

文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。
原文链接:aaaedu.blog.csdn.net/article/details/109240842
- 点赞
- 收藏
- 关注作者
评论(0)