public virtual void SaveUser(IUser user) { lock (Locker.GetLocker($"BasePermissionStore_SaveUser")) { // 账号不能重复.如果是多租户,同一租户里的账号不能重复. var allUser = GetAllUser(); var existSameAccountUser = false; var sameAccountUsers = allUser.Where(a => a.GetAccount() == user.GetAccount()).ToList(); if (HasTenant(out string tenantId)) { existSameAccountUser = allUser.Any(a => a.GetAccount() == user.GetAccount() && ((ITenant <string>)a).TenantId == tenantId && a.GetKey() != user.GetKey()); } else { existSameAccountUser = allUser.Any(a => a.GetAccount() == user.GetAccount() && a.GetKey() != user.GetKey()); } if (existSameAccountUser) { throw new BusinessException($"已经存在账号为{user.GetAccount()}的用户"); } var userTmp = EasyMap.MapToNew <TUser>(user); var userEntity = _db.Set <TUser>().AddOrUpdate(userTmp, CurrentUserId, CurrentTenantId, null); userEntity.SetName(user.GetName()); userEntity.SetAccount(user.GetAccount()); userEntity.SetPassword(user.GetPassword()); _db.SaveChanges(); _memoryCache.Remove(userCacheKey); } }
public virtual void SaveRole(IRole role) { lock (Locker.GetLocker($"BasePermissionStore_SaveRole")) { var roleTmp = EasyMap.MapToNew <TRole>(role); _db.Set <TRole>().AddOrUpdate(roleTmp, CurrentUserId, CurrentTenantId, null); _db.SaveChanges(); _memoryCache.Remove(roleCacheKey); } }
/// <summary> /// 保存资源。会从资源id和资源code两字段考虑是新增还是修改 /// </summary> /// <param name="resource"></param> public virtual void SaveResource(IResource resource) { var resourceTmp = EasyMap.MapToNew <TResource>(resource); var resourceEntity = _db.Set <TResource>().AddOrUpdate(resourceTmp, CurrentUserId, CurrentTenantId); resourceEntity.SetName(resourceTmp.GetName()); resourceEntity.SetParentKey(resourceTmp.GetParentKey()); _db.SaveChanges(); _memoryCache.Remove(resourceCacheKey); }
/// <summary> /// 实体列表的差异更新,包含增加、删除、更新 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <typeparam name="TDto"></typeparam> /// <typeparam name="TKey"></typeparam> /// <param name="entities"></param> /// <param name="existsEntities"></param> /// <param name="dtos"></param> /// <param name="userId"></param> /// <param name="tenantId"></param> public static void AddOrUpdateList <TEntity, TDto, TKey>(this DbSet <TEntity> entities, List <TEntity> existsEntities, List <TDto> dtos, TKey userId, TKey tenantId = default) where TEntity : class, IIdField <TKey>, new() where TDto : class, IIdField <TKey> { AddOrUpdateListInternal(entities, existsEntities, dtos, sourceDto => EasyMap.MapToNew <TEntity>(sourceDto), (sourceDto, entityDto) => EasyMap.Map(sourceDto.GetType(), entityDto.GetType(), sourceDto, entityDto, null), userId, tenantId); }
public static TEntity AddOrUpdate <TEntity, TDto, TKey>(this DbSet <TEntity> entities, TDto dto, TKey userId, TKey tenantId = default, List <TEntity> existEntities = null) where TEntity : class, IIdField <TKey>, new() where TDto : class, IIdField <TKey> { return(AddOrUpdateInternal(entities, dto, sourceDto => EasyMap.MapToNew <TEntity>(sourceDto), (sourceDto, entityDto) => EasyMap.Map(sourceDto.GetType(), entityDto.GetType(), sourceDto, entityDto, null), userId, tenantId, existEntities)); }
public void MapToNewTest() { try { var sourceType = typeof(Source); var targetType = typeof(Target); var includeFields = new List <string> { "Name", "Age", "Birth", "Week", "ExceptField" }; Expression <Func <Target, object> > includeSelector = t => new { t.Name, t.Age, t.Birth, t.Week }; var exceptFields = new List <string> { "ExceptField" }; Expression <Func <Target, object> > exceptSelector = t => new { t.ExceptField }; var source = new Source { Name = "zhoujing", Age = 30, Birth = new DateTime(1989, 1, 1), NoMatch1 = "未匹配的字段,不会赋值到target", Week = DayOfWeek.Wednesday, ExceptField = "这个是排除的字段", NotIncludeField = "这个是不包含字段" }; var target = new Target(); target.Clear(); target = (Target)EasyMap.MapToNew(sourceType, targetType, source, includeFields); Assert.True(IsMatch(source, target, false, true)); target.Clear(); target = EasyMap.MapToNew <Target>(source); Assert.True(IsMatch(source, target, false, false)); target.Clear(); target = EasyMap.MapToNew <Source, Target>(source); Assert.True(IsMatch(source, target, false, false)); target.Clear(); target = EasyMap.MapToNew <Source, Target>(source, includeFields); Assert.True(IsMatch(source, target, false, true)); target.Clear(); target = EasyMap.MapToNew(source, includeSelector, exceptSelector); Assert.True(IsMatch(source, target, true, true)); target.Clear(); target = EasyMap.MapToNew <Source, Target>(source, includeFields, exceptFields); Assert.True(IsMatch(source, target, true, true)); } catch (Exception ex) { throw; } }
/// <summary> /// 增加单个实体,会用EasyMap做实体映射 /// 请在外部提交更改 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <typeparam name="TDto"></typeparam> /// <typeparam name="TKey"></typeparam> /// <param name="entities"></param> /// <param name="dto"></param> /// <param name="userId"></param> /// <param name="tenantId"></param> public static void Add <TEntity, TDto, TKey>(this DbSet <TEntity> entities, TDto dto, TKey userId, TKey tenantId = default) where TEntity : class, IIdField <TKey>, new() { AddInternal(entities, dto, sourceDto => EasyMap.MapToNew <TEntity>(sourceDto), userId, tenantId); }