This project is read-only.

Using QInjection with Web Service

At the end of this first series of posts I explain how to set up a web service that uses QInjection in order to expose a SSWhere operation (Service Side Where Operation).

The first thing to do is to define the Service Interface:

    [ServiceContract]
    public interface ISimpleServiceProvider
    {
        [OperationContract]
        IEnumerable<int> Where(String expression);
    }


After you need to define your Data Provider to bound to QInjection:

    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();
        }

        public void ProvideData()
        {
            Random rnd = new Random();
            var size = rnd.Next(100);
            for (var i = 0; i <= size; i++)
                data.Add(rnd.Next(2));
        }

        #endregion

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


At the end of all then you have to implement the service operation:

    public class SimpleServiceProvider : ISimpleServiceProvider
    {
        public IEnumerable<int> Where(string expression)
        {
            ProviderForInjector provider4injector = new ProviderForInjector();
            provider4injector.ProvideData();

            QueryInjector injector = new QueryInjector(expression);

            injector = injector.Support(Node.SimpleLambdaExpression).As(Branch.Input)
            	.Support(Node.BinaryExpression).As(Branch.Body)
                .Support(Node.IdentifierName).As(Branch.LeftMember)
                .Support(Node.LiteralExpression).As(Branch.RightMember)
                .Support(Node.Parameter).As(Branch.Parameter);

            injector.Validate();

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

            if (provider4injector.CountOnes() != genericdata.Count())
                throw new FaultException("Query result mismatch: eat my short!");

            return genericdata;
        }
    }
 


Note these three C# entities have to share the same NameSpace: QueryInjection.Service.

Last edited Dec 28, 2013 at 3:53 PM by SamNium, version 2