项目暂时分为六大块,结构如图所示
代码地址是 https://github.com/hudean/VacantCloud-
里面有许多没有完成,不过一些大致的内容都写的差不多了,权限认证依赖注入,支持多种数据库等等。
Vacant.EntityFrameWorkCore 暂时没有用到
一、Vacant.Entity 顾名思义主要是存放与数据库交互的实体类,这个项目库里面主要有一个Entitys文件夹存放实体类,还有实体类用到的2个抽象类和两个接口
1、首先是Entity类代码如下
1 using System; 2 using System.Collections.Generic; 3 using System.Reflection; 4 using System.Text; 5 6 namespace Vacant.Entitys 7 { 8 /// <summary> 9 /// A shortcut of <see cref="Entity{TPrimaryKey}"/> for most used primary key type (<see cref="int"/>). 10 /// </summary> 11 [Serializable] 12 public abstract class Entity : Entity<long>, IEntity 13 { 14 15 } 16 17 18 /// <summary> 19 /// Basic implementation of IEntity interface. 20 /// An entity can inherit this class of directly implement to IEntity interface. 21 /// </summary> 22 /// <typeparam name="TPrimaryKey">Type of the primary key of the entity</typeparam> 23 [Serializable] 24 public abstract class Entity<TPrimaryKey> : IEntity<TPrimaryKey> 25 { 26 /// <summary> 27 /// Unique identifier for this entity. 28 /// </summary> 29 public virtual TPrimaryKey Id { get; set; } 30 31 /// <summary> 32 /// Checks if this entity is transient (it has not an Id). 33 /// </summary> 34 /// <returns>True, if this entity is transient</returns> 35 public virtual bool IsTransient() 36 { 37 if (EqualityComparer<TPrimaryKey>.Default.Equals(Id, default(TPrimaryKey))) 38 { 39 return true; 40 } 41 42 //Workaround for EF Core since it sets int/long to min value when attaching to dbcontext 43 if (typeof(TPrimaryKey) == typeof(int)) 44 { 45 return Convert.ToInt32(Id) <= 0; 46 } 47 48 if (typeof(TPrimaryKey) == typeof(long)) 49 { 50 return Convert.ToInt64(Id) <= 0; 51 } 52 53 return false; 54 } 55 56 /// <inheritdoc/> 57 public virtual bool EntityEquals(object obj) 58 { 59 if (obj == null || !(obj is Entity<TPrimaryKey>)) 60 { 61 return false; 62 } 63 64 //Same instances must be considered as equal 65 if (ReferenceEquals(this, obj)) 66 { 67 return true; 68 } 69 70 //Transient objects are not considered as equal 71 var other = (Entity<TPrimaryKey>)obj; 72 if (IsTransient() && other.IsTransient()) 73 { 74 return false; 75 } 76 77 //Must have a IS-A relation of types or must be same type 78 var typeOfThis = GetType(); 79 var typeOfOther = other.GetType(); 80 if (!typeOfThis.GetTypeInfo().IsAssignableFrom(typeOfOther) && !typeOfOther.GetTypeInfo().IsAssignableFrom(typeOfThis)) 81 { 82 return false; 83 } 84 85 //if (this is IMayHaveTenant && other is IMayHaveTenant && 86 // this.As<IMayHaveTenant>().TenantId != other.As<IMayHaveTenant>().TenantId) 87 //{ 88 // return false; 89 //} 90 91 //if (this is IMustHaveTenant && other is IMustHaveTenant && 92 // this.As<IMustHaveTenant>().TenantId != other.As<IMustHaveTenant>().TenantId) 93 //{ 94 // return false; 95 //} 96 97 return Id.Equals(other.Id); 98 } 99 100 public override string ToString() 101 { 102 return $"[{GetType().Name} {Id}]"; 103 } 104 } 105 }