public static void Add <T>(this IPlasmaContainer container, Type type, Lazy <T> instanceFactory) { if (container == null) { throw new ArgumentNullException("container"); } container.Add(type, CreateLazy(instanceFactory)); }
public static void Add(this IPlasmaContainer container, Type type, object instance) { if (container == null) { throw new ArgumentNullException("container"); } container.Add(type, CreateLazy(instance)); }
/// <summary> /// Register service with default factory /// </summary> /// <typeparam name="T">Registering type, e.g. IMyService</typeparam> /// <typeparam name="TImpl">Service implementation, e.g. MyServiceImpl</typeparam> /// <param name="container"></param> public static void Add <T, TImpl>(this IPlasmaContainer container) { if (container == null) { throw new ArgumentNullException("container"); } container.Add(typeof(T), typeof(TImpl)); }
/// <summary> /// Register service lazy /// </summary> /// <typeparam name="T">Registering type, e.g. IMyService</typeparam> /// <param name="container"></param> /// <param name="instanceFactory">Service lazy instance</param> public static void Add <T>(this IPlasmaContainer container, Lazy <T> instanceFactory) { if (container == null) { throw new ArgumentNullException("container"); } // TODO Eliminate lazy double wrapping: Lazy<T>(Lazy<object>(Lazy<T>())) container.Add(typeof(T), CreateLazy(instanceFactory)); }
/// <summary> /// Try get service of registered type or register by factory /// </summary> public static T Get <T>(this IPlasmaContainer container, Func <T> factory) { if (container == null) { throw new ArgumentNullException("container"); } if (factory == null) { throw new ArgumentNullException("factory"); } var result = container.TryGet <T>(); if (ReferenceEquals(null, result)) { result = factory(); container.Add(result); } return(result); }
public void Should_allow_register_func_factory_by_iface_and_lambda() { _sut.Add <IMyService>(() => new MyService()); Verify(); }