public ComplexModel GetModel(Type modelType) { ComplexModel model; if (!_models.TryGetValue(modelType, out model)) { model = new ComplexModel(); _models[modelType] = model; } return(model); }
public object CreateType(DataRequest request, ComplexModel model) { if (model.New != null) { return(model.New(request)); } var constructorInfo = _selector.SelectConstructor(request, request.RequestedType); if (constructorInfo == null) { if (request.RequestedType.GetTypeInfo().IsValueType) { return(Activator.CreateInstance(request.RequestedType)); } throw new Exception("Could not find public constructor on type " + request.RequestedType.FullName); } return(InjectConstructor(constructorInfo, request)); }
public IEnumerable <FieldInfo> SelectFields(object instance, DataRequest request, ComplexModel model) { var skipFields = new List <string>(); var skipFieldsEnumerable = _helper.GetValue <IEnumerable <string> >(request.Constraints, null, "_skipFields"); if (skipFieldsEnumerable != null) { skipFields.AddRange(skipFieldsEnumerable); } return(instance.GetType() .GetRuntimeFields() .Where(f => f.IsPublic && !skipFields.Contains(f.Name))); }
public virtual IEnumerable <PropertyInfo> SelectProperties(object instance, DataRequest request, ComplexModel model) { var skipProperties = new List <string>(); var skipPropertiesEnumerable = _helper.GetValue <IEnumerable <string> >(request.Constraints, null, "_skipProps", "_skipProperties"); if (skipPropertiesEnumerable != null) { skipProperties.AddRange(skipPropertiesEnumerable); } var returnProperties = instance.GetType() .GetRuntimeProperties() .Where(p => p.CanWrite && p.SetMethod.IsPublic && !p.SetMethod.IsStatic && p.SetMethod.GetParameters().Count() == 1 && !skipProperties.Contains(p.Name)); if (request.ParentRequest != null && _configuration.CircularReferenceHandling == CircularReferenceHandlingAlgorithm.OmitCircularReferences) { returnProperties = CheckForCircularProperties(request, returnProperties); } return(returnProperties); }
public virtual void Populate(object instance, DataRequest request, ComplexModel model) { if (instance == null) { return; } if (_configuration.PopulateProperties) { foreach (var propertyInfo in _propertySelector.SelectProperties(instance, request, model)) { object propertyValue = null; if (_configuration.CircularReferenceHandling == CircularReferenceHandlingAlgorithm.AutoWire) { var currentRequest = request; var requestTypeInfo = propertyInfo.PropertyType.GetTypeInfo(); while (currentRequest != null) { if (requestTypeInfo.IsAssignableFrom(currentRequest.RequestedType.GetTypeInfo())) { propertyValue = currentRequest.Instance; break; } currentRequest = currentRequest.ParentRequest; } } var newRequest = CreateDataRequestForProperty(propertyInfo, request); var foundProperty = false; if (propertyValue == null) { foundProperty = _helper.GetUnTypedValue(out propertyValue, propertyInfo.PropertyType, request.Constraints, null, propertyInfo.Name); if (propertyValue != null) { propertyValue = newRequest.Fixture.Behavior.Apply(newRequest, propertyValue); } } if (!foundProperty && propertyValue == null) { if (!model.GetPropertyValue(newRequest, propertyInfo, out propertyValue)) { if (model.SkipProperty(newRequest, propertyInfo)) { continue; } propertyValue = newRequest.Fixture.Generate(newRequest); } else { propertyValue = newRequest.Fixture.Behavior.Apply(newRequest, propertyValue); } } if (!foundProperty && propertyValue == null) { throw new Exception("Could not create type " + propertyInfo.PropertyType.FullName); } _setter.SetProperty(propertyInfo, instance, propertyValue); } } if (_configuration.PopulateFields) { foreach (var fieldInfo in _fieldSelector.SelectFields(instance, request, model)) { object propertyValue; var foundValue = _helper.GetUnTypedValue(out propertyValue, fieldInfo.FieldType, request.Constraints, null, fieldInfo.Name); var newRequest = CreateDataRequestForField(fieldInfo, request); if (propertyValue != null) { propertyValue = newRequest.Fixture.Behavior.Apply(newRequest, propertyValue); } else if (!foundValue) { propertyValue = newRequest.Fixture.Generate(newRequest); } if (!foundValue && propertyValue == null) { throw new Exception("Could not create type " + fieldInfo.FieldType.FullName); } _fieldSetter.SetField(fieldInfo, instance, propertyValue); } } }
/// <summary> /// Default Constructor /// </summary> /// <param name="complexModel"></param> public CustomizeModel(ComplexModel complexModel) { _complexModel = complexModel; }