哪些网站做外贸效果好,中国百强企业,app和网站的区别,鹏牛网做网站怎么样距离上一篇AspectCore的介绍发布已经很长一段时间了#xff0c;这篇文章也早该和大家见面#xff0c;最近一直忙于适应新工作#xff0c;并在业余时间有幸向何镇汐#xff0c;Savorboard#xff0c;农夫#xff0c;AlexLEWIS等几位大牛请教学习#xff0c;收获颇多。另一…距离上一篇AspectCore的介绍发布已经很长一段时间了这篇文章也早该和大家见面最近一直忙于适应新工作并在业余时间有幸向何镇汐Savorboard农夫AlexLEWIS等几位大牛请教学习收获颇多。另一方面一直在对AspectCore进行重构并把AspectCore从AspectCore Project迁移到.NET China Foundation(前身为AspNetCore文档翻译小组)由我和 DotNetCore 项目组共同维护。下面言归正传一起来看一下AspectCore给大家带来的新特性。
AspectCore 0.1.2-release
AspectCore v0.1.2已经正式发布相对于前一个版本V0.1.2中除了修复若干bug外最重要的特性就是带来了方法参数拦截器和方法参数注入的支持。
参数拦截器
我们先来看下面的方法
public class AppService : IAppService{ public void Run(string[] args) { if (args null){ throw new ArgumentNullException(nameof(args));} // todo..}
}
在开发中对方法的输入参数进行非空校验是一个不错的编程习惯可以避免意外的抛出NullReferenceException但对于所有方法的参数都进行如上的手动验证的话又是一个很耗费人力的事情。那么能不能只在参数上标记自定义的特性让框架来帮助我们进行自动的参数验证呢比如下面的代码
public void Run([NotNull]string[] args){ //todo..}
接下来让我们来看看AspectCore是怎么做的吧。
新建.net core的ConsoleApp项目添加AspectCore的nuget packagesPM Install-Package AspectCore.Extensions.DependencyInjectionPM Install-Package AspectCore.Extensions.CrossParameters在Main方法中添加public class Program{ public static void Main(string[] args) {IServiceCollection services new ServiceCollection();services.AddAspectCore(options {options.AddMethodInject();options.AddParameterIntercept();});services.AddTransientIAppService, AppService();services.AddTransientIAppLifetime, AppLifetime();IServiceProvider aspectCoreServiceProvider services.BuildAspectCoreServiceProvider();Console.ReadKey();}
}参数拦截器定义在AspectCore.Extensions.CrossParameters命名空间下并已经内置NotNullAttribute进行非空验证我们来通过继承ParameterInterceptorAttribute来自定义另一个参数拦截器NotNullOrEmptyAttribute:public class NotNullOrEmptyAttribute : ParameterInterceptorAttribute{ public override Task Invoke(IParameterDescriptor parameter, ParameterAspectContext context, ParameterAspectDelegate next) { if (parameter.ParameterType typeof(string)){ if (string.IsNullOrEmpty(parameter.Value?.ToString())){ throw new ArgumentException($Invalid parameter. {parameter.Name} cannot be null or empty);}} return next(parameter, context);}
}定义需要进行参数拦截的方法public interface IAppService{ void Run([NotNull]string[] args); void Stop([NotNullOrEmpty]string message); //..}通过AspectCore创建的类型实例便能在运行时对标记了特性的方法参数进行拦截验证。
方法参数注入
在Asp.Net Core MVC中我们通过在Action的参数上标记[FromServices]来进行参数注入。那么在非Controller类和非Action方法中我们同样可以通过AspectCore完成类似的功能。定义如下方法: public interface IAppService{ Lifetime GetLifetime([Inject]IAppLifetime lifetime null);}
那么我们可以在调用时
var appService aspectCoreServiceProvider.GetServiceIAppService();
appService.GetLifetime();
参数IAppLifetime lifetime便会自动在运行时注入到方法中。本文的示例代码在AspectCore/Sample。
AspectCore的后续计划
AspectCore 正式更名为 AspectCore Framework旨在提供以AOP为核心的跨平台开发框架正在开发的版本定义为v0.2-preview1在这个版本中我们会添加如下基础组件
AspectCore.Extensions.Reflection : 高性能的反射扩展AspectCore.Extensions.Expression : 表达式树和Emit扩展AspectCore.Extensions.Mapper : 轻量级对象映射库AspectCore.Extensions.DataValidations : 服务层方法的数据验证组件AspectCore.Extensions.CrossProperties : 属性拦截器
有问题反馈
如果您有任何想法或建议欢迎提交Issues给我们。QQ群 dotNET Core Studying Group 436035237
欢迎加入
如果您对AspectCore Framework感兴趣或者想对.net core做出贡献都欢迎您加入 AspectCore Framework 或 .NET China Foundation下的任何其他项目。如果您有优秀的.net core开源项目也欢迎项目加入 .NET China Foundation。现在是.net core开源最好的时代让我们一起为.net core贡献自己的一份力量吧
相关文章 [Asp.Net Core轻量级Aop解决方案]AspectCore Project 介绍Asp.Net Core轻量级Aop解决方案AspectCore
原文地址http://www.cnblogs.com/liuhaoyang/p/aspectcore-crossparameters.html .NET社区新闻深度好文微信中搜索dotNET跨平台或扫描二维码关注