Ejemplo n.º 1
0
        /// <summary>
        /// Override of <see cref="IBuilderStrategy.BuildUp"/>. Creates the requested object using the custom factory associated to type <paramref name="t"/>.
        /// </summary>
        /// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param>
        /// <param name="t">The type to build.</param>
        /// <param name="existing">The existing object. Should be <see langword="null"/>.</param>
        /// <param name="id">The ID of the object to be created.</param>
        /// <returns>The created object.</returns>
        /// <exception cref="InvalidOperationException"> when the requested type does not have the
        /// required <see cref="CustomFactoryAttribute">CustomFactory</see> attribute.</exception>
        /// <exception cref="System.Configuration.ConfigurationErrorsException"> when the configuration for the requested ID is not present or is
        /// invalid in the configuration source.</exception>
        public override object BuildUp(IBuilderContext context, Type t, object existing, string id)
        {
            string newId = id;

            IConfigurationSource         configurationSource = GetConfigurationSource(context);
            ConfigurationReflectionCache reflectionCache     = GetReflectionCache(context);

            ICustomFactory factory = GetCustomFactory(t, reflectionCache);

            if (factory != null)
            {
                existing = factory.CreateObject(context, newId, configurationSource, reflectionCache);
            }
            else
            {
                throw new InvalidOperationException(
                          string.Format(
                              Resources.Culture,
                              Resources.ExceptionCustomFactoryAttributeNotFound,
                              t.FullName,
                              id));
            }

            return(base.BuildUp(context, t, existing, newId));
        }
Ejemplo n.º 2
0
        private static void ProcessCustomFactory(
            ICustomFactory factory,
            object target,
            Dictionary <string, object> values,
            bool replace,
            string identifier,
            ref bool loaded)
        {
            foreach (var component in factory.Create(target, values))
            {
                loaded = true;
                if (component == null)
                {
                    continue;
                }

                Control.LogDebug(DType.CCLoading, $"Created {component} for {identifier}");

                if (Database.SetCustomWithIdentifier(identifier, component, replace))
                {
                    if (component is IAfterLoad load)
                    {
                        Control.LogDebug(DType.CCLoading, $"IAfterLoad: {identifier}");
                        load.OnLoaded(values);
                    }

                    if (component is IAdjustDescription ed)
                    {
                        ed.AdjustDescription();
                    }
                }
            }
        }
Ejemplo n.º 3
0
 public MockFactoryStrategy(ICustomFactory factory,
                            IConfigurationSource configurationSource,
                            ConfigurationReflectionCache reflectionCache)
 {
     this.factory             = factory;
     this.configurationSource = configurationSource;
     this.reflectionCache     = reflectionCache;
 }
Ejemplo n.º 4
0
        public void CanGetNullCustomFactoryForNonAnnotatedTypeTheFirstTime()
        {
            MockImplementor.constructorCalls = 0;
            ICustomFactory factory = cache.GetCustomFactory(typeof(TypeWithNoCustomFactoryAttribute));

            Assert.IsNull(factory);
            Assert.AreEqual(0, MockImplementor.constructorCalls);
        }
        public ViaFileInfo(ICustomFactory factory)
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            _factory = factory;
        }
Ejemplo n.º 6
0
        public void CanGetCustomFactoryForAnnotatedTypeTheFirstTime()
        {
            MockImplementor.constructorCalls = 0;
            ICustomFactory factory = cache.GetCustomFactory(typeof(TypeWithCustomFactoryAttribute));

            Assert.IsNotNull(factory);
            Assert.AreSame(typeof(MockImplementor), factory.GetType());
            Assert.AreEqual(1, MockImplementor.constructorCalls);
        }
        public ViaFile(IFile file, ICustomFactory factory)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            _file    = file;
            _factory = factory;
        }
Ejemplo n.º 8
0
        public override void PreBuildUp(IBuilderContext context)
        {
            base.PreBuildUp(context);
            IConfigurationSource         configurationSource = GetConfigurationSource(context);
            ConfigurationReflectionCache reflectionCache     = GetReflectionCache(context);
            NamedTypeBuildKey            key = (NamedTypeBuildKey)context.BuildKey;
            string         id      = key.Name;
            Type           t       = key.Type;
            ICustomFactory factory = GetCustomFactory(t, reflectionCache);

            if (factory != null)
            {
                context.Existing = factory.CreateObject(context, id, configurationSource, reflectionCache);
            }
            else
            {
                throw new InvalidOperationException(
                          string.Format(
                              Resources.Culture,
                              Resources.ExceptionCustomFactoryAttributeNotFound,
                              t.FullName,
                              id));
            }
        }
Ejemplo n.º 9
0
 public FactoryUnityExtension(ICustomFactory factory)
 {
     this.factory = factory;
 }
Ejemplo n.º 10
0
 public CustomFactoryBuildStrategy(ICustomFactory factory, ExtensionContext baseContext)
 {
     this.factory     = factory;
     this.baseContext = baseContext;
 }
Ejemplo n.º 11
0
 public ViaFileInfo(ICustomFactory factory)
 {
     _factory = factory ?? throw new ArgumentNullException(nameof(factory));
 }
Ejemplo n.º 12
0
 public ViaFile([NotNull] IFile file, [NotNull] ICustomFactory factory)
 {
     _file    = file ?? throw new ArgumentNullException(nameof(file));
     _factory = factory ?? throw new ArgumentNullException(nameof(factory));
 }
Ejemplo n.º 13
0
        private ICustomFactory GetCustomFactory(Type t, ConfigurationReflectionCache reflectionCache)
        {
            ICustomFactory customFactory = reflectionCache.GetCustomFactory(t);

            return(customFactory);
        }
Ejemplo n.º 14
0
 public MockFactoryStrategy(ICustomFactory factory, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
 {
     this.factory = factory;
     this.configurationSource = configurationSource;
     this.reflectionCache = reflectionCache;
 }
Ejemplo n.º 15
0
 public static void RegisterFactory(ICustomFactory factory)
 {
     Factories.Add(factory);
 }