public void SetConstructorSelector(IConstructorSelector selector) { if (selector != null) { ConstructorSelector = selector; } }
public ModelDataAccessor(Type type, ConstructorParameterBinding.Factory constructorParameterBindingFactory, ObjectRepositorySerializerFactory repositorySerializerFactory, IConstructorSelector constructorSelector) { if (constructorParameterBindingFactory == null) { throw new ArgumentNullException(nameof(constructorParameterBindingFactory)); } if (repositorySerializerFactory == null) { throw new ArgumentNullException(nameof(repositorySerializerFactory)); } if (constructorSelector == null) { throw new ArgumentNullException(nameof(constructorSelector)); } Type = type ?? throw new ArgumentNullException(nameof(type)); _childProperties = new Lazy <IImmutableList <ChildPropertyInfo> >(GetChildProperties); _modifiableProperties = new Lazy <IImmutableList <ModifiablePropertyInfo> >(GetModifiableProperties); _constructorBinding = new Lazy <ConstructorParameterBinding>(() => { var constructors = from c in Type.GetTypeInfo().GetConstructors() select constructorParameterBindingFactory(c); return(constructorSelector.SelectConstructorBinding(Type, constructors.ToArray())); }); repositorySerializerFactory().ValidateSerializable(Type); }
public override TImplementation Compose(IComponentComposer composer, bool throwOnUnknown, IConstructorSelector constructorSelector) { if (disposed) { throw new ObjectDisposedException(this.GetType().Name); } if (null == composer) { throw new ArgumentNullException(nameof(composer)); } if (null == constructorSelector) { throw new ArgumentNullException(nameof(constructorSelector)); } if (null == threadLocalInstance.Value) { threadLocalInstance.Value = (TImplementation)composer.ComposeUsingConstructor(typeof(TImplementation), throwOnUnknown, constructorSelector); } return(threadLocalInstance.Value); }
private DefaultConfiguration(Container container) { _constructorSelector = new ConstructorSelector(); _dependencyResolver = new DependencyResolver(this); _registrationStorage = new RegistrationStorage(); _typeActivator = new ExpressionActivator(container, _constructorSelector); }
/// <summary> /// Create an activator for the provided type. /// </summary> /// <param name="implementationType">Type to activate.</param> /// <param name="constructorFinder">Constructor finder.</param> /// <param name="constructorSelector">Constructor selector.</param> /// <param name="configuredParameters">Parameters configured explicitly for this instance.</param> /// <param name="configuredProperties">Properties configured explicitly for this instance.</param> public ReflectionActivator( Type implementationType, IConstructorFinder constructorFinder, IConstructorSelector constructorSelector, IEnumerable <Parameter> configuredParameters, IEnumerable <Parameter> configuredProperties) : base(implementationType) { if (constructorFinder == null) { throw new ArgumentNullException(nameof(constructorFinder)); } if (constructorSelector == null) { throw new ArgumentNullException(nameof(constructorSelector)); } if (configuredParameters == null) { throw new ArgumentNullException(nameof(configuredParameters)); } if (configuredProperties == null) { throw new ArgumentNullException(nameof(configuredProperties)); } _implementationType = implementationType; ConstructorFinder = constructorFinder; ConstructorSelector = constructorSelector; _configuredProperties = configuredProperties; _defaultParameters = configuredParameters.Concat( new Parameter[] { new AutowiringParameter(), new DefaultValueParameter() }); }
private void RegisterExplicitConstructor <TService, TImplementation>(Container container, IConstructorSelector selector) { var constructor = selector.GetConstructor(typeof(TImplementation)); this.constructors[CreateKey(typeof(TService), typeof(TImplementation))] = constructor; }
static Mocked() { var stubFactoryCollection = new StubFactoryCollection(); var moqBuilderFactory = new MoqBuilderFactory(stubFactoryCollection); stubFactoryCollection.Append(new EmptyStringFactory()); stubFactoryCollection.Append(new DefaultDateFactory()); stubFactoryCollection.Append(new DefaultPrimitiveTypeFactory()); stubFactoryCollection.Append(new NullableFactory()); stubFactoryCollection.Append(new InterfaceStubFactory(moqBuilderFactory)); stubFactoryCollection.Append(new TypeWithSingleParameterlessConstructorFactory(moqBuilderFactory)); stubFactoryCollection.Append(new TypeWithSingleConstructorFactory(moqBuilderFactory, stubFactoryCollection)); DefaultStubFactory = stubFactoryCollection; DefaultDependenciesFactory = new StubDependenciesFactory(stubFactoryCollection); DefaultInstanceFactory = new InstanceFactoryCollection( new ConcreteInstanceFactory(), new AbstractClassFactory(moqBuilderFactory) ); DefaultConstructorSelector = new ConstructorSelectorCollection( new ParameterlessConstructorSelector(), new SingleConstructorSelector() ); DefaultMockedInstanceFactory = new MockedInstanceFactory( DefaultConstructorSelector, DefaultDependenciesFactory, DefaultInstanceFactory ); }
public void Register <TConcrete>(IConstructorSelector selector) where TConcrete : class { this.RegisterExplicitConstructor <TConcrete, TConcrete>(this.container, selector); this.container.Register <TConcrete, TConcrete>(); }
public override TImplementation Compose(IComponentComposer composer, bool throwOnUnknown, IConstructorSelector constructorSelector) { if (disposed) { throw new ObjectDisposedException(this.GetType().Name); } if (null == composer) { throw new ArgumentNullException(nameof(composer)); } if (null == constructorSelector) { throw new ArgumentNullException(nameof(constructorSelector)); } if (!this.lazyInstance.IsValueCreated) { this.Composer = composer; this.ThrowOnUnknown = throwOnUnknown; this.ConstructorSelector = constructorSelector; } return(this.lazyInstance.Value); }
/// <summary> /// Initializes a new instance of the <see cref="ReflectionActivatorData"/> class. /// </summary> /// <param name="implementer">Type that will be activated.</param> public ReflectionActivatorData(Type implementer) { ImplementationType = implementer; _constructorFinder = DefaultConstructorFinder; _constructorSelector = DefaultConstructorSelector; }
public void RegisterSingle <TService, TImplementation>(IConstructorSelector selector) where TService : class where TImplementation : class, TService { this.RegisterExplicitConstructor <TService, TImplementation>(this.container, selector); this.container.RegisterSingle <TService, TImplementation>(); }
public void Register <TService, TImplementation>(IConstructorSelector selector, Lifestyle lifestyle) where TService : class where TImplementation : class, TService { this.RegisterExplicitConstructor <TImplementation>(selector); this.container.Register <TService, TImplementation>(lifestyle); }
public ReflectionActivator(Type implementationType, IConstructorFinder constructorFinder, IConstructorSelector constructorSelector, IEnumerable <Parameter> configuredParameters, IEnumerable <Parameter> configuredProperties) : base(Enforce.ArgumentNotNull <Type>(implementationType, "implementationType")) { this._implementationType = implementationType; this._constructorFinder = Enforce.ArgumentNotNull <IConstructorFinder>(constructorFinder, "constructorFinder"); this._constructorSelector = Enforce.ArgumentNotNull <IConstructorSelector>(constructorSelector, "constructorSelector"); this._configuredParameters = Enforce.ArgumentNotNull <IEnumerable <Parameter> >(configuredParameters, "configuredParameters"); this._configuredProperties = Enforce.ArgumentNotNull <IEnumerable <Parameter> >(configuredProperties, "configuredProperties"); }
/// <summary> /// Initializes a new instance of the <see cref="ContainerOption"/> class. /// </summary> /// <param name="useLightweightCodeGeneration">if set to <c>true</c>, use lightweight code generation to create object instances. /// Otherwise, use the <see cref="System.Reflection"/> instead.</param> /// <param name="ctorSelector">The constructor selector used to select an eligible constructor based on the provided parameter type and position.</param> /// <param name="autoRegistrationPolicies">The auto registration policies.</param> private ContainerOption(bool useLightweightCodeGeneration, IConstructorSelector ctorSelector, params IAutoRegistrationPolicy[] autoRegistrationPolicies) { _useLightweightCodeGeneration = useLightweightCodeGeneration; _constructorSelector = ctorSelector ?? new DefaultConstructorSelector( new BindingFlagsConstructorFinder(BindingFlags.Public), new AscendingConstructorSorter()); _autoRegistrationPolicies = autoRegistrationPolicies; }
public MockedInstanceFactory( IConstructorSelector constructorSelector, IDependenciesFactory dependenciesFactory, IInstanceFactory instanceFactory) { _dependenciesFactory = dependenciesFactory; _constructorSelector = constructorSelector; _instanceFactory = instanceFactory; }
// TODO: Add composition support using parameters that aren't in the container (Builder extension syntax). public T Compose <T>(bool throwOnUnknown = true, IComponentComposer composer = null, IConstructorSelector constructorSelector = null) { if (this.disposed) { throw new ObjectDisposedException(this.GetType().Name); } return((T)this.Compose(typeof(T), throwOnUnknown, composer, constructorSelector)); }
/// <summary> /// Ensures that there is a default for each option. /// </summary> public void EnsureDefaults() { if (DefaultLifecycle == null) { DefaultLifecycle = Lifecycle.Transient; } if (ConstructorSelector == null) { ConstructorSelector = new SinglePublicConstructorSelector(); } }
/// <summary> /// Creates a new instance of the <see cref="ConfigurationOptions"/> /// class. /// </summary> /// <param name="constructorSelector"> /// Indicates that unregistered types should be automatically resolved /// where possible. /// </param> /// <param name="expressionBuilder"> /// The mechanism by which constructors are selected. /// </param> /// <param name="defaultLifecycle"> /// The default lifecycle to apply to unregistered types. /// </param> /// <param name="autoResolve"> /// true if we are to auto-resolve unregistered types, otherwise false. /// </param> public ConfigurationOptions( IConstructorSelector constructorSelector = null, IExpressionBuilder expressionBuilder = null, ILifecycle defaultLifecycle = null, bool autoResolve = false ) { AutoResolve = autoResolve; ConstructorSelector = constructorSelector ?? new DefaultConstructorSelector(); DefaultLifecycle = defaultLifecycle ?? Lifecycle.Default; ExpressionBuilder = expressionBuilder ?? new ExpressionBuilder(); }
UsingConstructor <TLimit, TReflectionActivatorData, TStyle>( this IRegistrationBuilder <TLimit, TReflectionActivatorData, TStyle> registration, IConstructorSelector constructorSelector) where TReflectionActivatorData : ReflectionActivatorData { if (registration == null) { throw new ArgumentNullException(nameof(registration)); } registration.ActivatorData.ConstructorSelector = constructorSelector ?? throw new ArgumentNullException(nameof(constructorSelector)); return(registration); }
/// <summary> /// Initializes a new instance of the <see cref="TypeConstructionInfoBuilder"/> class. /// </summary> /// <param name="constructorSelector">The <see cref="IConstructorSelector"/> that is responsible /// for selecting the constructor to be used for constructor injection.</param> /// <param name="constructorDependencySelector">The <see cref="IConstructorDependencySelector"/> that is /// responsible for selecting the constructor dependencies for a given <see cref="ConstructionInfo"/>.</param> /// <param name="propertyDependencySelector">The <see cref="IPropertyDependencySelector"/> that is responsible /// for selecting the property dependencies for a given <see cref="Type"/>.</param> /// <param name="getConstructorDependencyExpression">A function delegate that returns the registered constructor dependency expression, if any.</param> /// <param name="getPropertyDependencyExpression">A function delegate that returns the registered property dependency expression, if any.</param> public TypeConstructionInfoBuilder( IConstructorSelector constructorSelector, IConstructorDependencySelector constructorDependencySelector, IPropertyDependencySelector propertyDependencySelector, Func <Type, string, Delegate> getConstructorDependencyExpression, Func <Type, string, Delegate> getPropertyDependencyExpression) { this.constructorSelector = constructorSelector; this.constructorDependencySelector = constructorDependencySelector; this.propertyDependencySelector = propertyDependencySelector; this.getConstructorDependencyExpression = getConstructorDependencyExpression; this.getPropertyDependencyExpression = getPropertyDependencyExpression; }
/// <summary> /// Create an activator for the provided type. /// </summary> /// <param name="implementationType">Type to activate.</param> /// <param name="constructorFinder">Constructor finder.</param> /// <param name="constructorSelector">Constructor selector.</param> /// <param name="configuredParameters">Parameters configured explicitly for this instance.</param> /// <param name="configuredProperties">Properties configured explicitly for this instance.</param> public ReflectionActivator( Type implementationType, IConstructorFinder constructorFinder, IConstructorSelector constructorSelector, IEnumerable<Parameter> configuredParameters, IEnumerable<Parameter> configuredProperties) : base(Enforce.ArgumentNotNull(implementationType, "implementationType")) { _implementationType = implementationType; _constructorFinder = Enforce.ArgumentNotNull(constructorFinder, "constructorFinder"); _constructorSelector = Enforce.ArgumentNotNull(constructorSelector, "constructorSelector"); _configuredParameters = Enforce.ArgumentNotNull(configuredParameters, "configuredParameters"); _configuredProperties = Enforce.ArgumentNotNull(configuredProperties, "configuredProperties"); }
public override void PrepareComposition(IComponentComposer composer, IConstructorSelector constructorSelector) { if (null == composer) { throw new ArgumentNullException(nameof(composer)); } if (null == constructorSelector) { throw new ArgumentNullException(nameof(constructorSelector)); } composer.PrepareToComposeUsingConstructor(typeof(TImplementation), constructorSelector); }
public bool IsPreparedToComposeUsingConstructor(Type componentType, IConstructorSelector constructorSelector) { if (null == componentType) { throw new ArgumentNullException(nameof(componentType)); } if (null == constructorSelector) { throw new ArgumentNullException(nameof(constructorSelector)); } var activatorKey = Tuple.Create(componentType, constructorSelector.GetType()); return(this.Activators.ContainsKey(activatorKey)); }
/// <summary> /// Create an activator for the provided type. /// </summary> /// <param name="implementationType">Type to activate.</param> /// <param name="constructorFinder">Constructor finder.</param> /// <param name="constructorSelector">Constructor selector.</param> /// <param name="configuredParameters">Parameters configured explicitly for this instance.</param> /// <param name="configuredProperties">Properties configured explicitly for this instance.</param> public ReflectionActivator( Type implementationType, IConstructorFinder constructorFinder, IConstructorSelector constructorSelector, IEnumerable <Parameter> configuredParameters, IEnumerable <Parameter> configuredProperties) : base(Enforce.ArgumentNotNull(implementationType, "implementationType")) { _implementationType = implementationType; _constructorFinder = Enforce.ArgumentNotNull(constructorFinder, "constructorFinder"); _constructorSelector = Enforce.ArgumentNotNull(constructorSelector, "constructorSelector"); _configuredParameters = Enforce.ArgumentNotNull(configuredParameters, "configuredParameters"); _configuredProperties = Enforce.ArgumentNotNull(configuredProperties, "configuredProperties"); _defaultParameters = _configuredParameters.Concat( new Parameter[] { new AutowiringParameter(), new DefaultValueParameter() }); }
/// <summary> /// Create an activator for the provided type. /// </summary> /// <param name="implementationType">Type to activate.</param> /// <param name="constructorFinder">Constructor finder.</param> /// <param name="constructorSelector">Constructor selector.</param> /// <param name="configuredParameters">Parameters configured explicitly for this instance.</param> /// <param name="configuredProperties">Properties configured explicitly for this instance.</param> public ReflectionActivator( Type implementationType, IConstructorFinder constructorFinder, IConstructorSelector constructorSelector, IEnumerable<Parameter> configuredParameters, IEnumerable<Parameter> configuredProperties) : base(Enforce.ArgumentNotNull(implementationType, "implementationType")) { _implementationType = implementationType; _constructorFinder = Enforce.ArgumentNotNull(constructorFinder, "constructorFinder"); _constructorSelector = Enforce.ArgumentNotNull(constructorSelector, "constructorSelector"); _configuredParameters = Enforce.ArgumentNotNull(configuredParameters, "configuredParameters"); _configuredProperties = Enforce.ArgumentNotNull(configuredProperties, "configuredProperties"); _defaultParameters = _configuredParameters.Concat( new Parameter[] {new AutowiringParameter(), new DefaultValueParameter()}); }
public InstanceActivator(IInstanceActivator activator) { this.activator = activator; var reflectionActivator = activator as ReflectionActivator; if (reflectionActivator != null) { this.constructorFinder = reflectionActivator.ConstructorFinder; this.constructorSelector = reflectionActivator.ConstructorSelector; this._defaultParameters = (IEnumerable <Parameter>)_defaultParametersField.GetValue(reflectionActivator); } else { this.constructorFinder = new DefaultConstructorFinder(); this.constructorSelector = new MostParametersConstructorSelector(); this._defaultParameters = new Parameter[] { new AutowiringParameter(), new DefaultValueParameter() }; } }
public virtual void PrepareToComposeUsingConstructor(Type componentType, IConstructorSelector constructorSelector) { if (null == componentType) { throw new ArgumentNullException(nameof(componentType)); } if (null == constructorSelector) { throw new ArgumentNullException(nameof(constructorSelector)); } var selectedConstructorInfo = this.DefaultConstructorSelector.SelectConstructor(componentType); var activatorKey = Tuple.Create(componentType, constructorSelector.GetType()); this.Activators.Add(activatorKey, (throwOnUnknown) => this.ComposeUsingConstructor(throwOnUnknown, selectedConstructorInfo, constructorSelector)); }
public object Compose(Type componentType, bool throwOnUnknown = true, IComponentComposer composer = null, IConstructorSelector constructorSelector = null) { if (this.disposed) { throw new ObjectDisposedException(this.GetType().Name); } if (null == componentType) { throw new ArgumentNullException(nameof(componentType)); } var component = this.GetRegisteredComponent(componentType); switch (component) { case null when componentType.IsInterface && !throwOnUnknown: return(null); case null when componentType.IsInterface && throwOnUnknown: throw new ArgumentException($"Failed to compose '{componentType.FullName}', no matching component registration was found. Did you forget to register a component?", nameof(componentType)); case null when componentType.IsAbstract && !throwOnUnknown: return(null); case null when componentType.IsAbstract && throwOnUnknown: throw new ArgumentException($"Failed to compose '{componentType.FullName}', no matching component registration was found and the requested type can not be constructed because it is abstract.", nameof(componentType)); case null: return((composer ?? this.DefaultComposer).ComposeUsingConstructor( componentType, throwOnUnknown, constructorSelector ?? this.DefaultConstructorSelector)); } if (component.InstanceType.IsAbstract) { throw new ArgumentException($"Failed to compose '{componentType.FullName}', no matching component registration was found and the requested type can not be constructed because it is abstract. There may be a bug in your { nameof(IContainerComponent) }.", nameof(componentType)); } return(component.Compose(composer ?? this.DefaultComposer, throwOnUnknown, constructorSelector ?? this.DefaultConstructorSelector)); }
public AutoMocker(IConstructorSelector constructorSelector, IProxyFactory proxyFactory) { if (constructorSelector == null) { throw new ArgumentNullException("constructorSelector"); } if (proxyFactory == null) { throw new ArgumentNullException("proxyFactory"); } _proxyFactory = proxyFactory; ConstructorInfo ctor = constructorSelector.Select(typeof(TClass)); _parameters = ctor .GetParameters() .ToDictionary(pi => pi.ParameterType, pi => (object)null); _classUnderTest = new Lazy <TClass>(() => CreateInstance(ctor)); }
/// <summary> /// Create an activator for the provided type. /// </summary> /// <param name="implementationType">Type to activate.</param> /// <param name="constructorFinder">Constructor finder.</param> /// <param name="constructorSelector">Constructor selector.</param> /// <param name="configuredParameters">Parameters configured explicitly for this instance.</param> /// <param name="configuredProperties">Properties configured explicitly for this instance.</param> public ReflectionActivator( Type implementationType, IConstructorFinder constructorFinder, IConstructorSelector constructorSelector, IEnumerable<Parameter> configuredParameters, IEnumerable<Parameter> configuredProperties) : base(implementationType) { if (constructorFinder == null) throw new ArgumentNullException(nameof(constructorFinder)); if (constructorSelector == null) throw new ArgumentNullException(nameof(constructorSelector)); if (configuredParameters == null) throw new ArgumentNullException(nameof(configuredParameters)); if (configuredProperties == null) throw new ArgumentNullException(nameof(configuredProperties)); _implementationType = implementationType; ConstructorFinder = constructorFinder; ConstructorSelector = constructorSelector; _configuredProperties = configuredProperties; _defaultParameters = configuredParameters.Concat( new Parameter[] {new AutowiringParameter(), new DefaultValueParameter()}); }
/// <summary> /// Initializes a new instance of the <see cref="CachedConstructorSelector"/> class. /// </summary> /// <param name="constructorSelector"> /// The <see cref="IConstructorSelector"/> being decorated. /// </param> public CachedConstructorSelector(IConstructorSelector constructorSelector) { this.constructorSelector = constructorSelector; }
public FormatterServicesActivator(Container container, IConstructorSelector constructorSelector) : base(container, constructorSelector) { _constructorSelector = constructorSelector; _container = container; }
public ExpressionActivator(Container container, IConstructorSelector constructorSelector) { _constructorSelector = constructorSelector; _container = container; }
public ProxyActivator(IConstructorSelector constructorSelector) { _constructorSelector = constructorSelector; }
public void Add(IConstructorSelector selector) { _selectors.Add(selector); }
///<summary> /// Creates a new instance of <see cref="ReflectionActivator" />. ///</summary> ///<param name="implementationType">The implementation type.</param> ///<param name="constructorSelector">The constructor selector.</param> ///<param name="argumentCollector">The argument collector.</param> internal ReflectionActivator(Type implementationType, IConstructorSelector constructorSelector, IArgumentCollector argumentCollector) { this._implementationType = implementationType; this._constructorSelector = constructorSelector; this._argumentCollector = argumentCollector; }
object IContainerComponent.Compose(IComponentComposer composer, bool throwOnUnknown, IConstructorSelector constructorSelector) { return(this.Compose(composer, throwOnUnknown, constructorSelector)); }
public virtual void PrepareComposition(IComponentComposer composer, IConstructorSelector constructorSelector) { throw new NotImplementedException(); }
public abstract TComponent Compose(IComponentComposer composer, bool throwOnUnknown, IConstructorSelector constructorSelector);
/// <summary> /// Initializes a new instance of the <see cref="ConstructorExpressionGenerator"/> class. /// </summary> /// <param name="selector"> /// The selector. /// </param> public ConstructorExpressionGenerator(IConstructorSelector selector) { this.selector = selector; }
IConfigurable IConfigurable.SetConstructorSelector(IConstructorSelector constructorSelector) { _constructorSelector = constructorSelector; return this; }
public DefaultTypeActivator(Container container, IConstructorSelector constructorSelector) { _constructorSelector = constructorSelector; _container = container; }