From 9487a63c4d1113d6ac5f56b69efa3041f6cfab39 Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@users.noreply.github.com> Date: Thu, 13 Mar 2025 09:11:47 +0800 Subject: [PATCH] Updated AOP (markdown) --- AOP.md | 38 +++++--------------------------------- 1 file changed, 5 insertions(+), 33 deletions(-) diff --git a/AOP.md b/AOP.md index 351705a..08b5e36 100644 --- a/AOP.md +++ b/AOP.md @@ -120,6 +120,8 @@ fsql1.Aop.ConfigEntityProperty += (s, e) => ### 字典表应用 +利用 全局过滤器 + Aop 实现如下效果: + ```csharp [MapTable(typeof(SysParam), nameof(SysParam.Value), "001")] class Param001 @@ -130,7 +132,7 @@ class Param001 public int Age { get; set; } } fsql.Select().ToList(); -//SELECT * FROM SysParam WHERE Value = '001' +//SELECT Value2, Value3 FROM SysParam WHERE Value = '001' public class SysParam { @@ -140,40 +142,10 @@ public class SysParam public string Value3 { get; set; } //... } - -//实现关键 -fsql.Aop.ConfigEntity += (s, e) => -{ - var attr = e.EntityType.GetCustomAttribute(); - if (attr != null) - { - var dbname = (fsql.CodeFirst as CodeFirstProvider)._commonUtils.GetEntityTableAttribute(attr.Type).Name; - var prop = attr.Type.GetProperty(attr.PropertyName); - var propValue = Utils.GetDataReaderValue(prop.PropertyType, attr.PropertyValue); - e.ModifyResult.Name = dbname; - e.ModifyResult.DisableSyncStructure = true; - var paramExp = Expression.Parameter(e.EntityType); - var lambdaExp = Expression.Lambda(typeof(Func<,>).MakeGenericType(e.EntityType, typeof(bool)), - Expression.Equal(Expression.MakeMemberAccess(Expression.TypeAs(paramExp, attr.Type), prop), Expression.Constant(propValue)), paramExp); - var applyOnly = fsql.GlobalFilter.GetType().GetMethods().Where(a => a.Name == "ApplyOnly").FirstOrDefault(); - applyOnly.MakeGenericMethod(e.EntityType).Invoke(fsql.GlobalFilter, new object[] { $"MapTable_filter_{e.EntityType.FullName}", lambdaExp, false }); - } -}; -public class MapTableAttribute : Attribute -{ - public Type Type { get; set; } - public string PropertyName { get; set; } - public object PropertyValue { get; set; } - - public MapTableAttribute(Type type = null, string propertyName = null, object propertyValue = null) - { - Type = type; - PropertyName = propertyName; - PropertyValue = propertyValue; - } -} ``` +实现参考:https://github.com/dotnetcore/FreeSql/issues/1997 + ### 自定义实体特性 比如项目内已经使用了其它 orm,如 efcore,这样意味着实体中可能存在 [Key],但它与 FreeSql [Column(IsPrimary = true] 不同。