boxu.zheng
1 year ago
9 changed files with 329 additions and 18 deletions
@ -0,0 +1,23 @@ |
|||
using System.Linq.Expressions; |
|||
|
|||
namespace Faster.Zheng.Winin.Extensions; |
|||
|
|||
public static class ExpressionExtensions |
|||
{ |
|||
public static string GetMemberName(Expression expression) |
|||
{ |
|||
if (expression is MemberExpression member) |
|||
{ |
|||
return member.Member.Name; |
|||
} |
|||
else if (expression is MethodCallExpression method) |
|||
{ |
|||
return method.Method.Name; |
|||
} |
|||
else if (expression is UnaryExpression unary) |
|||
{ |
|||
return GetMemberName(unary); |
|||
} |
|||
return null; |
|||
} |
|||
} |
@ -0,0 +1,49 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.ComponentModel.DataAnnotations; |
|||
using System.Linq; |
|||
using System.Linq.Dynamic.Core; |
|||
using System.Linq.Expressions; |
|||
using System.Reflection; |
|||
|
|||
namespace Faster.Zheng.Winin.Extensions; |
|||
|
|||
public static class ObjectExpressionExtensions |
|||
{ |
|||
public static IQueryable<TEntity> WhereByKey<TEntity, TModel>(this IQueryable<TEntity> source, TModel model) |
|||
{ |
|||
if (model == null) |
|||
{ |
|||
return null; |
|||
} |
|||
var modelType = model.GetType(); |
|||
var properties = modelType.GetProperties().Where(o => o.GetCustomAttribute<KeyAttribute>() != null).ToList(); |
|||
if (properties.Any()) |
|||
{ |
|||
foreach (var property in properties) |
|||
{ |
|||
var propertyName = property.Name; |
|||
var propertyValue = property.GetValue(model, null); |
|||
source = source.Where($"{propertyName} == @0", propertyValue); |
|||
} |
|||
return source; |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
public static List<IGrouping<object, T>> GroupByKey<T>(this IQueryable<T> source) |
|||
{ |
|||
var properties = typeof(T).GetProperties().Where(o => o.GetCustomAttribute<KeyAttribute>() != null).ToList(); |
|||
var names = string.Join(",", properties.Select(o => o.Name)); |
|||
return source.AsQueryable().GroupBy($"new ({names})").ToDynamicList<IGrouping<object, T>>(); |
|||
} |
|||
|
|||
public static Expression<Func<TEntity, bool>> GetExpressionByProperty<TEntity>(this Type type, string propertyName, string propertyValue) |
|||
{ |
|||
var o = Expression.Parameter(type, "p"); |
|||
var memberExpression = Expression.Property(o, propertyName); |
|||
var body = Expression.Call(typeof(string).GetMethod("Contains", new[] { typeof(string) }), memberExpression); |
|||
var predicate = Expression.Lambda<Func<TEntity, bool>>(body, o); |
|||
return predicate; |
|||
} |
|||
} |
@ -0,0 +1,113 @@ |
|||
using System; |
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using System.Reflection; |
|||
using Omu.ValueInjecter; |
|||
using Omu.ValueInjecter.Injections; |
|||
using Volo.Abp; |
|||
|
|||
namespace Faster.Zheng.Winin.Extensions; |
|||
|
|||
/// <summary>
|
|||
/// 对象映射
|
|||
/// </summary>
|
|||
public static class ObjectMapperExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// 从模型更新实体
|
|||
/// </summary>
|
|||
public static T FromObject<T>(this T to, object from) |
|||
{ |
|||
try |
|||
{ |
|||
to.InjectFrom<DeepInjectionForUpdate>(from); |
|||
return to; |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
throw new UserFriendlyException($"{from.GetType().FullName}映射到${typeof(T).FullName}时失败:{ex.Message},{ex}"); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 从实体创建模型
|
|||
/// </summary>
|
|||
/// <typeparam name="T"></typeparam>
|
|||
/// <param name="from"></param>
|
|||
/// <returns></returns>
|
|||
public static T ToObject<T>(this object from) |
|||
{ |
|||
try |
|||
{ |
|||
if (typeof(T).IsGenericType && typeof(T).IsAssignableTo(typeof(IList)) && from is IList list) |
|||
{ |
|||
var toListType = typeof(T); |
|||
var elementType = typeof(T).GetGenericArguments()[0]; |
|||
var toList = (IList)Activator.CreateInstance(typeof(T))!; |
|||
var fromList = list; |
|||
foreach (var item in fromList) |
|||
{ |
|||
toList.Add(Activator.CreateInstance(elementType).InjectFrom<DeepInjection>(item)); |
|||
} |
|||
return (T)toList; |
|||
} |
|||
return (T)Activator.CreateInstance<T>().InjectFrom<DeepInjection>(from); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
throw new UserFriendlyException($"{from.GetType().FullName}映射到${typeof(T).FullName}时失败:{ex.Message},{ex}"); |
|||
} |
|||
} |
|||
|
|||
private class DeepInjection : LoopInjection |
|||
{ |
|||
protected override bool MatchTypes(Type sourceType, Type targetType) |
|||
{ |
|||
if (sourceType != typeof(string) && |
|||
targetType != typeof(string) && |
|||
sourceType.IsGenericType && |
|||
targetType.IsGenericType && |
|||
sourceType.IsAssignableTo(typeof(IEnumerable)) && |
|||
sourceType.IsAssignableTo(typeof(IEnumerable)) |
|||
) |
|||
{ |
|||
return true; |
|||
} |
|||
return base.MatchTypes(sourceType, targetType); |
|||
} |
|||
|
|||
protected override void SetValue(object source, object target, PropertyInfo sp, PropertyInfo tp) |
|||
{ |
|||
if (sp.PropertyType != typeof(string) && |
|||
sp.PropertyType != typeof(string) && |
|||
sp.PropertyType.IsAssignableTo(typeof(IList)) && |
|||
tp.PropertyType.IsAssignableTo(typeof(IList))) |
|||
{ |
|||
var targetGenericType = tp.PropertyType.GetGenericArguments()[0]; |
|||
var listType = typeof(List<>).MakeGenericType(targetGenericType); |
|||
var addMethod = listType.GetMethod("Add"); |
|||
var list = Activator.CreateInstance(listType); |
|||
var sourceList = (IList)sp.GetValue(source); |
|||
foreach (var item in sourceList) |
|||
{ |
|||
addMethod.Invoke(list, new[] { Activator.CreateInstance(targetGenericType).FromObject(item) }); |
|||
} |
|||
tp.SetValue(target, list); |
|||
return; |
|||
} |
|||
base.SetValue(source, target, sp, tp); |
|||
} |
|||
} |
|||
|
|||
private class DeepInjectionForUpdate : DeepInjection |
|||
{ |
|||
protected override void SetValue(object source, object target, PropertyInfo sp, PropertyInfo tp) |
|||
{ |
|||
//if (tp.GetCustomAttribute<IgnoreUpdateAttribute>() != null)
|
|||
//{
|
|||
// return;
|
|||
//}
|
|||
base.SetValue(source, target, sp, tp); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,16 @@ |
|||
using System; |
|||
using System.Security.Cryptography; |
|||
using System.Text; |
|||
|
|||
namespace Faster.Zheng.Winin.Extensions; |
|||
|
|||
public static class StringExtensions |
|||
{ |
|||
public static string Md5(this string input) |
|||
{ |
|||
using (var md5 = MD5.Create()) |
|||
{ |
|||
return BitConverter.ToString(md5.ComputeHash(Encoding.ASCII.GetBytes(input))).Replace("-", ""); |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue