如何优雅的使用AbpSettings
在abp中配置虽然使用方便,但是每个配置要先定义key,要去provider中定义,再最后使用key从isetting中获取还是挺麻烦的一件事,
最主要是获取修改的时候,比如,修改用户配置,是从获取一批key/value来返回前端,并从前端提交修改保存就比较麻烦了。
很早之前做过一些尝试,如下:
但是那个时候比较菜也没怎么搞太清楚所以感觉也不太好用。
之前也想过使用定义配置类使用基类中注入的isettingmanager的方式来处理,如下
public string item { get { return this.settingmanager.getsettingvalue(nameof(item)); } set { this.settingmanager.changesettingforapplication(nameof(item), value); } }
但是这样对配置类污染比较大,也就放弃了,前段时间在看abp源代码的时候,突然想到,是否可以通过拦截器来代理配置类的get ,set方法来达到获取和修改配置的目的呢
于是便开始了配置的改造工作,首先,定义一个配置的接口来注册拦截器:
1 using abp.dependency; 2 3 namespace skys.charger.configuration 4 { 5 /// <summary> 6 /// 配置类接口,要实现从settingmanager更新/获取数据,请所有属性用virtual标识 7 /// </summary> 8 public interface isettings : isingletondependency 9 { 10 11 } 12 }
为了定义设置的一些配置,我们还需要定义一个特性用于设置的默认值/范围等:
using abp.configuration; using system; namespace skys.charger.configuration { [attributeusage(attributetargets.property)] public class autosettingdefinitionattribute : attribute { public object defaultvalue { get; private set; } public bool isvisibletoclients { get; private set; } public settingscopes scopes { get; private set; } public autosettingdefinitionattribute(object defaultvalue, bool isvisibletoclients = true, settingscopes scopes = settingscopes.application) { this.defaultvalue = defaultvalue; this.isvisibletoclients = isvisibletoclients; this.scopes = scopes; } } }
接下来,我们需要把所有继承至isettings的设置类都注册到settingprovider中,这里我直接使用的反射,从属性特性上读取设置的配置:
1 using abp.configuration; 2 using system.collections.generic; 3 using system.linq; 4 using system.reflection; 5 6 namespace skys.charger.configuration 7 { 8 /// <summary> 9 /// 10 /// </summary> 11 public class autosettingsprovider : settingprovider 12 { 13 public override ienumerable<settingdefinition> getsettingdefinitions(settingdefinitionprovidercontext context) 14 { 15 var settings = new list<settingdefinition>(); 16 17 var types = this.gettype().assembly 18 .gettypes() 19 .where(t => t.isclass && typeof(isettings).isassignablefrom(t)); 20 21 foreach (var type in types) 22 { 23 var scopes = settingscopes.all; 24 foreach (var p in type.getproperties()) 25 { 26 var key = autosettingsutils.createsettingname(type, p.name); 27 var isvisibletoclients = false; 28 var defaultvalue = autosettingsutils.getdefaultvalue(p.propertytype); 29 var attr = p.getcustomattribute<autosettingdefinitionattribute>(); 30 if (attr != null) 31 { 32 scopes = attr.scopes; 33 defaultvalue = attr.defaultvalue; 34 isvisibletoclients = attr.isvisibletoclients; 35 } 36 settings.add(new settingdefinition( 37 name: key, 38 defaultvalue: defaultvalue?.tostring(), 39 scopes: scopes, 40 isvisibletoclients: isvisibletoclients 41 )); 42 } 43 } 44 45 return settings; 46 } 47 } 48 }
接下来定义一个interceptor用于拦截设置类中属性的get/set方法,在拦截器中注入了isettingmanager及abpsession用于获取和修改设置,在修改的时候如果scope支持user优先修改用户设置,然后是租户设置,最后是应用设置
1 using abp.configuration; 2 using abp.runtime.session; 3 using castle.dynamicproxy; 4 using skys.charger.utilities; 5 6 namespace skys.charger.configuration 7 { 8 /// <summary> 9 /// 自动配置拦截器,用于获取/修改配置值 10 /// </summary> 11 public class autosettingsinterceptor : iinterceptor 12 { 13 private readonly isettingmanager _settingmanager; 14 private readonly isettingdefinitionmanager _settingdefinitionmanager; 15 public iabpsession abpsession { get; set; } 16 public autosettingsinterceptor(isettingmanager settingmanager, isettingdefinitionmanager settingdefinitionmanager) 17 { 18 this._settingmanager = settingmanager; 19 this._settingdefinitionmanager = settingdefinitionmanager; 20 this.abpsession = nullabpsession.instance; 21 } 22 23 protected void postproceed(iinvocation invocation) 24 { 25 var setflag = "set_"; 26 var getflag = "get_"; 27 28 var isset = invocation.method.name.startswith(setflag); 29 var isget = invocation.method.name.startswith(getflag); 30 //非属性方法不处理 31 if (!isset && !isget) 32 return; 33 34 var pname = invocation.method.name.replace(setflag, "") 35 .replace(getflag, ""); 36 var settingname = autosettingsutils.createsettingname(invocation.targettype, pname); 37 //配置 设置 38 if (isset) 39 { 40 var setting = this._settingdefinitionmanager.getsettingdefinition(settingname); 41 this.changesettingvalue(setting, invocation.arguments[0]?.tostring()); 42 } 43 //配置 获取 44 else 45 { 46 var val = this._settingmanager.getsettingvalue(settingname); 47 invocation.returnvalue = converthelper.changetype(val, invocation.method.returntype); 48 } 49 } 50 protected void changesettingvalue(settingdefinition settings, object value) 51 { 52 var val = value?.tostring(); 53 if (settings.scopes.hasflag(settingscopes.user) && this.abpsession.userid.hasvalue) 54 this._settingmanager.changesettingforuser(this.abpsession.touseridentifier(), settings.name, val); 55 else if (settings.scopes.hasflag(settingscopes.tenant) && this.abpsession.tenantid.hasvalue) 56 this._settingmanager.changesettingfortenant(this.abpsession.tenantid.value, settings.name, val); 57 else if (settings.scopes.hasflag(settingscopes.application)) 58 this._settingmanager.changesettingforapplication(settings.name, val); 59 } 60 61 public void intercept(iinvocation invocation) 62 { 63 invocation.proceed(); 64 this.postproceed(invocation); 65 } 66 } 67 }
定义完以后,我们还需要注册我们的拦截器,这里我使用了一个manager来注册,通过传入abpmodule中的configuration来完成注册
1 using abp.configuration.startup; 2 using castle.core; 3 4 namespace skys.charger.configuration 5 { 6 public class autosettingsmanager 7 { 8 public static void initialize(iabpstartupconfiguration configuration) 9 { 10 configuration.iocmanager.ioccontainer.kernel.componentregistered += (key, handler) => 11 { 12 if (typeof(isettings).isassignablefrom(handler.componentmodel.implementation)) 13 { 14 handler.componentmodel.interceptors.add(new interceptorreference(typeof(autosettingsinterceptor))); 15 } 16 }; 17 18 //把自动属性的provider注册 19 configuration.settings.providers.add<autosettingsprovider>(); 20 } 21 } 22 }
然后在你定义配置类型的module的preinitialize()中完成注册:
//自动配置初始化 autosettingsmanager.initialize(configuration);
到这里我们的工作基本上也就完成了,接下来我们就可以定义我们自己的设置类了,因为我们注入使用是类,所以定义的属性都需要加上virtual以便拦截器能正常工具
1 using abp.automapper; 2 using abp.configuration; 3 4 namespace skys.charger.configuration.settings 5 { 6 [automap(typeof(appsettings))] 7 public class appsettings : isettings 8 { 9 [autosettingdefinition("skys.charger")] 10 public virtual string systemname { get; set; } 11 12 [autosettingdefinition(20)] 13 public virtual int pagesize { get; set; } 14 15 /// <summary> 16 /// 得现手续费 17 /// </summary> 18 [autosettingdefinition(0.02)] 19 public virtual decimal takeservicefeerate { get; set; } 20 } 21 }
在任意使用的地方,直接注入即可使用,并且只要是注入的配置类型,设置它的属性即可完成修改并保存到数据库,获取也是直接从isettingmanager中获取值,再配合前端修改的时候就方便多了
1 namespace skys.charger.configuration 2 { 3 public class configurationappservice : applicationservice 4 { 5 private readonly appsettings _appsettings; 6 public configurationappservice(appsettings appsettings) 7 { 8 this._appsettings = appsettings; 9 } 10 11 /// <summary> 12 /// 获取系统配置 13 /// </summary> 14 public async task<appsettings> getsystemsettings() 15 { 16 return await task.fromresult(_appsettings); 17 } 18 /// <summary> 19 /// 修改系统配置 20 /// </summary> 21 [managerauthorize] 22 public async task changesystemsettings(appsettings appsettings) 23 { 24 this.objectmapper.map(appsettings, _appsettings); 25 26 await task.completedtask; 27 } 28 } 29 }
是不是比原来的使用方式简单了很多呢,因为是所有配置类型都是isingletondependency在不方便的地方还可以直接使用iocmanager.instance.resolve<appsettings>()直接获取:
1 public class pagedrequestfilter : ishouldnormalize 2 { 3 //public isettingmanager settingmanager { get; set; } 4 5 public const int defaultsize = 20; 6 7 //[range(1, 10000)] 8 public int page { get; set; } 9 10 //[range(1,100)] 11 public int size { get; set; } 12 13 public void normalize() 14 { 15 if (this.page <= 0) 16 this.page = 1; 17 if (this.size <= 0) 18 { 19 var appsettings = iocmanager.instance.resolve<appsettings>(); 20 this.size = appsettings.pagesize; 21 } 22 } 23 }
最后附上中间使用过的两个工具类autosettingsutils和converthelper
public static class autosettingsutils { public static string createsettingname(type type, string propertyname) { return $"{type.name}.{propertyname}"; } public static object getdefaultvalue(type targettype) { return targettype.isvaluetype ? activator.createinstance(targettype) : null; } }
1 /// <summary> 2 /// 数据转换帮助类 3 /// </summary> 4 public static class converthelper 5 { 6 #region = changetype = 7 public static object changetype(object obj, type conversiontype) 8 { 9 return changetype(obj, conversiontype, thread.currentthread.currentculture); 10 } 11 public static object changetype(object obj, type conversiontype, iformatprovider provider) 12 { 13 #region nullable 14 type nullabletype = nullable.getunderlyingtype(conversiontype); 15 if (nullabletype != null) 16 { 17 if (obj == null) 18 { 19 return null; 20 } 21 return convert.changetype(obj, nullabletype, provider); 22 } 23 #endregion 24 if (typeof(system.enum).isassignablefrom(conversiontype)) 25 { 26 return enum.parse(conversiontype, obj.tostring()); 27 } 28 return convert.changetype(obj, conversiontype, provider); 29 } 30 #endregion 31 }
上一篇: LeetCode 202. 快乐数