知识点回顾
一.委托
1.委托:委托使用一种类型安全的函数回调机制,能够调用实例方法、静态方法,并且具有按顺序执行多个方法的能力。
2.委托的分类
(1)简单委托 (2)匿名委托 (3)泛型委托 (4)多播委托
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApp1 8 { 9 //public delegate void ErrorHandler(); 10 11 public class Student 12 { 13 //public Action<int,string,DateTime> errorHandler; 14 public Func<int, string, DateTime> errorHandler; 15 public Student() 16 { 17 // string[] strs = new string[7]; 18 // strs.Where(m => 19 // { 20 // var len = m.Length; 21 // return len>5; 22 // }); 23 // errorHandler = () => { }; 24 25 errorHandler = (id, name ) => 26 { 27 return DateTime.Now; 28 }; 29 } 30 31 public int Star { get; set; } = 5; 32 33 private int score; 34 35 public event Action<int> LessThanSixty; 36 public int Score 37 { 38 get => score; 39 set 40 { 41 if (value > 90) Star = 4; 42 else if (value > 70) Star = 3; 43 else if (value > 60) Star = 2; 44 else 45 { 46 Star = 1; 47 // LessThanSixty; 48 LessThanSixty?.Invoke(value); 49 } 50 score = value; 51 } 52 } 53 54 55 } 56 }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var stu = new Student(); stu.LessThanSixty += Stu_LessThanSixty; stu.LessThanSixty += Stu_LessThanSixty1; //注册事件方法 //stu.LessThanSixty += val => //{ // Console.WriteLine("回家挨打"); //}; stu.Score = 70; Console.WriteLine(stu.Star); stu.Score = 90; Console.WriteLine(stu.Star); stu.Score = 55; Console.WriteLine(stu.Star); Console.ReadLine(); } private static void Stu_LessThanSixty1(int obj) { Console.WriteLine("继续努力!"); } private static void Stu_LessThanSixty(int obj) { Console.WriteLine($"你考的太少了,分数是:{obj}"); } } }
事件是对委托的一种封装
二.MVC项目的搭建(以OA权限管理为例)
1.在Models文件夹中添加一个基类
public class BaseEntity { public Guid Id { get; set; } = Guid.NewGuid(); public bool IsRemoved { get; set; } public DateTime CreateTime { get; set; } = DateTime.Now; }
2.在Models文件夹中添加5个类 Role(角色) User(用户) Power(方法) RoleUser(给用户分配角色) RoleinPower(给角色分配权限)
3.添加数据上下文类(OADbContext)
1 public class OADbContext:DbContext 2 { 3 /* 1.配置连接串 4 * 1.1关闭默认的数据库生成模式 5 * 2.注册model 6 * 3.关闭级联操作 one to many many to many 7 */ 8 9 public DbDemoContext() : base("con") 10 { 11 Database.SetInitializer<DbDemoContext>(null); 12 //Database.SetInitializer (new DropCreateDatabaseAlways<DbDemoContext>() ); 13 } 14 15 protected override void OnModelCreating(DbModelBuilder modelBuilder) 16 { 17 base.OnModelCreating(modelBuilder); 18 19 modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); 20 modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>(); 21 22 } 23 24 public DbSet<Student> Students { get; set; } 25 public DbSet<User> Users { get; set; } 26 public DbSet<Power> Powers { get; set; } 27 public DbSet<Role> Roles { get; set; } 28 public DbSet<UserInRole> UserInRole { get; set; } 29 public DbSet<UserInformation> UserInformation { get; set; } 30 public DbSet<PowerInRole> PowerInRole { get; set; } 31 }
4.添加DAL文件夹
5.在DAL文件夹中BaseService类
public class BaseService<T>:IDisposable where T:BaseEntity,new() { private readonly DbDemoContext _db; public BaseService() { _db = new DbDemoContext(); Context = _db; } public DbDemoContext Context { get; } public async Task Create(T t,bool isSaved = true) { _db.Set<T>().Add(t); if (isSaved) await _db.SaveChangesAsync(); } public void Dispose() { _db.Dispose(); } /// <summary> /// 通用修改方式,但是不太好 /// </summary> /// <param name="t"></param> /// <param name="isSaved"></param> /// <returns></returns> public async Task Edit(T t, bool isSaved = true) { _db.Entry(t).State = EntityState.Modified; if (isSaved) { _db.Configuration.ValidateOnSaveEnabled = false; await _db.SaveChangesAsync(); _db.Configuration.ValidateOnSaveEnabled = true; } } public IQueryable<T> GetAll() { return _db.Set<T>().Where(m => !m.IsRemoved).AsNoTracking(); } public IQueryable<T> GetAll(Expression<Func<T, bool>> predicate) { return GetAll().Where(predicate); } public IQueryable<T> GetAll(Expression<Func<T, bool>> predicate, int pageIndex = 0, int pageSize = 10) { return GetAll(predicate).OrderBy(m => m.CreateTime).Skip(pageIndex * pageSize).Take(pageSize); } //Expression用于通过C#原生代码构建表达式 public IQueryable<T> GetAll(Expression<Func<T, bool>> predicate, int pageIndex = 0, int pageSize = 10,bool isAsc = true) { var result = GetAll(predicate); if(isAsc) return result.OrderBy(m => m.CreateTime).Skip(pageIndex * pageSize).Take(pageSize); else return result.OrderByDescending(m => m.CreateTime).Skip(pageIndex * pageSize).Take(pageSize); } public async Task Remove(Guid id,bool isSaved = true) { var t = new T(){Id = id}; _db.Entry(t).State = EntityState.Unchanged; t.IsRemoved = true; if (isSaved) await _db.SaveChangesAsync(); } public async Task Save() { await _db.SaveChangesAsync(); } }
6.添加BLL文件夹
7.在BLL文件夹中添加UserManager类
public class UserManager { public static async Task<List<Dto.UserInfoDto>> GetAllUserInfo() { using (var userInfoSvc = new DAL.UserInformationService()) { return await userInfoSvc.GetAll().Include(m => m.User) .Select(m => new Dto.UserInfoDto() { Id = m.Id, UserId = m.UserId, NickName = m.NickName, Email = m.User.Email, ImagePath = m.ImagePath }).ToListAsync(); } } }
8.添加DTO文件夹,dto(数据传输对象)
public class UserInfoDto { public Guid Id { get; set; } public string NickName { get; set; } public string Email { get; set; } public string ImagePath { get; set; } public Guid UserId { get; set; } }
三.多线程
1.多线程的使用场景
(1)处理比较复杂的业务