03-EF Core笔记之查询数据
https://www.cnblogs.com/youring2/p/11186614.html
using (var context = new BloggingContext()) { var blogs = context.Blogs .Include(blog => blog.Posts) .Include(blog => blog.Owner) .ToList(); }
Castle DynamicProxy基本用法(AOP)
https://www.cnblogs.com/youring2/p/10962573.html
Autofac的集成#
Autofac集成了对DynamicProxy的支持,我们需要引用Autofac.Extras.DynamicProxy
,然后创建容器、注册服务、生成实例、调用方法,我们来看下面的代码:
ContainerBuilder builder = new ContainerBuilder(); //注册拦截器 builder.RegisterType<LoggerInterceptor>().AsSelf(); //注册基础服务 builder.RegisterType<ConsoleLogger>().AsImplementedInterfaces(); //注册要拦截的服务 builder.RegisterType<ProductRepository>().AsImplementedInterfaces() .EnableInterfaceInterceptors() //启用接口拦截 .InterceptedBy(typeof(LoggerInterceptor)); //指定拦截器 var container = builder.Build(); //解析服务 var productRepository = container.Resolve<IProductRepository>(); Product product = new Product() { Name = "Book" }; productRepository.Update(product);