Example #1
0
        /// <summary>
        /// Creates a new instance of the specified type asyncronously.
        /// </summary>
        /// <typeparam name="TInterface">The type of the interface.</typeparam>
        /// <returns></returns>
        public async Task <TInterface> CreateInstanceAsync <TInterface>() where TInterface : class
        {
            return(await Task.Run(() =>
            {
                BindingStructure binding = _objects.FirstOrDefault(x => x.BindingType == typeof(TInterface));
                if (binding.ConcreteType == null || binding.BindingType == null)
                {
                    return default(TInterface);
                }

                return Activator.CreateInstance(binding.ConcreteType, binding.Parameters) as TInterface;
            }));
        }
Example #2
0
        /// <summary>
        /// Gets the instance asynchronously. If binding name is specified, the service will retrieve the specific instance.
        /// </summary>
        /// <typeparam name="TInterface">The type of the interface.</typeparam>
        /// <param name="bindingName">Name of the binding.</param>
        /// <returns></returns>
        public async Task <TInterface> GetInstanceAsync <TInterface>(string bindingName = null) where TInterface : class
        {
            return(await Task.Run(() =>
            {
                BindingStructure binding = bindingName == null ?
                                           _objects.FirstOrDefault(x => x.BindingType == typeof(TInterface)) :
                                           _objects.FirstOrDefault(x => x.BindingType == typeof(TInterface) && x.BindingName.Equals(bindingName, StringComparison.OrdinalIgnoreCase));

                if (!(binding.BindingObject is TInterface rval))
                {
                    return default(TInterface);
                }

                return rval;
            }));
        }