private static void Register(IUnityContainer container,
            ServiceDescriptor descriptor)
        {
            if (descriptor.ImplementationType != null)
            {
                container.RegisterType(descriptor.ServiceType,
                    descriptor.ImplementationType,
                    GetLifetimeManager(descriptor.Lifetime));

                container.RegisterType(descriptor.ServiceType,
                    descriptor.ImplementationType,
                    descriptor.ImplementationType.ToString(),
                    GetLifetimeManager(descriptor.Lifetime));
            }
            else if (descriptor.ImplementationFactory != null)
            {
                container.RegisterType(descriptor.ServiceType,
                    GetLifetimeManager(descriptor.Lifetime),
                    new InjectionFactory(unity =>
                    {
                        var provider = unity.Resolve<IServiceProvider>();
                        return descriptor.ImplementationFactory(provider);
                    }));

                container.RegisterType(descriptor.ServiceType,
                    Guid.NewGuid().ToString(),
                    GetLifetimeManager(descriptor.Lifetime),
                    new InjectionFactory(unity =>
                    {
                        var provider = unity.Resolve<IServiceProvider>();
                        return descriptor.ImplementationFactory(provider);
                    }));
            }
            else if (descriptor.ImplementationInstance != null)
            {
                container.RegisterInstance(descriptor.ServiceType,
                    descriptor.ImplementationInstance,
                    GetLifetimeManager(descriptor.Lifetime));

                container.RegisterInstance(descriptor.ServiceType,
                    Guid.NewGuid().ToString(),
                    descriptor.ImplementationInstance,
                    GetLifetimeManager(descriptor.Lifetime));
            }
        }
Ejemplo n.º 2
0
 private static void Use(GenericFamilyExpression expression, ServiceDescriptor descriptor)
 {
     if (descriptor.ImplementationFactory != null)
     {
         expression.Use(Guid.NewGuid().ToString(), context => { return descriptor.ImplementationFactory(context.GetInstance<IServiceProvider>()); });
     }
     else if (descriptor.ImplementationInstance != null)
     {
         expression.Use(descriptor.ImplementationInstance);
     }
     else if (descriptor.ImplementationType != null)
     {
         expression.Use(descriptor.ImplementationType);
     }
     else
     {
         throw new InvalidOperationException("IServiceDescriptor is invalid");
     }
 }
 private static Expression<Func<IContext, object>> CreateFactory(ServiceDescriptor descriptor)
 {
     return context => descriptor.ImplementationFactory(context.GetInstance<IServiceProvider>());
 }