/// <summary> /// Creates a copy of the repository and apply changes according to the new test values provided in the transformation. /// </summary> /// <typeparam name="TRepository">The type of the repository.</typeparam> /// <typeparam name="TModel">The type of the model.</typeparam> /// <typeparam name="TProperty">The type of the property or field.</typeparam> /// <param name="repository">The repository.</param> /// <param name="node">The node to which the change should be applied.</param> /// <param name="propertyPicker">An expression that identifies the property or field that will have <paramref name="value" /> assigned.</param> /// <param name="value">The value to assign to the property or field identified by <paramref name="propertyPicker" />.</param> /// <returns>The newly created copy. Both parents and children nodes have been cloned as well.</returns> public static TModel With <TRepository, TModel, TProperty>(this TRepository repository, TModel node, Expression <Func <TModel, TProperty> > propertyPicker, TProperty value = default) where TRepository : IObjectRepository where TModel : IModelObject { if (repository == null) { throw new ArgumentNullException(nameof(repository)); } if (node == null) { throw new ArgumentNullException(nameof(node)); } if (propertyPicker == null) { throw new ArgumentNullException(nameof(propertyPicker)); } if (!object.ReferenceEquals(node.Repository, repository)) { throw new GitObjectDbException(NodeNotInRepositoryMessage); } var propertyTransformation = new PropertyTransformation(node, propertyPicker, value); var composer = new TransformationComposer(repository, ImmutableList.Create <ITransformation>(propertyTransformation)); var result = (TRepository)repository.DataAccessor.With(repository, composer); return((TModel)result.GetFromGitPath(node.GetDataPath())); }
public void PropertyTransformationWorksForModifiableProperties(Page page) { // Act var sut = new PropertyTransformation(page, CreateExpression <Page>(p => p.Name), null); // Assert Assert.That(sut.PropertyName, Is.EqualTo(nameof(Page.Name))); }
public void TransformPropertyOnSerializationTest() { var serializer = new JsonSerializer(); var person = new { name = "Sue", age = 5 }; var transformAge = new PropertyTransformation( /*name*/ "age", /*transform*/ (age) => { return (int)age * 2; } ); var options = new SerializationOptions { Include = new[] { "age" }, Transformations = new[] { transformAge } }; var doc = serializer.Serialize(person, options); Assert.Equal(@"{ ""age"": 10 }", doc.ToString()); }