Commits

Anonymous committed 2d93db6

Basic open generic support for 2.0-experimental.

  • Participants
  • Parent commits 4062837
  • Branches 2.0-experimental

Comments (0)

Files changed (6)

File Autofac.Tests/LifetimeEntryPointsTests.cs

             var ret = container.Resolve<AddOperation>().Invoke(1, 2);
             Assert.AreEqual(3, ret);
         }
-
-        public delegate void AddAction(int a, int b);
-
-        [Test]
-        public void ExecutesActionInNestedScope()
-        {
-            Assert.Fail();
-        }
     }
 }

File Autofac/OpenGenerics/OpenGenericActivatorGenerator.cs

 
         public bool TryGenerateActivator(Service service, IEnumerable<Service> configuredServices, out IInstanceActivator activator, out IEnumerable<Service> services)
         {
-            throw new NotImplementedException();
+            Type[] genericParameters = null;
+            Type genericTypeDefinition = null;
+
+            var typedService = service as TypedService;
+            if (typedService != null)
+            {
+                var serviceType = typedService.ServiceType;
+                if (serviceType.IsGenericType)
+                {
+                    genericTypeDefinition = serviceType.GetGenericTypeDefinition();
+                    genericParameters = serviceType.GetGenericArguments();
+                }
+            }
+
+            if (genericTypeDefinition != null &&
+                configuredServices
+                    .Cast<TypedService>()
+                    .Any(ts => ts.ServiceType == genericTypeDefinition))
+            {
+                activator = new ReflectionActivator(
+                    _implementationType.MakeGenericType(genericParameters),
+                    _constructorFinder,
+                    _constructorSelector);
+                
+                services = configuredServices
+                    .Cast<TypedService>()
+                    .Select(ts => new TypedService(ts.ServiceType.MakeGenericType(genericParameters)))
+                    .Cast<Service>();
+
+                return true;
+            }
+
+            activator = null;
+            services = null;
+            return false;
         }
     }
 }

File Autofac/RegistrationExtensions.cs

         public static IDynamicReflectiveRegistrar RegisterGenericType(this IContainer container, Type openGenericType)
         {
             var activatorGenerator = new OpenGenericActivatorGenerator(openGenericType);
-            var registrar = new DynamicRegistrar();
+            var registrar = new DynamicRegistrar(new TypedService(openGenericType));
             container.ComponentRegistry.AddDynamicRegistrationSource(new DynamicRegistrationSource(registrar, activatorGenerator));
             return new DynamicReflectiveRegistrar(registrar, activatorGenerator);
         }

File Autofac/RegistrationSources/DynamicRegistrationSource.cs

                 services,
                 _registrationData.ExtendedProperties);
 
+            foreach (var activatingHandler in _registrationData.ActivatingHandlers)
+                registration.Activating += activatingHandler;
+
+            foreach (var activatedHandler in _registrationData.ActivatedHandlers)
+                registration.Activated += activatedHandler;
+
             return true;
         }
     }

File Autofac/Syntax/ConcreteRegistrar.cs

             return As(new TypedService(typeof(TService1)), new TypedService(typeof(TService2)), new TypedService(typeof(TService3)));
         }
 
+        public IConcreteRegistrar<T> As(params Type[] services)
+        {
+            return As(services.Select(t => new TypedService(t)).Cast<Service>().ToArray());
+        }
+
         public IConcreteRegistrar<T> As(params Service[] services)
         {
             Enforce.ArgumentNotNull(services, "services");
 
         public IConcreteRegistrar<T> OnActivating(Action<ActivatingEventArgs<T>> handler)
         {
+            Enforce.ArgumentNotNull(handler, "handler");
             ActivatingHandlers.Add((s, e) =>
             {
                 handler(new ActivatingEventArgs<T>(e.Context, e.Component, (T)e.Instance));

File Autofac/Syntax/DynamicRegistrar.cs

 using System;
 using Autofac.Services;
 using Autofac.Events;
+using System.Linq;
+using Autofac.Injection;
+using System.Collections.Generic;
 
 namespace Autofac.Syntax
 {
     public class DynamicRegistrar : RegistrationData, IDynamicRegistrar, IRegistrationData
     {
-        public DynamicRegistrar()
+        Service _defaultService;
+
+        public DynamicRegistrar(Service defaultService)
         {
+            _defaultService = Enforce.ArgumentNotNull(defaultService, "defaultService");
         }
 
         public IDynamicRegistrar ExternallyOwned()
             return this;
         }
 
-        public IDynamicRegistrar As(params Service[] services)
+        public IDynamicRegistrar As<TService>()
         {
-            return this;
+            return As(new TypedService(typeof(TService)));
+        }
+
+        public IDynamicRegistrar As<TService1, TService2>()
+        {
+            return As(new TypedService(typeof(TService1)), new TypedService(typeof(TService2)));
+        }
+
+        public IDynamicRegistrar As<TService1, TService2, TService3>()
+        {
+            return As(new TypedService(typeof(TService1)), new TypedService(typeof(TService2)), new TypedService(typeof(TService3)));
         }
 
         public IDynamicRegistrar As(params Type[] services)
         {
+            return As(services.Select(t => new TypedService(t)).Cast<Service>().ToArray());
+        }
+
+        public IDynamicRegistrar As(params Service[] services)
+        {
+            Enforce.ArgumentNotNull(services, "services");
+
+            foreach (var service in services)
+                base.Services.Add(service);
+
             return this;
         }
 
-        public IDynamicRegistrar OnActivating(Action<ActivatingEventArgs<object>> e)
+        public IDynamicRegistrar OnActivating(Action<ActivatingEventArgs<object>> handler)
         {
+            Enforce.ArgumentNotNull(handler, "handler");
+            ActivatingHandlers.Add((s, e) =>
+            {
+                handler(new ActivatingEventArgs<object>(e.Context, e.Component, e.Instance));
+            });
             return this;
         }
 
-        public IDynamicRegistrar OnActivated(Action<ActivatedEventArgs<object>> e)
+        public IDynamicRegistrar OnActivated(Action<ActivatedEventArgs<object>> handler)
         {
+            Enforce.ArgumentNotNull(handler, "handler");
+            ActivatedHandlers.Add((s, e) =>
+            {
+                handler(new ActivatedEventArgs<object>(e.Context, e.Component, e.Instance));
+            });
             return this;
         }
 
         public IDynamicRegistrar PropertiesAutowired()
         {
+            var injector = new AutowiringPropertyInjector();
+            ActivatingHandlers.Add((s, e) =>
+            {
+                injector.InjectProperties(e.Context, e.Instance, true);
+            });
             return this;
         }
 
         public IDynamicRegistrar PropertiesAutowired(bool allowCircularDependencies)
         {
-            return this;
+            if (allowCircularDependencies)
+            {
+                var injector = new AutowiringPropertyInjector();
+                ActivatedHandlers.Add((s, e) =>
+                {
+                    injector.InjectProperties(e.Context, e.Instance, true);
+                });
+                return this;
+            }
+            else
+            {
+                return PropertiesAutowired();
+            }
+        }
+
+        public override ICollection<Service> Services
+        {
+            get
+            {
+                var result = base.Services;
+                if (!result.Any())
+                    result = new[] { _defaultService };
+                return result;
+            }
+            protected set
+            {
+                base.Services = value;
+            }
         }
     }
 }