diff --git a/CC.Yi.API/CC.Yi.API.csproj b/CC.Yi.API/CC.Yi.API.csproj index 4e2559a2..697c4d89 100644 --- a/CC.Yi.API/CC.Yi.API.csproj +++ b/CC.Yi.API/CC.Yi.API.csproj @@ -5,6 +5,8 @@ + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/CC.Yi.API/Program.cs b/CC.Yi.API/Program.cs index 970c28f8..f10b5838 100644 --- a/CC.Yi.API/Program.cs +++ b/CC.Yi.API/Program.cs @@ -1,3 +1,4 @@ +using Autofac.Extensions.DependencyInjection; using CC.Yi.DAL; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; @@ -38,6 +39,6 @@ namespace CC.Yi.API .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); - }); + }).UseServiceProviderFactory(new AutofacServiceProviderFactory()); } } diff --git a/CC.Yi.API/Startup.cs b/CC.Yi.API/Startup.cs index fe9446c4..ec647137 100644 --- a/CC.Yi.API/Startup.cs +++ b/CC.Yi.API/Startup.cs @@ -1,5 +1,8 @@ +using Autofac; +using Autofac.Extras.DynamicProxy; using CC.Yi.BLL; +using CC.Yi.Common.Castle; using CC.Yi.DAL; using CC.Yi.IBLL; using CC.Yi.IDAL; @@ -45,8 +48,18 @@ namespace CC.Yi.API { options.UseSqlServer(connection, b => b.MigrationsAssembly("CC.Yi.API"));//设置数据库 }); - services.AddScoped(typeof(IBaseDal<>), typeof(BaseDal<>)); - services.AddScoped(typeof(IstudentBll), typeof(studentBll)); + //依赖注入转交给Autofac + //services.AddScoped(typeof(IBaseDal<>), typeof(BaseDal<>)); + //services.AddScoped(typeof(IstudentBll), typeof(studentBll)); + } + + //动态 面向AOP思想的依赖注入 Autofac + public void ConfigureContainer(ContainerBuilder builder) + { + builder.RegisterType(typeof(CustomAutofacAop)); + builder.RegisterGeneric(typeof(BaseDal<>)).As(typeof(IBaseDal<>)) ; + builder.RegisterType().As().EnableInterfaceInterceptors();//表示注入前后要执行Castle + } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/CC.Yi.Common/CC.Yi.Common.csproj b/CC.Yi.Common/CC.Yi.Common.csproj index a20524d3..efd8020e 100644 --- a/CC.Yi.Common/CC.Yi.Common.csproj +++ b/CC.Yi.Common/CC.Yi.Common.csproj @@ -5,6 +5,8 @@ + + diff --git a/CC.Yi.Common/Castle/CustomAutofacAop.cs b/CC.Yi.Common/Castle/CustomAutofacAop.cs new file mode 100644 index 00000000..526d39ef --- /dev/null +++ b/CC.Yi.Common/Castle/CustomAutofacAop.cs @@ -0,0 +1,23 @@ +锘縰sing Castle.DynamicProxy; +using System; +using System.Collections.Generic; +using System.Text; + +namespace CC.Yi.Common.Castle +{ + public class CustomAutofacAop : IInterceptor + { + public void Intercept(IInvocation invocation) + { + { + + //杩欓噷鍐欐墽琛屾柟娉曞墠 + } + invocation.Proceed();//鎵ц鍏蜂綋鐨勫疄渚 + { + + //杩欓噷鍐欐墽琛屾柟娉曞悗 + } + } + } +} diff --git a/CC.Yi.IBLL/CC.Yi.IBLL.csproj b/CC.Yi.IBLL/CC.Yi.IBLL.csproj index 54b41008..778e1962 100644 --- a/CC.Yi.IBLL/CC.Yi.IBLL.csproj +++ b/CC.Yi.IBLL/CC.Yi.IBLL.csproj @@ -6,6 +6,7 @@ + diff --git a/CC.Yi.IBLL/IBaseBll.cs b/CC.Yi.IBLL/IBaseBll.cs index 456c9be7..ff781ea7 100644 --- a/CC.Yi.IBLL/IBaseBll.cs +++ b/CC.Yi.IBLL/IBaseBll.cs @@ -1,10 +1,13 @@ -锘縰sing System; +锘縰sing Autofac.Extras.DynamicProxy; +using CC.Yi.Common.Castle; +using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; namespace CC.Yi.IBLL { + [Intercept(typeof(CustomAutofacAop))] public interface IBaseBll where T : class, new() { #region