Skip to content
/ Biind Public

Bind classes to interfaces, enabling more polymorphic inheritence and extensability of code you don't own, or making using legacy code slightly more enjoyable.

License

Notifications You must be signed in to change notification settings

monoclex/Biind

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Biind

Version Downloads

Bind classes to interfaces, enabling more polymorphic inheritence and extensability of code you don't own, or making using legacy code slightly more enjoyable.

PM> Package-Install Biind

Examples

Two different classes to the same interface

public class A
{
    public int MyValue { get; set; }
}

public class B
{
    public int StoredValue { get; set; }
}

public interface IIntegerValue
{
    int Value { get; set; }
}

public Bind<A, IIntegerValue> MakeABinder(BindAssembly bindAssembly)
	=> new BindOptions<A, IIntegerValue>()
	.MapProperty(e => e.MyValue, e => e.Value)
	.Build(bindAssembly);

public Bind<B, IIntegerValue> MakeBBinder(BindAssembly bindAssembly)
	=> new BindOptions<B, IIntegerValue>()
	.MapProperty(e => e.StoredValue, e => e.Value)
	.Build(bindAssembly);

public void BindingTest()
{
	var assembly = new BindAssembly();

	var aBinder = MakeABinder(assembly);
	var bBinder = MakeBBinder(assembly);

	var a = new A();
	var b = new B();

	a.MyValue = 8;
	b.StoredValue = 4;

	var boundA = aBinder.NewBind(a);
	var boundB = bBinder.NewBind(b);

	Console.WriteLine(boundA.Value); // 8
	Console.WriteLine(boundB.Value); // 4

	boundA.Value = 6;
	boundB.Value = 6;

	Console.WriteLine(a.MyValue); // 6
	Console.WriteLine(b.StoredValue); // 6
}

Binding Console to an interface

public interface ILogger
{
    void WriteLine(string message);
    
    void Write(string message);
}

public ILogger MakeConsole(BindAssembly bindAssembly)
{
    return new BindOptions<object, ILogger>()
        .MapMethod(() => Console.WriteLine(default(string)), e => e.WriteLine(default(string)))
        .MapMethod(() => Console.Write(default(string)), e => e.Write(default(string)))
        .Build(bindAssembly)
        .NewBind(null);
}

var console = MakeConsole(new BindAssembly());

About

Bind classes to interfaces, enabling more polymorphic inheritence and extensability of code you don't own, or making using legacy code slightly more enjoyable.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages