Code Monkey home page Code Monkey logo

fastexpressionkit's Introduction

FastExpressionKit

.NET Core

A small library to make reflection-y things faster

Reflection in C# can be slow. There are operations that iterate over certain properties of objects that typically use reflection, with suboptimal performance.

This mini-library (just one .cs file to copy to your project, less than 200 lines!) provides minimal building blocks that generate code (Linq Expressions) and compile it to fast machine code. The recommended pattern is to instantiate the class where you configure your mappings (takes few milliseconds), and then reuse the instance for hundreds of operations with good performance (few microseconds per round).

Installation

Options:

Either

  • Download FastExpressionKit.cs and add it to your project

Or use the nuget package:

$ dotnet add package FastExpressionKit

and to refer to the file in your csproj file, add this to paket.references:

File: FastExpressionKit.cs

After paket install, you will find the file in paket-files/ directory. A reference has automatically been added to your csproj file.

Usage

FieldExtract

Extract fields a and b to array of integers:

var extractor = new FieldExtract<C, int>(new[] { "a", "b" };);
var results = extractor.Extract(c1);

If you want fields boxed to object (i.e. don't want to specify the target type), there is a special case for object:

var boxedExtract = new FieldExtract<BigDto, object>(bigpropnames);

Clearly, dealing with the end result will be slower as you got objects now.

Differ

Compare two objects field-by-field, yielding a list of differences. Works across different classes.

var differ = new Differ<C, C>(new[] { "a", "b" });
var res = differ.Compare(c1, c2);

// compare different types!
var differ2 = new Differ<C, D>(new[] { "a", "b" });
res = differ2.Compare(c1, d1);

Copier

Copy a set of fields from one object to another.

var copier = new FieldCopier<C, C>(fields);
copier.Copy(c1, c2);

Yes, this is essentially a trivial version of AutoMapper (when you specify the list of fields in the target type). The property types must be of the same type, as copying is just doing src.foo = target.foo for each property.

Nullable T? can be assigned on top of non-nullable T with FieldCopier, it coerces nulls to default(T).

Utility classes

The library also contains some helpers for doing reflection (ReflectionHelper) and static helpers for creating Expressions (EE), but those are not part of the documented API.

License

License: MIT
Copyright 2017 Ville M. Vainio

fastexpressionkit's People

Contributors

dependabot[bot] avatar vivainio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

fastexpressionkit's Issues

Include `System.ComponentModel.Annotations` package to support .NET Standard 2.0

Hey - I stumbled across your repository from your recent post on DEV and it caught my eye about how you mentioned that System.ComponentModel.DataAnnotations is missing from .NET Standard 2.0 - while you are right, you can still target .NET Standard 2.0 by including the System.ComponentModel.Annotations package.

That package includes the attributes you were expecting under System.ComponentModel.DataAnnotations.

I do this in my own library here: https://github.com/TurnerSoftware/MongoFramework/blob/d8bda7f76d505f668b7dd4339849a91e51347422/src/MongoFramework/MongoFramework.csproj#L3-L16

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.