예제 #1
0
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            int count = 4;

            Type key   = context.RequestType.GetGenericArguments()[0];
            Type value = context.RequestType.GetGenericArguments()[1];

            // If dictionary key is an enum with unique number of items less than the available count, use that number for the dictionary generation instead.
            if (key.GetTypeInfo().IsEnum)
            {
                int max = Enum.GetNames(context.RequestType.GetGenericArguments()[0]).Length;
                count = max < count ? max : count;
            }

            var keys   = UniqueKeys(count, key, pipeline, new HashSet <object>()).ToList();
            var values = Values(count, value, pipeline).ToList();

            var dictionary = (IDictionary)Activator.CreateInstance(typeof(Dictionary <,>).MakeGenericType(key, value));

            for (var i = 0; i <= count - 1; i++)
            {
                dictionary.Add(keys[i], values[i]);
            }

            return(dictionary);
        }
예제 #2
0
        public override char Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&";
            var num   = _random.Next(0, chars.Length);

            return(chars[num]);
        }
예제 #3
0
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var userName = Guid.NewGuid().ToString("N");
            var domain   = domains[random.Next(domains.Count - 1)];

            return(userName + domain);
        }
예제 #4
0
 private IEnumerable <object> Values(int count, Type closedType, ConstruktionPipeline pipeline)
 {
     for (var i = 0; i < count; i++)
     {
         yield return(pipeline.Construct(new ConstruktionContext(closedType)));
     }
 }
예제 #5
0
        public override DateTime Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var start = DateTime.Today.AddYears(-2);

            var range = (DateTime.Today - start).Days;

            return(start.AddDays(_random.Next(range)));
        }
예제 #6
0
            public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
            {
                var inject = new Foo();

                pipeline.Inject(context.RequestType, inject);

                return(inject);
            }
예제 #7
0
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var closedType = context.RequestType.GenericTypeArguments[0];

            var results = construct(closedType, pipeline);

            return(results);
        }
예제 #8
0
        /// <summary>
        /// Construct a parameter using its attribute's value.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="pipeline"></param>
        /// <returns></returns>
        public virtual object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var attribute = (T)context.ParameterInfo.GetCustomAttribute(typeof(T));

            var value = _value(attribute);

            return(value);
        }
예제 #9
0
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var arrayType = context.RequestType.GetElementType();

            var results = construct(arrayType, pipeline);

            return(results);
        }
예제 #10
0
        public override string Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var prefix = context.PropertyInfo.IsNulloPropertyInfo() ? "String" : context.PropertyInfo.Name;

            var result = prefix + "-" + _random.Next(1, 10000);

            return(result);
        }
예제 #11
0
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var implementation = _typeMap[context.RequestType];

            var result = pipeline.Send(new ConstruktionContext(implementation));

            return(result);
        }
예제 #12
0
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var items = pipeline.Settings.EnumerableCount;

            var key       = context.RequestType.GetGenericArguments()[0];
            var valueType = context.RequestType.GetGenericArguments()[1];

            if (key.GetTypeInfo().IsEnum)
            {
                items = Enum.GetNames(key).Length;
            }

            var keys   = uniqueKeys();
            var values = itemValues(valueType);

            var dictionary = (IDictionary)typeof(Dictionary <,>).NewGeneric(key, valueType);

            for (var i = 0; i <= items - 1; i++)
            {
                dictionary.Add(keys[i], values[i]);
            }

            return(dictionary);

            List <object> uniqueKeys()
            {
                var builtKeys = new HashSet <object>();

                while (true)
                {
                    var newItem = pipeline.Send(new ConstruktionContext(key));

                    if (newItem != null)
                    {
                        builtKeys.Add(newItem);
                    }

                    if (builtKeys.Count == items)
                    {
                        return(builtKeys.ToList());
                    }
                }
            }

            List <object> itemValues(Type closedType)
            {
                var results = new List <object>();

                for (var i = 0; i < items; i++)
                {
                    results.Add(pipeline.Send(new ConstruktionContext(closedType)));
                }

                return(results);
            }
        }
예제 #13
0
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var closedType = context.RequestType.GetGenericArguments()[0];

            var useNull = _random.Next(1, 5);

            return(useNull == 1
                       ? null
                       : pipeline.Send(new ConstruktionContext(closedType)));
        }
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var implementation = _typeMap.ContainsKey(context.RequestType)
                ? _typeMap[context.RequestType]
                : context.RequestType;

            var ctor = BuildCtor(implementation, pipeline);

            var instance = construct(ctor, pipeline);

            return(instance);
        }
예제 #15
0
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var method = typeof(A)
                         .GetMethod("Fake", Type.EmptyTypes)
                         .MakeGenericMethod(context.ParameterInfo.ParameterType);

            var fake = method.Invoke(null, null);

            pipeline.Inject(context.RequestType, fake);

            return(fake);
        }
예제 #16
0
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            if (context.RequestType.GetTypeInfo().IsInterface)
            {
                throw new Exception($"Cannot construct the interface {context.RequestType.Name}. " +
                                    "You must register it or add a custom blueprint.");
            }


            throw new Exception($"No Blueprint could be found for {context.RequestType.FullName}. Please add " +
                                $"a custom blueprint that can create it.");
        }
예제 #17
0
        private HashSet <object> UniqueKeys(int count, Type key, ConstruktionPipeline pipeline, HashSet <object> items)
        {
            var newItem = pipeline.Construct(new ConstruktionContext(key));

            if (newItem != null)
            {
                items.Add(newItem);
            }

            return(items.Count == count
                ? items
                : UniqueKeys(count, key, pipeline, items));
        }
예제 #18
0
        private IList construct(Type closedType, ConstruktionPipeline pipeline)
        {
            var count = 3;
            var items = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(closedType));

            for (var i = 0; i < count; i++)
            {
                var result = pipeline.Construct(new ConstruktionContext(closedType));

                items.Add(result);
            }

            return(items);
        }
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var closedType = context.RequestType.GenericTypeArguments[0];

            var items = (IList)typeof(List <>).NewGeneric(closedType);

            for (var i = 0; i < pipeline.Settings.EnumerableCount; i++)
            {
                var result = pipeline.Send(new ConstruktionContext(closedType));

                items.Add(result);
            }

            return(items);
        }
예제 #20
0
        private Array construct(Type arrayType, ConstruktionPipeline pipeline)
        {
            var count = 3;

            var array = Array.CreateInstance(arrayType, count);

            for (var i = 0; i <= count - 1; i++)
            {
                var value = pipeline.Construct(new ConstruktionContext(arrayType));

                array.SetValue(value, i);
            }

            return(array);
        }
예제 #21
0
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var arrayType = context.RequestType.GetElementType();

            var array = Array.CreateInstance(arrayType, pipeline.Settings.EnumerableCount);

            for (var i = 0; i <= pipeline.Settings.EnumerableCount - 1; i++)
            {
                var value = pipeline.Send(new ConstruktionContext(arrayType));

                array.SetValue(value, i);
            }

            return(array);
        }
        private object construct(Func <object> ctor, ConstruktionPipeline pipeline)
        {
            var instance = ctor();

            var properties = instance.GetType()
                             .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                             .Where(x => x.CanWrite);

            foreach (var property in properties)
            {
                var result = pipeline.Construct(new ConstruktionContext(property));

                property.SetValue(instance, result);
            }

            return(instance);
        }
예제 #23
0
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var instance = Activator.CreateInstance(context.RequestType);

            var properties = context.RequestType
                             .GetTypeInfo()
                             .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                             .Where(x => x.CanWrite);

            foreach (var property in properties)
            {
                var result = pipeline.Construct(new ConstruktionContext(property));

                property.SetValue(instance, result);
            }

            return(instance);
        }
        private Func <object> BuildCtor(Type type, ConstruktionPipeline pipeline)
        {
            var ctors = type.GetTypeInfo()
                        .DeclaredConstructors
                        .ToList();

            var ctor = _ctorStrategy(ctors);

            var @params = new List <ConstantExpression>();

            foreach (var parameter in ctor.GetParameters())
            {
                var ctorArg = parameter.ParameterType;

                var value = pipeline.Construct(new ConstruktionContext(ctorArg));

                @params.Add(Expression.Constant(value));
            }

            return(Expression.Lambda <Func <object> >(Expression.New(ctor, @params)).Compile());
        }
예제 #25
0
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var instance = newUp();

            var properties = pipeline.Settings.PropertyStrategy(context.RequestType);

            foreach (var property in properties)
            {
                var result = pipeline.Send(new ConstruktionContext(property));

                property.SetPropertyValue(instance, result);
            }

            return(instance);

            object newUp()
            {
                var ctors = context.RequestType.GetTypeInfo()
                            .DeclaredConstructors
                            .ToList();

                var ctor = pipeline.Settings.CtorStrategy(ctors);

                var @params = new List <object>();

                foreach (var parameter in ctor.GetParameters())
                {
                    var ctorArg = parameter.ParameterType;

                    var value = pipeline.Send(new ConstruktionContext(ctorArg));

                    @params.Add(value);
                }

                return(context.RequestType.NewUp(@params.ToArray()));
            }
        }
예제 #26
0
 public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
 {
     return("StringB");
 }
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var result = context.PropertyContext.Name + "-" + _random.Next(1, 10000);

            return(result);
        }
예제 #28
0
 /// <summary>
 /// Construct an object of T.
 /// </summary>
 /// <param name="context"></param>
 /// <param name="pipeline"></param>
 /// <returns></returns>
 public abstract T Construct(ConstruktionContext context, ConstruktionPipeline pipeline);
예제 #29
0
 /// <inheritdoc />
 object Blueprint.Construct(ConstruktionContext context, ConstruktionPipeline pipeline) => Construct(context, pipeline);
예제 #30
0
 public override object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
 {
     return(GetAttribute(context).Value);
 }