Code Monkey home page Code Monkey logo

sharpquery's Introduction

SharpQuery is a simple library for querying and setting values on an object using string inputs. Given the following object:

       var helloWorld = new MyClass {
          String1 = "Hello",
          String2 = "World"
        };

        var halloWorld = new Query<MyClass>(helloWorld)
            .If("String1 == \"Hello\"")
            .Set("String1", "Hallo")
            .Result;

        Assert.AreEqual("Hallo", halloWorld.String1);

It's useful when your business rules tend to be a barrage of different types of conditional logic on a particular object, and you'd benefit from a runtime way to do that rather than hardcoding a ton of if/else statements that are very customer-specific. You can store these strings in a database, for example, and do basic logic that you'd normally have to hardcode.

Under the hood, If is just dynamic Linq, so anything that works in dynamic Linq will work here.

You can exclude the If if you want to use Set without conditions.

You can also exclude the Set if you just care about checking some condition on an object. Result will be null or you can use HasMatch in place of it to get a boolean for whether the object meets the condition.

Set supports dot notation if your fields are nested, and your second parameter can be JSON to set more complex objects.

You can pass lists of strings to If for multiple conditions, and Set accepts a dictionary if you need to update multiple fields on the object.

If your object implements ICloneable, the original object will remain unchanged and the .Result will be a clone with the relevant fields changed. Otherwise, .Result is a reference to the original object, so it will be changed. If you need to preserve the old fields on the input object make it implement ICloneable.

sharpquery's People

Watchers

James Cloos avatar Chad Hedgcock avatar

Forkers

bubdm

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.