Example #1
0
        public virtual T Build <T>(string variation = "", Action <T> userSpecifiedProperties = null) where T : class
        {
            var constructedObject = _constructorHelper.CreateInstance <T>(_costructors.Get <T>(variation));

            if (_properties.ContainsKey <T>(variation))
            {
                _propertyHelper.SetProperties(_properties.Get <T>(variation), constructedObject);
            }

            if (userSpecifiedProperties != null)
            {
                userSpecifiedProperties.Invoke(constructedObject);
            }

            // We should check if for the object properties we have a creation strategy and call create on that one.
            // Also if the property has a value, don't override.
            //foreach (PropertyInfo prop in constructedObject.GetType().GetProperties())
            //{
            //    if (!_costructors.ContainsKey(variation, prop.PropertyType) || prop.GetValue(constructedObject, null) != null)
            //        continue;

            //    // check if property has a setter
            //    if (prop.GetSetMethod() == null)
            //        continue;

            //    object value = GetType().
            //        GetMethod("CreateForChild").
            //        MakeGenericMethod(prop.PropertyType).
            //        Invoke(this, null);

            //    prop.SetValue(constructedObject, value, null);
            //}

            return(constructedObject);
        }