//--- Constructors --- /// <summary> /// Create a new XDoc configurator. /// </summary> /// <param name="config">Configuration fragment.</param> /// <param name="defaultScope">Default registration scope.</param> public XDocAutofacContainerConfigurator(XDoc config, DreamContainerScope defaultScope) { if(config == null) { throw new ArgumentNullException("config"); } _defaultScope = defaultScope; _config = config; }
private void IsRegisteredInScopeWithName(Type type, DreamContainerScope scope, string name) { IComponentRegistration registration; Assert.IsTrue(_serviceContainer.ComponentRegistry.TryGetRegistration(new NamedService(name, type), out registration), string.Format("no registration found for type '{0}' with name '{1}'", typeof(Foo), "fooz")); Assert.AreEqual(InstanceOwnership.OwnedByLifetimeScope, registration.Ownership); object instance; switch(scope) { case DreamContainerScope.Factory: Assert.AreEqual(InstanceSharing.None, registration.Sharing); break; case DreamContainerScope.Host: Assert.AreEqual(InstanceSharing.Shared, registration.Sharing); Assert.IsTrue(_hostContainer.TryResolve(name, type, out instance), "unable to resolve in host"); Assert.IsTrue(_serviceContainer.TryResolve(name, type, out instance), "unable to resolve in service"); Assert.IsTrue(_requestContainer.TryResolve(name, type, out instance), "unable to resolve in request"); break; case DreamContainerScope.Service: Assert.AreEqual(InstanceSharing.Shared, registration.Sharing); try { Assert.IsFalse(_hostContainer.TryResolve(name, type, out instance), "able to resolve in host"); } catch(DependencyResolutionException) {} Assert.IsTrue(_serviceContainer.TryResolve(name, type, out instance), "unable to resolve in service"); Assert.IsTrue(_requestContainer.TryResolve(name, type, out instance), "unable to resolve in request"); break; case DreamContainerScope.Request: Assert.AreEqual(InstanceSharing.Shared, registration.Sharing); try { Assert.IsFalse(_hostContainer.TryResolve(name, type, out instance), "able to resolve in host"); } catch(DependencyResolutionException) {} try { Assert.IsFalse(_serviceContainer.TryResolve(name, type, out instance), "able to resolve in service"); } catch(DependencyResolutionException) {} Assert.IsTrue(_requestContainer.TryResolve(name, type, out instance), "unable to resolve in request"); break; } }
private void IsRegisteredInScope(Type type, DreamContainerScope scope) { IComponentRegistration registration = GetRegistration(type); if(scope == DreamContainerScope.Factory) { var componentRegistration = registration as Registration; Assert.IsNotNull(componentRegistration); Assert.AreEqual(typeof(FactoryScope), componentRegistration.Scope.GetType()); } else { var taggedRegistration = registration as TaggedRegistration<DreamContainerScope>; Assert.IsNotNull(taggedRegistration); Assert.AreEqual(scope, taggedRegistration.Tag); } }
/// <summary> /// Set the registered item's container resolution scope. /// </summary> /// <param name="registrar">Registrar instance.</param> /// <param name="scope">Container Resolution scope.</param> /// <returns>The modified registrar instance.</returns> public static IReflectiveRegistrar InScope(this IReflectiveRegistrar registrar, DreamContainerScope scope) { return scope == DreamContainerScope.Factory ? registrar.WithScope(InstanceScope.Factory) : registrar.InContext(scope); }