Coder Social home page Coder Social logo

antiboilerplateextensions's Introduction

Anti-boilerplate

Remove boilerplate from your C# code.

Build Status

Installation

dotnet add package antiboilerplate --version 1.2.0

Functional

Sprinkle some functional magic on your C# code

Map

Map your object of type TIn to type TOut using the provided mapping function:

var httpRequest = someObject
    .Map(o => JsonConvert.SerializeObject(o))
    .Map(o => new StringContent(o, Encoding.UTF8, "application/json"))
    .Map(o => new HttpRequestMessage(HttpMethod.Post,"chat.postMessage")
        {
            Content = o
        });

Then

Perform action on object before returning it.

public string CreateMessage(string data)
    => $"Some message: {data}".Then(s => Console.WriteLine(s));

AsCollection

Treat any object as a collection (typically to be able to use Linq).

var transformedArray = someArray
    .Union(someObject.AsCollection())
    .Select(x => doAMapping(x));

Each

Perform an action on each element in an array (similar to foreach).

new []{1, 2, 3}
    .Select(x => x * x)
    .Each(Console.WriteLine); // method group

Array

Deconstruct arrays (C# 7).

var (lastName, firstName, _) =
    "Chaplin, Charlie Spencer".Split(new[] {' ', ','},
        StringSplitOptions.RemoveEmptyEntries);

String

IsNullOrEmpty

if (someString.IsNullOrEmpty())
{
    ...
}

IsNullOrWhitespace

if (someString.IsNullOrWhitespace())
{
    ...
}

ToUtf8Base64

Example: creating a basic authorization header.

var authorizationHeader = "Basic "
   + $"{username}:{password}".ToUtf8Base64()

FromUtf8Base64

Example: decoding a basic authorization header. (Including array deconstruction)

    var (username, password) = authorizationHeader
        .FromUtf8Base64()
        .Split(':');

Disposables

Handle disposable objects without using unnecessary variables.

var text = Disposable.Using(() => File.OpenText("somefile.txt"), file => file.ReadToEnd());

Instead of:

string text;
using (var file = File.OpenText("somefile.txt"))
{
    text = file.ReadToEnd();
}

Embedded resources

Read an embedded resource:

var data = await EmbeddedResource.Read("Antiboilerplate.tests.TestData.ConfigurationTestData.json",
    Assembly.GetExecutingAssembly());

Read a Json file by naming convention. Add a marker type in with the same name and in the same namespace as the embedded resource:

// Declare type in the same namespace as ConfigurationTestData.json
internal class ConfigurationTestData {}
...
string data = await EmbeddedResource.ReadJson<ConfigurationTestData>();

As above, but parse the Json file using DataContractJsonSerializer:

ConfigurationTestContents config = await EmbeddedResource.ReadAndDeserializeJson<ConfigurationTestData, ConfigurationTestContents>();

Reads and parses and XML file:

XDocument data = await EmbeddedResource.ReadXml<XmlTestData>();

URL

Parse the query from a URI:

IDictionary<string,string> query = new Uri("http://host/path?a=1&b=2").ParseQuery();
var a = query["a"]; // => 1

Parse the query from a string:

IDictionary<string,string> query = "http://host/path?a=1&b=2".ParseQuery();
var a = query["a"]; // => 1

Create URI from string:

Uri uri = "http://host/path?a=1&b=2".ToUri();

antiboilerplateextensions's People

Contributors

azure-pipelines[bot] avatar vidarkongsli avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

antiboilerplateextensions's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.