Ejemplo n.º 1
0
        /// <summary>
        /// Register and calibrated an Implementation to a Contract
        /// </summary>
        /// <param name="Contract"></param>
        /// <param name="Implementation"></param>
        /// <param name="Name"></param>
        /// <param name="LifeTime"></param>
        /// <returns></returns>
        public static IoCRegistry Register(Type Contract, Type Implementation, string Name, IoCLifeTime LifeTime)
        {
            IoCRegistry result = new IoCRegistry
            {
                Name           = Name,
                Contract       = Contract,
                Implementation = Implementation,
                LifeTime       = LifeTime
            };

            implementations.Add(result);
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Resolve the contract using its constructor
        /// </summary>
        /// <param name="contract"></param>
        /// <returns></returns>
        public static object Resolve(IoCRegistry ResolutionType)
        {
            // if single instance
            if (ResolutionType.LifeTime == IoCLifeTime.Singleton)
            {
                object instance = instances.FirstOrDefault(x => x.GetType() == ResolutionType.Implementation);
                if (instance != null)
                {
                    return(instance);
                }
            }

            Type implementation = ResolutionType.Implementation;
            // Get the constructor with most parameters to make sure we get utilize the interfaces full potential.
            ConstructorInfo constructor = implementation.GetConstructors().OrderByDescending(x => x.GetParameters()).First();

            ParameterInfo[] constructorParameters = constructor.GetParameters();
            if (constructorParameters.Length == 0)
            {
                return(Activator.CreateInstance(implementation));
            }

            List <object> parameters = new List <object>(constructorParameters.Length);

            foreach (ParameterInfo parameterInfo in constructorParameters)
            {
                parameters.Add(Resolve(parameterInfo.ParameterType));
            }

            object inst = constructor.Invoke(parameters.ToArray());

            // only add singleton instances, so we dont get an overflow of potentially millions of multi-instance over an application runtime.
            if (ResolutionType.LifeTime == IoCLifeTime.Singleton)
            {
                instances.Add(inst);
            }

            return(inst);
        }
Ejemplo n.º 3
0
 public static IoCRegistry AsMultiInstance(this IoCRegistry self)
 {
     self.LifeTime = IoCLifeTime.MultiInstance;
     return(self);
 }
Ejemplo n.º 4
0
 public static IoCRegistry AsSingleton(this IoCRegistry self)
 {
     self.LifeTime = IoCLifeTime.Singleton;
     return(self);
 }
Ejemplo n.º 5
0
        public static object Resolve(Type contract)
        {
            IoCRegistry ResolutionType = implementations.FirstOrDefault(x => x.Contract == contract);

            return(Resolve(ResolutionType));
        }