public HttpRequestContext(object dto, EndpointAttributes endpointAttributes, IFactoryProvider factory) { this.Dto = dto; this.EndpointAttributes = endpointAttributes; this.Factory = factory; this.RequestAttributes = new RequestAttributes(HttpContext.Current); }
/// <summary> /// Creates a new instance /// </summary> /// <param name="descriptor"></param> /// <param name="provider"></param> /// <returns></returns> internal static object GetInstance(this ServiceDescriptor descriptor, IFactoryProvider provider) { ConstructorInfo item = descriptor.ImplementationType.GetConstructors().FirstOrDefault(o => o.IsPublic && !o.IsStatic); if (item != null) { List <object> args = new List <object>(); foreach (var attr in item.GetParameters()) { var attr_type = attr.ParameterType; args.Add(provider.Get(attr_type) ?? attr.DefaultValue); } return(args.Count > 0 ? Activator.CreateInstance(descriptor.ImplementationType, args.ToArray()) : Activator.CreateInstance(descriptor.ImplementationType)); } else { if (provider.ThrowNotExist) { throw new FactoryConfigurationException($"Implementation {descriptor.ImplementationType.Name} not have public constructors"); } else { return(default);
/// <summary> /// Adds builder into the provider by the specified factory and builder identifiers. /// </summary> /// <param name="provider">The factory provider.</param> /// <param name="factoryId">The identifier of the factory.</param> /// <param name="builderId">The identifier of the builder.</param> /// <param name="builder">The builder to add.</param> public static void AddBuilder <TFactoryId, TBuilderId>(this IFactoryProvider provider, TFactoryId factoryId, TBuilderId builderId, IBuilder builder) { if (provider == null) { throw new ArgumentNullException(nameof(provider)); } if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (!provider.TryGet(out IFactoryCollection <TFactoryId> collection)) { collection = new FactoryCollection <TFactoryId>(); provider.Add(collection); } if (!collection.TryGet(factoryId, out IFactory <TBuilderId> factory)) { factory = new Factory <TBuilderId>(); collection.Add(factoryId, factory); } factory.Add(builderId, builder); }
public void SetDefaultFactoryProvider(IFactoryProvider factoryProvider) { if (factoryProvider == null) { throw new ArgumentNullException("factoryProvider"); } this.DefaultFactoryProvider = factoryProvider; }
public ServiceInstanceGenerator(IServiceHasFactoryChecker hasServiceFactoryChecker, IFactoryProvider factoryProvider, IFactoryInstanceCreator factoryInstanceCreator, IConstructorParametersGenerator constructorParametersGenerator, IConstructorInvoker constructorInvoker, IParametersValuesExtractor parametersValuesExtractor, IServiceConstructorProvider serviceConstructorProvider) { HasServiceFactoryChecker = hasServiceFactoryChecker; FactoryProvider = factoryProvider; FactoryInstanceCreator = factoryInstanceCreator; ConstructorParametersGenerator = constructorParametersGenerator; ConstructorInvoker = constructorInvoker; ParametersValuesExtractor = parametersValuesExtractor; ServiceConstructorProvider = serviceConstructorProvider; }
/// <summary> /// Adds builder into the provider by the specified factory and builder identifiers. /// </summary> /// <param name="provider">The factory provider.</param> /// <param name="builderId">The identifier of the builder.</param> /// <param name="handler">The builder handler.</param> public static void AddBuilder <TResult, TBuilderId>(this IFactoryProvider provider, TBuilderId builderId, Func <TResult> handler) { if (handler == null) { throw new ArgumentNullException(nameof(handler)); } var builder = new BuilderFunc <TResult>(handler); AddBuilder(provider, typeof(TResult), builderId, builder); }
/// <summary> /// Gets builder from the provider by the specified factory identifier and builder identifier. /// <para> /// The type of the factory identifier represents the identifier of the factory collection. /// </para> /// </summary> /// <param name="provider">The factory provider.</param> /// <param name="factoryId">The identifier of the factory.</param> /// <param name="builderId">The identifier of the builder.</param> public static TBuilder GetBuilder <TBuilder, TFactoryId, TBuilderId>(this IFactoryProvider provider, TFactoryId factoryId, TBuilderId builderId) where TBuilder : IBuilder { if (provider == null) { throw new ArgumentNullException(nameof(provider)); } IFactoryCollection <TFactoryId> collection = provider.Get <TFactoryId>(); var factory = collection.Get <IFactory <TBuilderId> >(factoryId); return(factory.Get <TBuilder>(builderId)); }
private ServiceContext() { this.factories = new ConcurrentDictionary <Type, IServiceConnectionFactory <object> >(); #if (!SILVERLIGHT) Type type = TypeResolver.GetType(Properties.Settings.Default.DefaultServiceFactoryProvider); if (type != null) { this.DefaultFactoryProvider = Activator.CreateInstance(type) as IFactoryProvider; } #endif }
/// <summary> /// Removes builder from the provider by the specified factory and builder identifiers. /// </summary> /// <param name="provider">The factory provider.</param> /// <param name="factoryId">The identifier of the factory.</param> /// <param name="builderId">The identifier of the builder.</param> public static void RemoveBuilder <TFactoryId, TBuilderId>(this IFactoryProvider provider, TFactoryId factoryId, TBuilderId builderId) { if (provider == null) { throw new ArgumentNullException(nameof(provider)); } if (provider.TryGet(out IFactoryCollection <TFactoryId> collection) && collection.TryGet(factoryId, out IFactory <TBuilderId> factory)) { factory.Remove(builderId); } }
/// <summary> /// Initializes an instance of the <see cref="Container"/> class. /// </summary> public Container(ContainerOptions options, IFactoryProvider factoryProvider) { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (factoryProvider == null) { throw new ArgumentNullException(nameof(factoryProvider)); } _options = options; _options.EnsureDefaults(); _factory = factoryProvider.CreateFactory(this); }
/// <summary> /// Method for getting an instance of a type /// </summary> /// <param name="descriptor"></param> /// <param name="provider"></param> /// <returns></returns> internal static object Resolve(this ServiceDescriptor descriptor, IFactoryProvider provider) { switch (descriptor.Lifetime) { case ServiceLifetime.Transient: case ServiceLifetime.TransientFirstLoader: return(descriptor.GetInstance(provider)); case ServiceLifetime.Singleton: case ServiceLifetime.HostedService: case ServiceLifetime.SingletonFirstLoader: return(descriptor.ImplementationInstance ?? (descriptor.ImplementationInstance = descriptor.GetInstance(provider))); default: return(null); } }
private void CreateFactory(ComponentRegistration key, Delegate factoryDelegate, InstanceMode instanceMode) { IFactoryProvider factoryProvider = null; if (instanceMode == InstanceMode.Transient) { factoryProvider = new TransientFactory(factoryDelegate); } if (instanceMode == InstanceMode.Singleton) { factoryProvider = new SingletonFactory(factoryDelegate); } lock (syncLock) { components[key] = factoryProvider; } }
private static List <ConstructorInfo> GetValidConstructors(IContainer container, Type implementationType) { IFactoryProvider factoryProvider = (IFactoryProvider)container; var typeInfo = implementationType.GetTypeInfo(); var constructors = typeInfo.DeclaredConstructors(); var validConstructors = new List <ConstructorInfo>(); foreach (var c in constructors) { if (c.IsStatic) { continue; } var parameters = c.GetParameters(); bool canConstruct = parameters.Length == 0; if (!canConstruct) { bool canConstructParams = true; foreach (var p in c.GetParameters()) { if (!factoryProvider.CanResolve(p.ParameterType)) { canConstructParams = false; break; } } canConstruct = canConstructParams; } if (canConstruct) { validConstructors.Add(c); } } return(validConstructors); }
public T CreateInstance <T>() { lock (syncLock) { IFactoryProvider creator = components. Where(x => x.Key.TypeToCreate == typeof(T)). Select(x => x.Value).SingleOrDefault(); if (creator != null) { T newlyCreatedObject = (T)creator.Create(); SatisfyProperties <T>(newlyCreatedObject); return(newlyCreatedObject); } throw new ContainerConfigurationException(string.Format( "Couldn't create instance of {0} could not find correct " + "IFactoryProvider. This may be down to missing Component registration", typeof(T).FullName)); } }
private void ResolveProperties <T>(T newlyCreatedObject) { foreach (PropertyInfo prop in newlyCreatedObject.GetType().GetProperties() .Where(x => x.GetCustomAttributes(typeof(DependencyConstructorAttribute), false).Count() > 0)) { IFactoryProvider factoryProvider = components.Single(x => x.Key.TypeToCreate == prop.PropertyType || prop.PropertyType.IsAssignableFrom(x.Key.TypeToLookFor)).Value; if (factoryProvider != null) { prop.SetValue(newlyCreatedObject, factoryProvider.Create(), null); } else { throw new Exception(string.Format( "Couldn't find instance of {0} to use for property injection", prop.PropertyType.FullName)); } } }
/// <summary> /// Tries to get builder from the provider by the specified factory identifier and builder identifier. /// <para> /// The type of the factory identifier represents the identifier of the factory collection. /// </para> /// <para> /// Returns true if the builder was found, otherwise false. /// </para> /// </summary> /// <param name="provider">The factory provider.</param> /// <param name="factoryId">The identifier of the factory.</param> /// <param name="builderId">The identifier of the builder.</param> /// <param name="builder">The found builder.</param> public static bool TryGetBuilder <TBuilder, TFactoryId, TBuilderId>(this IFactoryProvider provider, TFactoryId factoryId, TBuilderId builderId, out TBuilder builder) where TBuilder : IBuilder { if (provider == null) { throw new ArgumentNullException(nameof(provider)); } if (provider.TryGet(out IFactoryCollection <TFactoryId> collection) && collection.TryGet(factoryId, out IFactory <TBuilderId> factory)) { if (factory.TryGet(builderId, out IBuilder value) && value is TBuilder result) { builder = result; return(true); } } builder = default; return(false); }
private static List <IInstanceFactory> GetParameterFactories(IContainer container, ConstructorInfo constructor) { IFactoryProvider factoryProvider = container as IFactoryProvider; CheckIsNotNull(nameof(factoryProvider), factoryProvider); var paramFactories = new List <IInstanceFactory>(); foreach (var p in constructor.GetParameters()) { var paramType = p.ParameterType; IInstanceFactory pf; // ReSharper disable once PossibleNullReferenceException if (factoryProvider.TryGetFactory(paramType, out pf)) { paramFactories.Add(pf); } } return(paramFactories); }
/// <summary> /// Initializes a new instance of the <see cref="FileIncludeManager"/> class. /// </summary> /// <param name="context">The context.</param> /// <param name="provider">The provider.</param> public FileIncludeManager(KbContext context, IFactoryProvider provider) : base(context, provider.CreateFileIncludeFactory()) { }
/// <summary> /// Adds builder into the provider by the specified factory and builder identifiers. /// </summary> /// <param name="provider">The factory provider.</param> /// <param name="builderId">The identifier of the builder.</param> /// <param name="builder">The builder to add.</param> public static void AddBuilder <TResult, TBuilderId>(this IFactoryProvider provider, TBuilderId builderId, IBuilder builder) { AddBuilder(provider, typeof(TResult), builderId, builder); }
public ManagersFactory(IFactoryProvider factoryProvider) { _factoryProvider = factoryProvider; }
/// <summary> /// Tries to get typed builder from the provider with the specified identifier. /// <para> /// The type of the builder represents the identifier of the factory collection and factory id. /// </para> /// <para> /// Returns true if the builder was found, otherwise false. /// </para> /// </summary> /// <param name="provider">The factory provider.</param> /// <param name="builderId">The identifier of the builder.</param> /// <param name="builder">The found builder.</param> public static bool TryGetBuilder <TBuilder, TBuilderId>(this IFactoryProvider provider, TBuilderId builderId, out TBuilder builder) where TBuilder : IBuilder { return(TryGetBuilder(provider, typeof(TBuilder), builderId, out builder)); }
protected VisualizerBase(IFactoryProvider factoryProvider) { FactoryProvider = factoryProvider; }
/// <summary> /// Gets typed builder from the provider with the specified identifier. /// <para> /// The type of the builder represents the identifier of the factory collection and factory id. /// </para> /// </summary> /// <param name="provider">The factory provider.</param> /// <param name="builderId">The identifier of the builder.</param> public static TBuilder GetBuilder <TBuilder, TBuilderId>(this IFactoryProvider provider, TBuilderId builderId) where TBuilder : IBuilder { return(GetBuilder <TBuilder, Type, TBuilderId>(provider, typeof(TBuilder), builderId)); }
/// <summary> /// Initializes a new instance of the <see cref="WebProvider"/> class. /// </summary> /// <param name="connectionString">The connection string.</param> public DalWebProvider(IFactoryProvider provider) { _factoryProvider = provider; }
public SvgVisualizer(IFactoryProvider factoryProvider) : base(factoryProvider) { }
/// <summary> /// Initializes a new instance of the <see cref="FileLinkManager"/> class. /// </summary> /// <param name="context">The context.</param> /// <param name="provider">The provider.</param> public FileLinkManager(KbContext context, IFactoryProvider provider) : base(context, provider.CreateFileLinkFactory()) { }
/// <summary> /// Initializes a new instance of the <see cref="UserRightManager"/> class. /// </summary> /// <param name="context">The context.</param> /// <param name="provider">The provider.</param> public UserRightManager(KbContext context, IFactoryProvider provider) : base(context, provider.CreateUserRightFactory()) { }
/// <summary> /// Initializes a new instance of the <see cref="CategoryManager"/> class. /// </summary> public CategoryManager(KbContext context, IFactoryProvider provider) : base(context, provider.CreateCategoryFactory()) { }
public HttpRequestContext(object requestDto, IFactoryProvider factory) : this(requestDto, EndpointAttributes.None, factory) { }
public SessionProvider(IFactoryProvider factoryProvider) { Current = factoryProvider.Current.OpenSession(); }