static Exception TryResolveWithType(this IObjectResolver resolver, Type contractType, ParameterSet parameters, out object instance)
        {
            Requires.NotNull(resolver, "resolver");
            Requires.NotNull(contractType, "contractType");

            ObjectBuilder builder;
            var           state = resolver.Kernel.TryGet(contractType, out builder);

            switch (state)
            {
            case ObjectBuilderState.Normal:
                instance = resolver.Resolve(builder, parameters);
                return(null);

            case ObjectBuilderState.Invalid:
                instance = null;
                return(new ObjectBuilderNotFoundException(GetObjectBuilderNotFoundErrorMessage(contractType)));

            case ObjectBuilderState.Unregistered:
                builder = resolver.Kernel.AutoObjectRegistrar.GetObjectBuilder(contractType);
                if (builder == null)
                {
                    instance = null;
                    return(new ObjectBuilderNotFoundException(GetObjectBuilderNotFoundErrorMessage(contractType)));
                }
                instance = resolver.Resolve(builder, parameters);
                return(null);

            default:
                throw new ImpossibleException();
            }
        }
Ejemplo n.º 2
0
            public T Resolve <T>()
            {
                if (isDisposed)
                {
                    throw new ObjectDisposedException(this.GetType().Name);
                }

                var lifestyle = resolver.Lifestyle(typeof(T));

                switch (lifestyle)
                {
                case MicroResolver.Lifestyle.Transient:
                case MicroResolver.Lifestyle.Singleton:
                    return(resolver.Resolve <T>());

                case MicroResolver.Lifestyle.Scoped:
                    bool isFirstCreated;
                    var  v = (T)provider.GetValueFromScoped(typeof(T), out isFirstCreated);
                    if (isFirstCreated && v is IDisposable)
                    {
                        lock (disposeTarget)
                        {
                            disposeTarget.Add((IDisposable)v);
                        }
                    }
                    return(v);

                default:
                    throw new InvalidOperationException("Invalid Lifestyle:" + lifestyle);
                }
            }
Ejemplo n.º 3
0
        public Task <object> GetValueAsync()
        {
            var injectAttribute = _parameterInfo.GetCustomAttribute <InjectAttribute>();

            if (!injectAttribute.HasName)
            {
                return(Task.FromResult(_objectResolver.Resolve(Type)));
            }
            return(Task.FromResult(_objectResolver.Resolve(Type, injectAttribute.Name)));
        }
Ejemplo n.º 4
0
    public IEnumerable <T> ParseInput <T>(string input)
    {
        var xml = XElement.Parse(input);
        // some more code here
        var parser = _resolver.Resolve <IParser <T> >();

        return(parser.Parse(xml));
    }
        public static T Resolve <T>(this IObjectResolver resolver, IObjectObserver <T> observer)
        {
            var builder = observer.ObjectBuilder;

            if (builder == null)
            {
                throw new ObsoleteObjectBuilderException(GetObsoleteObjectBuilderErrorMessage(observer.ContractType));
            }
            return(resolver.Resolve(builder, null));
        }
Ejemplo n.º 6
0
 public object GetLogger(IObjectResolver objectResolver, Type declaringType)
 {
     // method is used to resolve logger instances azure function implementations with [inject] attribute
     return(GetLogger(declaringType, delegate
     {
         var factory = objectResolver.Resolve <ILoggerFactory>();
         var loggerName = GetLoggerName(declaringType);
         return factory.CreateLogger(loggerName);
     }));
 }
Ejemplo n.º 7
0
        void InjectMethods(object obj, IObjectResolver resolver, IReadOnlyList <IInjectParameter> parameters)
        {
            if (injectTypeInfo.InjectMethods == null)
            {
                return;
            }

            foreach (var method in injectTypeInfo.InjectMethods)
            {
                var parameterInfos  = method.ParameterInfos;
                var parameterValues = CappedArrayPool <object> .Shared8Limit.Rent(parameterInfos.Length);

                try
                {
                    for (var i = 0; i < parameterInfos.Length; i++)
                    {
                        var set           = false;
                        var parameterInfo = parameterInfos[i];
                        if (parameters != null)
                        {
                            foreach (var x in parameters)
                            {
                                if (x.Match(parameterInfo))
                                {
                                    parameterValues[i] = x.Value;
                                    set = true;
                                    break;
                                }
                            }
                        }

                        if (set)
                        {
                            continue;
                        }

                        var parameterType = parameterInfo.ParameterType;
                        try
                        {
                            parameterValues[i] = resolver.Resolve(parameterType);
                        }
                        catch (VContainerException ex)
                        {
                            throw new VContainerException(parameterType, $"Failed to resolve {injectTypeInfo.Type.FullName} : {ex.Message}");
                        }
                    }
                    method.Invoke(obj, parameterValues);
                }
                finally
                {
                    CappedArrayPool <object> .Shared8Limit.Return(parameterValues);
                }
            }
        }
        public Task <object> GetValueAsync()
        {
            var injectAttribute = _parameterInfo.GetCustomAttribute <InjectAttribute>();

            if (injectAttribute.HasName)
            {
                return(Task.FromResult(_objectResolver.Resolve(Type, injectAttribute.Name)));
            }

            if (Type != typeof(ILogger))
            {
                return(Task.FromResult(_objectResolver.Resolve(Type)));
            }

            //logging does not consider naming
            //logging for Azure Functions cannot contain any <T> as the function's class is static and cannot be used to
            //define a concrete type for generic usage.
            var loggerResolver = _objectResolver.Resolve <LoggerResolver>();

            return(Task.FromResult(loggerResolver.GetLogger(_objectResolver, _parameterInfo.Member.DeclaringType)));
        }
Ejemplo n.º 9
0
        void InjectFields(object obj, IObjectResolver resolver)
        {
            if (injectTypeInfo.InjectFields == null)
            {
                return;
            }

            foreach (var x in injectTypeInfo.InjectFields)
            {
                var fieldValue = resolver.Resolve(x.FieldType);
                x.SetValue(obj, fieldValue);
            }
        }
Ejemplo n.º 10
0
        void InjectProperties(object obj, IObjectResolver resolver)
        {
            if (injectTypeInfo.InjectProperties == null)
            {
                return;
            }

            foreach (var x in injectTypeInfo.InjectProperties)
            {
                var propValue = resolver.Resolve(x.PropertyType);
                x.SetValue(obj, propValue);
            }
        }
 static Exception TryResolveWithObjectRegistration <T>(this IObjectResolver resolver, IObjectRegistration <T> registration, ParameterSet parameters, out T instance)
 {
     try
     {
         instance = resolver.Resolve(registration.ObjectBuilder, parameters);
         return(null);
     }
     catch (Exception ex)
     {
         instance = default(T);
         return(ex);
     }
 }
 public static Exception TryResolve <T>(IObjectResolver resolver, ObjectBuilder <T> builder, ParameterSet parameters, out T instance)
 {
     try
     {
         instance = resolver.Resolve(builder, parameters);
     }
     catch (Exception ex)
     {
         instance = default(T);
         return(ex);
     }
     return(null);
 }
 public static Exception TryResolve(IObjectResolver resolver, ObjectBuilder builder, ParameterSet parameters, out object instance)
 {
     try
     {
         instance = resolver.Resolve(builder, parameters);
     }
     catch (Exception ex)
     {
         instance = null;
         return(ex);
     }
     return(null);
 }
Ejemplo n.º 14
0
        public object CreateInstance(IObjectResolver resolver, IReadOnlyList <IInjectParameter> parameters)
        {
            var parameterInfos  = injectTypeInfo.InjectConstructor.ParameterInfos;
            var parameterValues = CappedArrayPool <object> .Shared8Limit.Rent(parameterInfos.Length);

            try
            {
                for (var i = 0; i < parameterInfos.Length; i++)
                {
                    var set           = false;
                    var parameterInfo = parameterInfos[i];
                    if (parameters != null)
                    {
                        foreach (var x in parameters)
                        {
                            if (!x.Match(parameterInfo))
                            {
                                continue;
                            }
                            parameterValues[i] = x.Value;
                            set = true;
                            break;
                        }
                    }

                    if (set)
                    {
                        continue;
                    }

                    try
                    {
                        parameterValues[i] = resolver.Resolve(parameterInfo.ParameterType);
                    }
                    catch (VContainerException ex)
                    {
                        throw new VContainerException(injectTypeInfo.Type, $"Failed to resolve {injectTypeInfo.Type.FullName} : {ex.Message}");
                    }
                }
                var instance = injectTypeInfo.InjectConstructor.Factory(parameterValues);
                Inject(instance, resolver, parameters);
                return(instance);
            }
            finally
            {
                CappedArrayPool <object> .Shared8Limit.Return(parameterValues);
            }
        }
        public static T[] ResolveAll <T>(this IObjectResolver resolver, IObjectCollectionObserver <T> observer)
        {
            var builders = observer.ObjectBuilders;

            if (builders == null)
            {
                throw new ObsoleteObjectBuilderException(GetObsoleteObjectBuilderErrorMessage(observer.ContractType));
            }
            var result = new T[builders.Length];

            for (int i = 0; i < builders.Length; i++)
            {
                result[i] = resolver.Resolve(builders[i], null);
            }
            return(result);
        }
Ejemplo n.º 16
0
            protected override object GetValueFromScoped(Type type, out bool isFirstCreated)
            {
                var v     = objects.GetOrAdd(type, createValue);
                var value = v.Value;

                if (value == null)
                {
                    v.Value        = value = resolver.Resolve(type);
                    isFirstCreated = true;
                }
                else
                {
                    isFirstCreated = false;
                }

                return(value);
            }
        World GetWorld(IObjectResolver resolver)
        {
            if (worldName is null && World.DefaultGameObjectInjectionWorld != null)
            {
                return(World.DefaultGameObjectInjectionWorld);
            }

            var worlds = resolver.Resolve <IEnumerable <World> >();

            foreach (var world in worlds)
            {
                if (world.Name == worldName)
                {
                    return(world);
                }
            }
            throw new VContainerException(ImplementationType, $"World `{worldName}` is not registered");
        }
 public static object ResolveOrParameter(
     this IObjectResolver resolver,
     Type parameterType,
     string parameterName,
     IReadOnlyList <IInjectParameter> parameters)
 {
     if (parameters != null)
     {
         foreach (var parameter in parameters)
         {
             if (parameter.Match(parameterType, parameterName))
             {
                 return(parameter.Value);
             }
         }
     }
     return(resolver.Resolve(parameterType));
 }
        /// <summary>
        /// Retrieve all service associated with the <typeparamref name="TContract"/> and use those components
        /// to build a object instances.
        /// </summary>
        /// <typeparam name="TContract">The type of the contract.</typeparam>
        /// <param name="resolver">The resolver.</param>
        /// <returns></returns>
        public static IList <TContract> ResolveAll <TContract>(this IObjectResolver resolver)
        {
            Requires.NotNull(resolver, "resolver");
            IEnumerable <ObjectBuilder <TContract> > builders;

            if (!resolver.Kernel.TryGet(typeof(TContract), out builders))
            {
                throw new ObjectBuilderNotFoundException(GetObjectBuilderNotFoundErrorMessage(typeof(TContract)));
            }

            var instances = new List <TContract>();

            foreach (var builder in builders)
            {
                var instance = resolver.Resolve(builder, null);
                instances.Add(instance);
            }

            return(instances);
        }
 public static object ResolveOrParameter(
     this IObjectResolver resolver,
     Type parameterType,
     string parameterName,
     IReadOnlyList <IInjectParameter> parameters)
 {
     if (parameters != null)
     {
         // ReSharper disable once ForCanBeConvertedToForeach
         for (var i = 0; i < parameters.Count; i++)
         {
             var parameter = parameters[i];
             if (parameter.Match(parameterType, parameterName))
             {
                 return(parameter.Value);
             }
         }
     }
     return(resolver.Resolve(parameterType));
 }
Ejemplo n.º 21
0
        public object SpawnInstance(IObjectResolver resolver)
        {
            var genericType     = typeof(List <>).MakeGenericType(elementType);
            var parameterValues = CappedArrayPool <object> .Shared8Limit.Rent(1);

            parameterValues[0] = registrations.Count;
            var list = (IList)Activator.CreateInstance(genericType, parameterValues);

            try
            {
                foreach (var registration in registrations)
                {
                    list.Add(resolver.Resolve(registration));
                }
            }
            finally
            {
                CappedArrayPool <object> .Shared8Limit.Return(parameterValues);
            }
            return(list);
        }
 public static object Resolve(this IObjectResolver resolver, IObjectRegistration registration, IList <NamedParameter> overridenParameters)
 {
     return(resolver.Resolve(registration.ObjectBuilder, new NamedParameterSet(overridenParameters)));
 }
 public static T Resolve <T>(this IObjectResolver resolver, IObjectRegistration <T> registration, params NamedParameter[] overridenParameters)
 {
     return(resolver.Resolve(registration.ObjectBuilder, new NamedParameterSet(overridenParameters)));
 }
Ejemplo n.º 24
0
 public void Resolve(IObjectResolver aHandler)
 {
     aHandler.Resolve(this);
 }
Ejemplo n.º 25
0
 ThreadLocal <object> CreateValue(Type key)
 {
     return(new ThreadLocal <object>(() => resolver.Resolve(key)));
 }
Ejemplo n.º 26
0
 Lazy <object> CreateValue(Type key)
 {
     return(new Lazy <object>(() => resolver.Resolve(key)));
 }
Ejemplo n.º 27
0
 public object GetService(Type serviceType)
 {
     return(resolver.Resolve(serviceType));
 }
Ejemplo n.º 28
0
 public Task <object> GetValueAsync()
 {
     return(Task.FromResult(_objectResolver.Resolve(Type)));
 }
Ejemplo n.º 29
0
        public void Dispatch()
        {
            PlayerLoopHelper.Initialize();

            EntryPointExceptionHandler exceptionHandler = null;

            try
            {
                exceptionHandler = container.Resolve <EntryPointExceptionHandler>();
            }
            catch (VContainerException ex) when(ex.InvalidType == typeof(EntryPointExceptionHandler))
            {
            }

            var initializables = container.Resolve <IReadOnlyList <IInitializable> >();

            if (initializables.Count > 0)
            {
                var loopItem = new InitializationLoopItem(initializables, exceptionHandler);
                disposable.Add(loopItem);
                PlayerLoopHelper.Dispatch(PlayerLoopTiming.Initialization, loopItem);
            }

            var postInitializables = container.Resolve <IReadOnlyList <IPostInitializable> >();

            if (postInitializables.Count > 0)
            {
                var loopItem = new PostInitializationLoopItem(postInitializables, exceptionHandler);
                disposable.Add(loopItem);
                PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostInitialization, loopItem);
            }

            var startables = container.Resolve <IReadOnlyList <IStartable> >();

            if (startables.Count > 0)
            {
                var loopItem = new StartableLoopItem(startables, exceptionHandler);
                disposable.Add(loopItem);
                PlayerLoopHelper.Dispatch(PlayerLoopTiming.Startup, loopItem);
            }

            var postStartables = container.Resolve <IReadOnlyList <IPostStartable> >();

            if (postStartables.Count > 0)
            {
                var loopItem = new PostStartableLoopItem(postStartables, exceptionHandler);
                disposable.Add(loopItem);
                PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostStartup, loopItem);
            }

            var fixedTickables = container.Resolve <IReadOnlyList <IFixedTickable> >();

            if (fixedTickables.Count > 0)
            {
                var loopItem = new FixedTickableLoopItem(fixedTickables, exceptionHandler);
                disposable.Add(loopItem);
                PlayerLoopHelper.Dispatch(PlayerLoopTiming.FixedUpdate, loopItem);
            }

            var postFixedTickables = container.Resolve <IReadOnlyList <IPostFixedTickable> >();

            if (postFixedTickables.Count > 0)
            {
                var loopItem = new PostFixedTickableLoopItem(postFixedTickables, exceptionHandler);
                disposable.Add(loopItem);
                PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostFixedUpdate, loopItem);
            }

            var tickables = container.Resolve <IReadOnlyList <ITickable> >();

            if (tickables.Count > 0)
            {
                var loopItem = new TickableLoopItem(tickables, exceptionHandler);
                disposable.Add(loopItem);
                PlayerLoopHelper.Dispatch(PlayerLoopTiming.Update, loopItem);
            }

            var postTickables = container.Resolve <IReadOnlyList <IPostTickable> >();

            if (postTickables.Count > 0)
            {
                var loopItem = new PostTickableLoopItem(postTickables, exceptionHandler);
                disposable.Add(loopItem);
                PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostUpdate, loopItem);
            }

            var lateTickables = container.Resolve <IReadOnlyList <ILateTickable> >();

            if (lateTickables.Count > 0)
            {
                var loopItem = new LateTickableLoopItem(lateTickables, exceptionHandler);
                disposable.Add(loopItem);
                PlayerLoopHelper.Dispatch(PlayerLoopTiming.LateUpdate, loopItem);
            }

            var postLateTickables = container.Resolve <IReadOnlyList <IPostLateTickable> >();

            if (postLateTickables.Count > 0)
            {
                var loopItem = new PostLateTickableLoopItem(postLateTickables, exceptionHandler);
                disposable.Add(loopItem);
                PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostLateUpdate, loopItem);
            }

#if VCONTAINER_UNITASK_INTEGRATION
            var asyncStartables = container.Resolve <IReadOnlyList <IAsyncStartable> >();
            if (asyncStartables.Count > 0)
            {
                var loopItem = new AsyncStartableLoopItem(asyncStartables, exceptionHandler);
                disposable.Add(loopItem);
                PlayerLoopHelper.Dispatch(PlayerLoopTiming.Startup, loopItem);
            }
#endif

#if VCONTAINER_ECS_INTEGRATION
            container.Resolve <IEnumerable <ComponentSystemBase> >();

            var worldHelpers = container.Resolve <IEnumerable <WorldConfigurationHelper> >();
            foreach (var x in worldHelpers)
            {
                x.SortSystems();
            }
#endif
        }
 public static T Resolve <T>(this IObjectResolver resolver, IObjectRegistration <T> registration, IEnumerable <NamedParameter> overridenParameters)
 {
     return(resolver.Resolve(registration.ObjectBuilder, new NamedParameterSet(overridenParameters.ToList())));
 }
Ejemplo n.º 31
0
 public static T GetInstance <T>()
 {
     return(resolver.Resolve <T>());
 }
Ejemplo n.º 32
0
 public override void InitializeForView(IObjectResolver resolver)
 {
     Recipe = resolver.Resolve<IRecipeProvider>().Get(RecipeId);
 }