|
1 | 1 | using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Linq; |
4 | 2 | using System.Reflection; |
5 | 3 |
|
6 | 4 | using Autofac.Core; |
@@ -42,9 +40,7 @@ public void interceptor_registration_with_registercallback_should_work_with_prop |
42 | 40 |
|
43 | 41 | var orderService = The<IOrderAppService>(); |
44 | 42 | orderService.ProductAppService.ShouldNotBeNull(); |
45 | | - |
46 | | - var orderAppService = The<OrderAppService>(); |
47 | | - orderAppService.ProductAppService.ShouldNotBeNull(); |
| 43 | + orderService.DoSomeCoolStuff(); |
48 | 44 | } |
49 | 45 |
|
50 | 46 | [Fact] |
@@ -72,20 +68,13 @@ public void multiple_interceptor_should_be_able_to_intercept_any_dependency() |
72 | 68 | { |
73 | 69 | Building(builder => |
74 | 70 | { |
75 | | - builder.RegisterServices(r => r.UseBuilder(cb => cb.RegisterCallback(registry => registry.Registered += (sender, args) => |
76 | | - { |
77 | | - MultipleInterceptorRegistrar(args); |
78 | | - }))); |
| 71 | + builder.RegisterServices(r => r.UseBuilder(cb => cb.RegisterCallback(registry => registry.Registered += (sender, args) => { MultipleInterceptorRegistrar(args); }))); |
79 | 72 | builder.RegisterServices(r => r.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly())); |
80 | 73 | }); |
81 | 74 |
|
82 | 75 | var orderService = The<IOrderAppService>(); |
83 | 76 | orderService.ProductAppService.ShouldNotBeNull(); |
84 | 77 | orderService.DoSomeCoolStuff(); |
85 | | - |
86 | | - var orderAppService = The<OrderAppService>(); |
87 | | - orderAppService.ProductAppService.ShouldNotBeNull(); |
88 | | - orderAppService.DoSomeCoolStuff(); |
89 | 78 | } |
90 | 79 |
|
91 | 80 | private static void UnitOfWorkRegistrar(ComponentRegisteredEventArgs args) |
@@ -149,6 +138,13 @@ public interface IOrderAppService |
149 | 138 |
|
150 | 139 | public class OrderAppService : IOrderAppService, IApplicationService |
151 | 140 | { |
| 141 | + private readonly IProductAppService _productAppService; |
| 142 | + |
| 143 | + public OrderAppService(IProductAppService productAppService) |
| 144 | + { |
| 145 | + _productAppService = productAppService; |
| 146 | + } |
| 147 | + |
152 | 148 | public IProductAppService ProductAppService { get; set; } |
153 | 149 |
|
154 | 150 | public virtual void DoSomeCoolStuff() |
@@ -179,45 +175,5 @@ public void Log(string message) |
179 | 175 | { |
180 | 176 | } |
181 | 177 | } |
182 | | - |
183 | | - public class UnitOfWorkInterceptorRegistrarModule : Module |
184 | | - { |
185 | | - // This is a private constant from the Autofac.Extras.DynamicProxy2 assembly |
186 | | - // that is needed to "poke" interceptors into registrations. |
187 | | - private const string InterceptorsPropertyName = "Autofac.Extras.DynamicProxy2.RegistrationExtensions.InterceptorsPropertyName"; |
188 | | - |
189 | | - protected override void Load(ContainerBuilder builder) |
190 | | - { |
191 | | - builder.RegisterType<UnitOfWorkInterceptor>().As<IInterceptor>().AsSelf(); |
192 | | - } |
193 | | - |
194 | | - protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration) |
195 | | - { |
196 | | - // Here is where you define your "global interceptor list" |
197 | | - var interceptorServices = new Service[] { new TypedService(typeof(UnitOfWorkInterceptor)) }; |
198 | | - |
199 | | - Type impl = registration.Activator.LimitType; |
200 | | - |
201 | | - //Type[] services = registration.Services.OfType<TypedService>().Select(x => x.ServiceType).ToArray(); |
202 | | - |
203 | | - // Append the global interceptors to any existing list, or create a new interceptor |
204 | | - // list if none are specified. Note this info will only be used by registrations |
205 | | - // that are set to have interceptors enabled. It'll be ignored by others. |
206 | | - |
207 | | - if (typeof(IApplicationService).IsAssignableFrom(impl)) |
208 | | - { |
209 | | - object existing; |
210 | | - if (registration.Metadata.TryGetValue(InterceptorsPropertyName, out existing)) |
211 | | - { |
212 | | - registration.Metadata[InterceptorsPropertyName] = |
213 | | - ((IEnumerable<Service>)existing).Concat(interceptorServices).Distinct(); |
214 | | - } |
215 | | - else |
216 | | - { |
217 | | - registration.Metadata.Add(InterceptorsPropertyName, interceptorServices); |
218 | | - } |
219 | | - } |
220 | | - } |
221 | | - } |
222 | 178 | } |
223 | 179 | } |
0 commit comments