public static ISimpleContainer RegisterMethod <T>(this ISimpleContainer container, Func <ISimpleContainer, T> method)
        {
            var key          = new TypeRegistrationKey(typeof(T), null);
            var registration = new TypeRegistration(key, typeof(T), new SingletonLifetime(), c => method.Invoke(c));

            return(container.Register(registration));
        }
Exemple #2
0
 public TypeRegistration(TypeRegistrationKey key, Object instance)
 {
     this.Key      = key;
     this.MapTo    = instance.GetType();
     this.Lifetime = new SingletonLifetime();
     this.Lifetime.SetValue(instance);
 }
        public static ISimpleContainer Register(this ISimpleContainer container, Type from, Type mapTo, string name, ILifetime lifetime)
        {
            var key = new TypeRegistrationKey(from, name);

            container.Register(new TypeRegistration(key, mapTo, lifetime));
            return(container);
        }
Exemple #4
0
 public virtual object Resovle(TypeRegistrationKey key)
 {
     if (this.Registry.TryGetValue(key, out var entry))
     {
         var value = entry.Lifetime?.GetValue();
         if (value == null)
         {
             if (entry.Factory != null)
             {
                 value = entry.Factory.Invoke(this);
             }
             else
             {
                 value = this.CreateInstance(entry.MapTo);
             }
         }
         if (entry.Lifetime != null)
         {
             entry.Lifetime.SetValue(value);
         }
         return(value);
     }
     else
     {
         var type = key.Type;
         if (type.IsInterface || type.IsAbstract)
         {
             throw new ContainerException(String.Format(SR.Error_NoInstanceConstructor, type.FullName));
         }
         return(this.CreateInstance(type));
     }
 }
Exemple #5
0
 public TypeRegistration(TypeRegistrationKey key, Type mapTo, ILifetime lifetime, Func <ISimpleContainer, object> factory)
 {
     this.Key      = key;
     this.MapTo    = mapTo;
     this.Lifetime = lifetime;
     this.Factory  = factory;
 }
        public static ISimpleContainer RegisterInstance(this ISimpleContainer container, Type type, string name, object instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            var key = new TypeRegistrationKey(type, name);

            container.Register(new TypeRegistration(key, instance));
            return(container);
        }
Exemple #7
0
        public virtual object Resolve(Type type, string name)
        {
            var key = new TypeRegistrationKey(type, name);

            return(this.Resovle(key));
        }
Exemple #8
0
 public bool IsRegistered(TypeRegistrationKey key)
 {
     return(this.Registry.IsRegistered(key));
 }
Exemple #9
0
 public TypeRegistration(TypeRegistrationKey key, Type mapTo, ILifetime lifetime)
 {
     this.Key      = key;
     this.MapTo    = mapTo;
     this.Lifetime = lifetime;
 }
Exemple #10
0
 public TypeRegistration(TypeRegistrationKey key, Type mapTo)
     : this(key, mapTo, null)
 {
 }