src/Topelab.Core.Resolver.Autofac/ResolveInfoCollectionFactory.cs
/// <summary>
/// Registers within the specified container the specified resolve info collection.
/// </summary>
/// <param name="builder">The container builder.</param>
/// <param name="resolveInfoCollection">The resolve info collection.</param>
/// <param name="constructorsByKey">The constructors by key.</param>
Registers within the specified container the specified resolve info collection.
Registers within the specified container the specified resolve info collection .
internal static void RegisterAutofac(this ContainerBuilder builder, ResolveInfoCollection resolveInfoCollection, Dictionary<string, ConstructorInfo> constructorsByKey)
        {
            resolveInfoCollection.ForEach(resolveInfo =>
            {
                var key = ResolverKeyFactory.Create(resolveInfo);
                var constructorInfo = GetConstructorInfo(resolveInfo);
                if (constructorInfo != null && key != null)
                {
                    constructorsByKey[key] = constructorInfo;
                }
                switch (resolveInfo.ResolveMode)
                {
                    case ResolveModeEnum.Instance:
                        builder.RegisterInstance(resolveInfo.Instance).As(resolveInfo.TypeFrom);
                        break;
                    case ResolveModeEnum.Factory:
                        builder.Register(c => resolveInfo.Factory.Invoke(c.Resolve<IResolver>())).WithName(resolveInfo.Key, resolveInfo.TypeFrom);
                        break;
                    default:
                        switch (resolveInfo.ResolveLifeCycle)
                        {
                            case ResolveLifeCycleEnum.Singleton:
                                if (resolveInfo.ConstructorParamTypes.Length > 0)
                                {
                                    builder.RegisterType(resolveInfo.TypeTo).WithName(resolveInfo.Key, resolveInfo.TypeFrom).UsingConstructor(resolveInfo.ConstructorParamTypes).SingleInstance();
                                }
                                else
                                {
                                    builder.RegisterType(resolveInfo.TypeTo).WithName(resolveInfo.Key, resolveInfo.TypeFrom).SingleInstance();
                                }
                                break;
                            case ResolveLifeCycleEnum.Scoped:
                                if (resolveInfo.ConstructorParamTypes.Length > 0)
                                {
                                    builder.RegisterType(resolveInfo.TypeTo).WithName(resolveInfo.Key, resolveInfo.TypeFrom).UsingConstructor(resolveInfo.ConstructorParamTypes).InstancePerLifetimeScope();
                                }
                                else
                                {
                                    builder.RegisterType(resolveInfo.TypeTo).WithName(resolveInfo.Key, resolveInfo.TypeFrom).InstancePerLifetimeScope();
                                }
                                break;
                            default:
                                if (resolveInfo.ConstructorParamTypes.Length > 0)
                                {
                                    builder.RegisterType(resolveInfo.TypeTo).WithName(resolveInfo.Key, resolveInfo.TypeFrom).UsingConstructor(resolveInfo.ConstructorParamTypes).InstancePerDependency();
                                }
                                else
                                {
                                    builder.RegisterType(resolveInfo.TypeTo).WithName(resolveInfo.Key, resolveInfo.TypeFrom).InstancePerDependency();
                                }
                                break;
                        }
                        break;
                }
            });
        }
internal static void RegisterAutofac ( this ContainerBuilder builder , ResolveInfoCollection resolveInfoCollection , Dictionary < string , ConstructorInfo > constructorsByKey ) { resolveInfoCollection . ForEach ( resolveInfo => { var key = ResolverKeyFactory . Create ( resolveInfo ) ; var constructorInfo = GetConstructorInfo ( resolveInfo ) ; if ( constructorInfo != null && key != null ) { constructorsByKey [ key ] = constructorInfo ; } switch ( resolveInfo . ResolveMode ) { case ResolveModeEnum . Instance : builder . RegisterInstance ( resolveInfo . Instance ) . As ( resolveInfo . TypeFrom ) ; break ; case ResolveModeEnum . Factory : builder . Register ( c => resolveInfo . Factory . Invoke ( c . Resolve < IResolver > ( ) ) ) . WithName ( resolveInfo . Key , resolveInfo . TypeFrom ) ; break ; default : switch ( resolveInfo . ResolveLifeCycle ) { case ResolveLifeCycleEnum . Singleton : if ( resolveInfo . ConstructorParamTypes . Length > 0 ) { builder . RegisterType ( resolveInfo . TypeTo ) . WithName ( resolveInfo . Key , resolveInfo . TypeFrom ) . UsingConstructor ( resolveInfo . ConstructorParamTypes ) . SingleInstance ( ) ; } else { builder . RegisterType ( resolveInfo . TypeTo ) . WithName ( resolveInfo . Key , resolveInfo . TypeFrom ) . SingleInstance ( ) ; } break ; case ResolveLifeCycleEnum . Scoped : if ( resolveInfo . ConstructorParamTypes . Length > 0 ) { builder . RegisterType ( resolveInfo . TypeTo ) . WithName ( resolveInfo . Key , resolveInfo . TypeFrom ) . UsingConstructor ( resolveInfo . ConstructorParamTypes ) . InstancePerLifetimeScope ( ) ; } else { builder . RegisterType ( resolveInfo . TypeTo ) . WithName ( resolveInfo . Key , resolveInfo . TypeFrom ) . InstancePerLifetimeScope ( ) ; } break ; default : if ( resolveInfo . ConstructorParamTypes . Length > 0 ) { builder . RegisterType ( resolveInfo . TypeTo ) . WithName ( resolveInfo . Key , resolveInfo . TypeFrom ) . UsingConstructor ( resolveInfo . ConstructorParamTypes ) . InstancePerDependency ( ) ; } else { builder . RegisterType ( resolveInfo . TypeTo ) . WithName ( resolveInfo . Key , resolveInfo . TypeFrom ) . InstancePerDependency ( ) ; } break ; } break ; } } ) ; }
Registers within the specified container the specified resolve info collection.
Registers within the specified container the specified resolve info collection .
ContainerBuilder
this
resolveInfoCollection
ResolveInfoCollection
The resolve info collection.
The resolve info collection .
constructorsByKey
Dictionary<string, ConstructorInfo>
The constructors by key.
The constructors by key .
builder
The container builder.
The container builder .