Skip to content
This repository has been archived by the owner on Dec 28, 2021. It is now read-only.
/ MultiTypeBinder Public archive

Multi-type object binder; without using any runtime reflection

Notifications You must be signed in to change notification settings

amir734jj/MultiTypeBinder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MultiTypeBinder

This library is similar to my object-binder but supports multiple types for binding without any reflection.

pipeline status

NuGet

public enum Key
{
    Name
}

public class EntityA
{
    public string Name { get; set; }
}

public class EntityB
{
    public string Name { get; set; }
}

public class MultiTypeBinderTest
{    
    [Fact]
    public void Test_Basic_Set()
    {
        // Arrange
        var a = new EntityA {Name = "A"};
        var b = new EntityB {Name = "B"};
        
        var multiTypeItems = new MultiTypeBinderBuilder<Key>()
            .WithType<EntityA>(opt1 => opt1
                .WithProperty(x => x.Name, opt2 => opt2
                    .Bind(Key.Name)
                    .WithGetter(x => x.Name)
                    .WithSetter((x, y) => x.Name = y))
                .FinalizeType())
            .WithType<EntityB>(opt1 => opt1
                .WithProperty(x => x.Name, opt2 => opt2
                    .Bind(Key.Name)
                    .WithGetter(x => x.Name)
                    .WithSetter((x, y) => x.Name = y))
                .FinalizeType())
            .Build()
            .Map(new List<object> { a, b });

        // Act
        multiTypeItems.FirstOrDefault()[Key.Name] = "updated A";
        multiTypeItems.LastOrDefault()[Key.Name] = "updated B";
        
        var v1 = multiTypeItems.FirstOrDefault()[Key.Name];
        var v2 = multiTypeItems.LastOrDefault()[Key.Name];

        // Assert
        Assert.Equal(2, multiTypeItems.Count());
        Assert.Equal("updated A", v1);
        Assert.Equal("updated B", v2);
    }
}

About

Multi-type object binder; without using any runtime reflection

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages