public void RegisterAndResolveInstanceByInterface() { var serviceLocator = new ServiceLocator(); var a = new A(); serviceLocator.RegisterInstance(typeof(IA), a); var result = serviceLocator.GetService(typeof (IA)); Assert.AreEqual(a, result); }
public void RegisterAndResolveTypeByClass() { var serviceLocator = new ServiceLocator(); serviceLocator.RegisterSingleton(typeof(A), typeof(A)); var result = serviceLocator.GetService(typeof(A)); Assert.NotNull(result); Assert.IsInstanceOf<A>(result); }
public void AvoidCircularReferences() { var serviceLocator = new ServiceLocator(); serviceLocator.RegisterSingleton(typeof(IA), typeof(A)); serviceLocator.RegisterSingleton(typeof(IB), typeof(B)); serviceLocator.RegisterSingleton(typeof(IC), typeof(C)); var result = (IC) serviceLocator.GetService(typeof(IC)); Assert.NotNull(result); Assert.IsInstanceOf<C>(result); Assert.AreEqual(3, result.UsedConstructor); }
public void ResolveAdHocTransientInstantIfNotRegistered() { var serviceLocator = new ServiceLocator(); var result = serviceLocator.GetService(typeof(A)); var result2 = serviceLocator.GetService(typeof(A)); Assert.NotNull(result); Assert.IsInstanceOf<A>(result); Assert.NotNull(result2); Assert.IsInstanceOf<A>(result2); Assert.AreNotEqual(result, result2); }
public void UseProperConstructorWithOneParameter() { var serviceLocator = new ServiceLocator(); serviceLocator.RegisterSingleton(typeof(IA), typeof(A)); serviceLocator.RegisterSingleton(typeof(IC), typeof(C)); var result = (IC)serviceLocator.GetService(typeof(IC)); Assert.NotNull(result); Assert.IsInstanceOf<C>(result); Assert.AreEqual(2, result.UsedConstructor); }
public void Transient() { var serviceLocator = new ServiceLocator(); serviceLocator.RegisterTransient(typeof(IA), typeof(A)); var result = serviceLocator.GetService(typeof(IA)); var result2 = serviceLocator.GetService(typeof(IA)); Assert.NotNull(result); Assert.IsInstanceOf<A>(result); Assert.NotNull(result2); Assert.IsInstanceOf<A>(result2); Assert.AreNotEqual(result, result2); }
public void SingletonInstance() { var serviceLocator = new ServiceLocator(); serviceLocator.RegisterSingleton(typeof(IA), typeof(A)); var result = serviceLocator.GetService(typeof(IA)); var result2 = serviceLocator.GetService(typeof(IA)); Assert.NotNull(result); Assert.IsInstanceOf<A>(result); Assert.AreEqual(result, result2); }
public RecursiveResolver(ServiceLocator locator, Type implementedType, Type implementingType) { _locator = locator; _implementedType = implementedType; _implementingType = implementingType; }