//--- 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;
 }
Ejemplo n.º 2
0
        //--- 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;
        }
Ejemplo n.º 3
0
        //--- Methods ---

        /// <summary>
        /// Load the configuration into a builder
        /// </summary>
        /// <param name="builder">Container builder to public.</param>
        protected override void Load(ContainerBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            foreach (var component in _config["component"])
            {
                var componentDebug         = new DebugStringBuilder(_log.IsDebugEnabled);
                var implementationTypename = component["@implementation"].AsText;
                var type = LoadType(component["@type"]);
                IRegistrationBuilder <object, ConcreteReflectionActivatorData, SingleRegistrationStyle> registrar;
                var name = component["@name"].AsText;
                if (string.IsNullOrEmpty(implementationTypename))
                {
                    componentDebug.AppendFormat("registering concrete type '{0}'", type.FullName);
                    registrar = builder.RegisterType(type);
                }
                else
                {
                    var concreteType = LoadType(implementationTypename);
                    componentDebug.AppendFormat("registering concrete type '{0}' as '{1}'", concreteType.FullName, type.FullName);
                    registrar = builder.RegisterType(concreteType).As(new TypedService(type));
                }
                if (!string.IsNullOrEmpty(name))
                {
                    registrar.Named(name, type);
                    componentDebug.AppendFormat("named '{0}'", name);
                }
                registrar.WithParameters(
                    (from parameter in component["parameters/parameter"]
                     let parameterName = parameter["@name"].AsText
                                         let parameterValue = parameter["@value"].AsText
                                                              select new ResolvedParameter(
                         (p, c) => p.Name == parameterName,
                         (p, c) => SysUtil.ChangeType(parameterValue, p.ParameterType))
                    ).Cast <Parameter>());

                // set scope
                DreamContainerScope scope = _defaultScope;
                var strScope = component["@scope"].AsText;
                if (strScope != null)
                {
                    scope = SysUtil.ParseEnum <DreamContainerScope>(strScope);
                }
                componentDebug.AppendFormat(" in '{0}' scope", scope);
                registrar.InScope(scope);
                if (_log.IsDebugEnabled)
                {
                    _log.Debug(componentDebug.ToString());
                }
            }
        }
Ejemplo n.º 4
0
        //--- Methods ---

        /// <summary>
        /// Load the configuration into a builder
        /// </summary>
        /// <param name="builder">Container builder to public.</param>
        protected override void Load(ContainerBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            foreach (var component in _config["component"])
            {
                var componentDebug         = new DebugStringBuilder(_log.IsDebugEnabled);
                var implementationTypename = component["@implementation"].AsText;
                var type = LoadType(component["@type"]);
                IReflectiveRegistrar registrar;
                if (string.IsNullOrEmpty(implementationTypename))
                {
                    componentDebug.AppendFormat("registering concrete type '{0}'", type.FullName);
                    registrar = builder.Register(type);
                }
                else
                {
                    var concreteType = LoadType(implementationTypename);
                    registrar = builder.Register(concreteType);
                    registrar.As(new TypedService(type));
                    componentDebug.AppendFormat("registering concrete type '{0}' as '{1}'", concreteType.FullName, type.FullName);
                }
                registrar.WithArguments(GetParameters(component));

                // set scope
                DreamContainerScope scope = _defaultScope;
                var strScope = component["@scope"].AsText;
                if (strScope != null)
                {
                    scope = SysUtil.ParseEnum <DreamContainerScope>(strScope);
                }
                componentDebug.AppendFormat(" in '{0}' scope", scope);
                registrar.InScope(scope);

                // set up name
                var name = component["@name"].AsText;
                if (!string.IsNullOrEmpty(name))
                {
                    componentDebug.AppendFormat(" named '{0}'", name);
                    registrar.Named(name);
                }
                if (_log.IsDebugEnabled)
                {
                    _log.Debug(componentDebug.ToString());
                }
            }
        }
        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);
            }
        }
Ejemplo n.º 6
0
        private void IsRegisteredInScopeWithName(Type type, DreamContainerScope scope, string name)
        {
            IComponentRegistration registration;

            Assert.IsTrue(_serviceContainer.ComponentRegistry.TryGetRegistration(new KeyedService(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.TryResolveNamed(name, type, out instance), "unable to resolve in host");
                Assert.IsTrue(_serviceContainer.TryResolveNamed(name, type, out instance), "unable to resolve in service");
                Assert.IsTrue(_requestContainer.TryResolveNamed(name, type, out instance), "unable to resolve in request");
                break;

            case DreamContainerScope.Service:
                Assert.AreEqual(InstanceSharing.Shared, registration.Sharing);
                try {
                    Assert.IsFalse(_hostContainer.TryResolveNamed(name, type, out instance), "able to resolve in host");
                } catch (DependencyResolutionException) {}
                Assert.IsTrue(_serviceContainer.TryResolveNamed(name, type, out instance), "unable to resolve in service");
                Assert.IsTrue(_requestContainer.TryResolveNamed(name, type, out instance), "unable to resolve in request");
                break;

            case DreamContainerScope.Request:
                Assert.AreEqual(InstanceSharing.Shared, registration.Sharing);
                try {
                    Assert.IsFalse(_hostContainer.TryResolveNamed(name, type, out instance), "able to resolve in host");
                } catch (DependencyResolutionException) {}
                try {
                    Assert.IsFalse(_serviceContainer.TryResolveNamed(name, type, out instance), "able to resolve in service");
                } catch (DependencyResolutionException) {}
                Assert.IsTrue(_requestContainer.TryResolveNamed(name, type, out instance), "unable to resolve in request");
                break;
            }
        }
 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);
     }
 }
Ejemplo n.º 9
0
        //--- Extension Methods ---

        /// <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 IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> InScope <TLimit, TActivatorData, TRegistrationStyle>(this IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> registrar, DreamContainerScope scope)
        {
            return(scope == DreamContainerScope.Factory ? registrar.InstancePerDependency() : registrar.InstancePerMatchingLifetimeScope(scope));
        }
Ejemplo n.º 10
0
 /// <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));
 }
Ejemplo n.º 11
0
 /// <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);
 }