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)); }
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); }
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)); } }
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); }
public virtual object Resolve(Type type, string name) { var key = new TypeRegistrationKey(type, name); return(this.Resovle(key)); }
public bool IsRegistered(TypeRegistrationKey key) { return(this.Registry.IsRegistered(key)); }
public TypeRegistration(TypeRegistrationKey key, Type mapTo, ILifetime lifetime) { this.Key = key; this.MapTo = mapTo; this.Lifetime = lifetime; }
public TypeRegistration(TypeRegistrationKey key, Type mapTo) : this(key, mapTo, null) { }