コード例 #1
0
 public HttpRequestContext(object dto, EndpointAttributes endpointAttributes, IFactoryProvider factory)
 {
     this.Dto = dto;
     this.EndpointAttributes = endpointAttributes;
     this.Factory            = factory;
     this.RequestAttributes  = new RequestAttributes(HttpContext.Current);
 }
コード例 #2
0
        /// <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);
コード例 #3
0
        /// <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);
        }
コード例 #4
0
        public void SetDefaultFactoryProvider(IFactoryProvider factoryProvider)
        {
            if (factoryProvider == null)
            {
                throw new ArgumentNullException("factoryProvider");
            }

            this.DefaultFactoryProvider = factoryProvider;
        }
コード例 #5
0
 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;
 }
コード例 #6
0
        /// <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);
        }
コード例 #7
0
        /// <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));
        }
コード例 #8
0
        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
        }
コード例 #9
0
        /// <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);
            }
        }
コード例 #10
0
ファイル: Container.cs プロジェクト: zhech2/towerInject
        /// <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);
        }
コード例 #11
0
        /// <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);
            }
        }
コード例 #12
0
        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;
            }
        }
コード例 #13
0
        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);
        }
コード例 #14
0
        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));
            }
        }
コード例 #15
0
ファイル: Resolver.cs プロジェクト: dannybarrus/recon
        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));
                }
            }
        }
コード例 #16
0
        /// <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);
        }
コード例 #17
0
        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);
        }
コード例 #18
0
 /// <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())
 {
 }
コード例 #19
0
 /// <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);
 }
コード例 #20
0
ファイル: ManagersFactory.cs プロジェクト: minskowl/MY
 public ManagersFactory(IFactoryProvider factoryProvider)
 {
     _factoryProvider = factoryProvider;
 }
コード例 #21
0
 /// <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));
 }
コード例 #22
0
 protected VisualizerBase(IFactoryProvider factoryProvider)
 {
     FactoryProvider = factoryProvider;
 }
コード例 #23
0
 /// <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));
 }
コード例 #24
0
 /// <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;
 }
コード例 #25
0
 public SvgVisualizer(IFactoryProvider factoryProvider)
     : base(factoryProvider)
 {
 }
コード例 #26
0
ファイル: SvgVisualizer.cs プロジェクト: pretyk/VisualLogs
 public SvgVisualizer(IFactoryProvider factoryProvider)
     : base(factoryProvider)
 {
 }
コード例 #27
0
ファイル: FileLinksManager.cs プロジェクト: minskowl/MY
 /// <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())
 {
 }
コード例 #28
0
ファイル: UserRightsManager.cs プロジェクト: minskowl/MY
 /// <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())
 {
 }
コード例 #29
0
ファイル: VisualizerBase.cs プロジェクト: pretyk/VisualLogs
 protected VisualizerBase(IFactoryProvider factoryProvider)
 {
     FactoryProvider = factoryProvider;
 }
コード例 #30
0
ファイル: CategoriesManager.cs プロジェクト: minskowl/MY
 /// <summary>
 /// Initializes a new instance of the <see cref="CategoryManager"/> class.
 /// </summary>
 public CategoryManager(KbContext context, IFactoryProvider provider)
     : base(context, provider.CreateCategoryFactory())
 {
 }
コード例 #31
0
 public HttpRequestContext(object requestDto, IFactoryProvider factory)
     : this(requestDto, EndpointAttributes.None, factory)
 {
 }
コード例 #32
0
ファイル: Session.cs プロジェクト: jeroenheijmans/bieb
 public SessionProvider(IFactoryProvider factoryProvider)
 {
     Current = factoryProvider.Current.OpenSession();
 }