This project is read-only.

A Code Sample

Here I explain how to use QInjection.

Suppose you have a data provider, a class, a service or some other software components that provides you a collection of integers.
This post explain how to apply a runtime lambda expression over this data collection using QInjection.

In order to use QInjection:
  • You have to implement a class that act as Data Provider Bridge of the data collection, this class have to implement the interface IQProvider<T> where T is the type of the items (int in this example)
  • You have to instantiate the QInjector object and then:
    • Define the validation RULE
    • Validate the expression
    • Execute the expression on your Data Provider Bridge (this one will act as container of the runtime injected code).

Following code illustrates how to declare the Bridge Class:

    public class ProviderForInjector : IQProvider<int>
        #region Data Provider Logic
        private List<int> data = new List<int>();

        public ProviderForInjector()

        public int CountOnes()
            return data.Where(x => x == 1).Count();

        internal void ProvideData()
            Random rnd = new Random();
            var size = rnd.Next(100);
            for (var i = 0; i <= size; i++)

        #region SQuery Interface
        public IEnumerable<int> QueryInjectorGetIEnumerable()
            return data;

As you can see the Data Provider Bridge have to implement the method "public IEnumerable<int> QueryInjectorGetIEnumerable()" in order to supply data to the QInjection engine.

Following code shows how to use the QInjector Class in order to execute the lambda expression "x => x == 1":

       ProviderForInjector provider4injector = new ProviderForInjector();

       QInjector injector = new QInjector(@"x => x == 1");

       injector = injector.Support(Node.SimpleLambdaExpression).As(Branch.Input)


       var genericdata = injector.WhereOn<Int32>(provider4injector);

       Console.WriteLine("Check 2");
       if (provider4injector.CountOnes() != genericdata.Count())
           Console.WriteLine("SQuery failed: eat my short!");
           Console.WriteLine("SQuery success: Woo Hoo!!!");

As you can see you need to define step by step the syntax of the lambda expression to accetp as input.
This is done using methods Support(...) and As(...).

In the next post I'll talk about QInjection Internal (and little bit also about Roslyn).

Last edited Dec 30, 2013 at 3:18 PM by SamNium, version 5