All Projects → smatsson → Chainly

smatsson / Chainly

Licence: mit
Make any .NET object a fluent interface regardless if you have the source code or not!

Projects that are alternatives of or similar to Chainly

Mailbody
Create transactional email with a fluent interface (.net)
Stars: ✭ 151 (+694.74%)
Mutual labels:  fluent-interface
Dexiom.EPPlusExporter
A very simple, yet incredibly powerfull library to generate Excel documents out of objects, arrays, lists, collections, etc.
Stars: ✭ 19 (+0%)
Mutual labels:  fluent-interface
csharp-http-client
Twilio SendGrid's C# HTTP Client for calling APIs
Stars: ✭ 25 (+31.58%)
Mutual labels:  fluent-interface
Fluent Reveal Effect
Fluent Reveal Effect JavaScript library for web
Stars: ✭ 164 (+763.16%)
Mutual labels:  fluent-interface
ugo
Simple and expressive toolbox written in Go
Stars: ✭ 27 (+42.11%)
Mutual labels:  fluent-interface
csvplus
csvplus extends the standard Go encoding/csv package with fluent interface, lazy stream operations, indices and joins.
Stars: ✭ 67 (+252.63%)
Mutual labels:  fluent-interface
Patchfluent
💧 🦄 Customize Windows 10 Updates
Stars: ✭ 128 (+573.68%)
Mutual labels:  fluent-interface
Pnp Js Core
Code moved to https://github.com/pnp/pnpjs. This repository is archived.
Stars: ✭ 382 (+1910.53%)
Mutual labels:  fluent-interface
1c http
Подсистема 1С для работы с HTTP
Stars: ✭ 48 (+152.63%)
Mutual labels:  fluent-interface
php-underscore
PHP underscore inspired &/or cloned from _.js, with extra goodies like higher order messaging
Stars: ✭ 42 (+121.05%)
Mutual labels:  fluent-interface
Filehound
Flexible and fluent interface for searching the file system
Stars: ✭ 190 (+900%)
Mutual labels:  fluent-interface
SwiftBuilder
SwiftBuilder is a fast way to assign new value to the property of the object.
Stars: ✭ 26 (+36.84%)
Mutual labels:  fluent-interface
php-currency-api
Standardized wrapper for popular currency rate APIs. Currently supports FixerIO, CurrencyLayer, Open Exchange Rates and Exchange Rates API.
Stars: ✭ 17 (-10.53%)
Mutual labels:  fluent-interface
Jaque
Lets Java 8 Lambdas to be represented as objects in the form of expression trees at runtime
Stars: ✭ 152 (+700%)
Mutual labels:  fluent-interface
Acf Fluent
✒️ A fluent interface for the Advanced Custom Fields WordPress plugin
Stars: ✭ 264 (+1289.47%)
Mutual labels:  fluent-interface
Fluentmediator
🔀 FluentMediator is an unobtrusive library that allows developers to build custom pipelines for Commands, Queries and Events.
Stars: ✭ 128 (+573.68%)
Mutual labels:  fluent-interface
fluentcheck
Fluent assertions for Python
Stars: ✭ 79 (+315.79%)
Mutual labels:  fluent-interface
Validation
The most awesome validation engine ever created for PHP
Stars: ✭ 5,484 (+28763.16%)
Mutual labels:  fluent-interface
Thumbnailator
Thumbnailator - a thumbnail generation library for Java
Stars: ✭ 3,845 (+20136.84%)
Mutual labels:  fluent-interface
windows-ui-web
Build windows fluent UI apps or web apps using Html, CSS & JavaScript. Comes with rich native like components, icon sets. Used as fast prototyping tool for Windows environment platforms.
Stars: ✭ 98 (+415.79%)
Mutual labels:  fluent-interface

Chainly

Make any .NET object a fluent interface regardless if you have the source code or not!

NuGet

PM> Install-Package Chainly

Reflection.Emit method

Example

Given a class Asdf we define an interface IAsdfChain. We then run the Chain extension on the Asdf instance and voila! Fluent interface! :)

var myString = new Asdf("This is my string!")
				.Chain<IAsdfChain>()
				.SomeMethod()
				.ParameterMethod("A", 123)
				.Value() // Call .Value() to get the unchained Asdf instance
				.GetMyString();
// myString == "This is my string!"

public class Asdf
{
	private readonly string _myString;

	public int SomeMethodCount { get; set; }
	public int ParameterMethodWithTwoParametersCount { get; set; }

	public Asdf(string myString)
	{
		_myString = myString;
	}

	public void SomeMethod()
	{
		SomeMethodCount++;
	}

	public void ParameterMethod(string value, int otherValue)
	{
		ParameterMethodWithTwoParametersCount++;
	}

	public string GetMyString()
	{
		return _myString;
	}
}

// Only methods defined in this interface will be made fluent. Other methods will be left alone.
public interface IAsdfChain
{
	IAsdfChain SomeMethod();
	IAsdfChain ParameterMethod(string value, int value2);
	// Value is a special method, returning the original unchained object.
	Asdf Value();
}

Of course it also works with built in classes!

var buffer = new char[2];
var asdfUpper = "asdf".Chain<IChainString>().CopyTo(0, buffer, 0, 2).Value().ToUpper();
// buffer == ['a', 's']
// asdfUpper = ASDF

var elapsed = StopWatch.StartNew().Chain<IStopWatchChain>().Stop().Value().Elapsed;

public interface IChainString
{
	IChainString CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count);
	string Value();
}

public interface IStopWatchChain
{
	IStopWatchChain Stop();
	Stopwatch Value();
}

What are the steps to make an object fluent?

  • Create an interface. You can call it anything you'd like but it must be public.
  • Add each void method you want to make fluent to the interface and use the interface as return type (not all void methods are required, only the ones you wish to make fluent).
  • Add a Value method with the object type as return value.
  • Run Chain<{interfacename}> on the object to make it fluent.

Example

public interface IMyInterface
{
	IMyInterface SomeVoidMethod(string param1, int param2);
	object Value();
}

objectWithSomeVoidMethod = objectWithSomeVoidMethod
							.Chain<IMyInterface>
							.SomeVoidMethod("A", 1)
							.SomeVoidMethod("B", 2)
							.Value();

Action based method

Summary

The action based method allows chaining for any type without the need for an interface.

Example

var model = new Asdf("a");

var myString = model.Chain()
	.Do(m => m.ParameterMethod("b"))
	.Do(m => m.ParameterMethod("c"))
	.Do(m => m.ParameterMethod("d", 1))
	.Value()
	.GetMyString();

Overloaded Operator Example

It's also possible to use + instead of .Do().

var model = new Asdf("a");

var myString = (model.Chain()
				+ (m => m.ParameterMethod("b"))
				+ (m => m.ParameterMethod("c"))
				+ (m => m.ParameterMethod("d", 1)))
	.Value()
	.GetMyString();

Project structure

Chainly.Dotnet.sln - .NET core project supporting multiple frameworks Chainly.net452.sln - Old .NET 4.5.2 solution kept for compatibility

License

This project is licensed under the MIT license, see LICENSE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].