1+ using Autofac ;
2+ using System ;
3+ using AspectCore . Configuration ;
4+ using AspectCore . Extensions . Autofac ;
5+ using AspectCore . Extensions . Test . Fakes ;
6+ using Xunit ;
7+ using static AspectCoreTest . Autofac . Issues . PropertyInjectorProxyWithVirtualTests ;
8+ using System . Threading . Tasks ;
9+ using AspectCore . DynamicProxy ;
10+
11+ namespace AspectCoreTest . Autofac . Issues
12+ {
13+ public class PropertiesAutowiredTests
14+ {
15+ public interface IService
16+ {
17+ int Run ( ) ;
18+ string GetString ( string key ) ;
19+ string GetStringReturnValueModify ( string key ) ;
20+ }
21+
22+ public interface IServiceTwo
23+ {
24+ int Run ( ) ;
25+ string GetString ( string key ) ;
26+ }
27+
28+ public class Service : IService
29+ {
30+ public IServiceTwo ServiceTwo { get ; set ; }
31+
32+ [ CacheInterceptor ]
33+ public int Run ( )
34+ {
35+ return ServiceTwo . Run ( ) ;
36+ }
37+
38+ [ StringParameterIntercept ]
39+ public string GetString ( string key )
40+ {
41+ return ServiceTwo . GetString ( key ) ;
42+ }
43+
44+ [ ReturnValueIntercept ]
45+ public string GetStringReturnValueModify ( string key )
46+ {
47+ return ServiceTwo . GetString ( key ) ;
48+ }
49+ }
50+
51+ public class ServiceTwo : IServiceTwo
52+ {
53+ public int Run ( )
54+ {
55+ return 1024 ;
56+ }
57+
58+ public string GetString ( string key )
59+ {
60+ return key ;
61+ }
62+ }
63+
64+ public class ControllerAction
65+ {
66+ public IService Service { get ; set ; }
67+
68+ public int ActionRun ( )
69+ {
70+ return Service . Run ( ) ;
71+ }
72+
73+ public string GetString ( string key )
74+ {
75+ return Service . GetString ( key ) ;
76+ }
77+
78+ public string GetStringReturnValueModify ( string key )
79+ {
80+ return Service . GetStringReturnValueModify ( key ) ;
81+ }
82+ }
83+
84+ public class StringParameterIntercept : AbstractInterceptorAttribute
85+ {
86+ public override Task Invoke ( AspectContext context , AspectDelegate next )
87+ {
88+ context . Parameters [ 0 ] = "EMT" ;
89+ return context . Invoke ( next ) ;
90+ }
91+ }
92+
93+ public class ReturnValueIntercept : AbstractInterceptorAttribute
94+ {
95+ public override async Task Invoke ( AspectContext context , AspectDelegate next )
96+ {
97+ await next ( context ) ;
98+ context . ReturnValue = "Emilia" ;
99+ }
100+ }
101+
102+ private ContainerBuilder CreateBuilder ( )
103+ {
104+ return new ContainerBuilder ( ) . RegisterDynamicProxy ( config =>
105+ {
106+ config . Interceptors . AddDelegate ( next => ctx => next ( ctx ) , Predicates . ForNameSpace ( "AspectCore.Extensions.Test.Issues" ) ) ;
107+ } ) ;
108+ }
109+
110+ /// <summary>
111+ /// 多层属性注入测试
112+ /// </summary>
113+ [ Fact ]
114+ public void PropertiesAutowiredWithMultilayer_Test ( )
115+ {
116+ var builder = CreateBuilder ( ) ;
117+ builder . RegisterType < ControllerAction > ( ) . PropertiesAutowired ( ) ;
118+ builder . RegisterType < Service > ( ) . AsImplementedInterfaces ( ) . PropertiesAutowired ( ) ;
119+ builder . RegisterType < ServiceTwo > ( ) . AsImplementedInterfaces ( ) . PropertiesAutowired ( ) ;
120+ var container = builder . Build ( ) ;
121+ var action = container . Resolve < ControllerAction > ( ) ;
122+ action . ActionRun ( ) ;
123+ }
124+
125+ /// <summary>
126+ /// 多层属性注入下启用拦截器测试
127+ /// </summary>
128+ [ Fact ]
129+ public void PropertiesAutowiredWithMultilayerIntercept_Test ( )
130+ {
131+ var builder = CreateBuilder ( ) ;
132+ builder . RegisterType < ControllerAction > ( ) . PropertiesAutowired ( ) ;
133+ builder . RegisterType < Service > ( ) . AsImplementedInterfaces ( ) . PropertiesAutowired ( ) ;
134+ builder . RegisterType < ServiceTwo > ( ) . AsImplementedInterfaces ( ) . PropertiesAutowired ( ) ;
135+ var container = builder . Build ( ) ;
136+ var action = container . Resolve < ControllerAction > ( ) ;
137+ Assert . Equal ( "EMT" , action . GetString ( "Lion" ) ) ;
138+ Assert . Equal ( "Emilia" , action . GetStringReturnValueModify ( "Lion" ) ) ;
139+ }
140+ }
141+ }
0 commit comments