views:

73

answers:

0

I have an application with an EDMX and an Ado .net data service in it. I would like to make a call from the same application (not an http call) to the ado .net data service and would like to use the url like syntax to query using a string of filters that have been built up programatically:

public class AdventureWorksService : DataService<AdventureWorksEntities>
            {
                public static void InitializeService(IDataServiceConfiguration config)
                {
                    config.UseVerboseErrors = true;
                    config.SetEntitySetAccessRule("*", EntitySetRights.All);
                    config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
                }


                // Can take in a filter like: Filter=Quantity gt '0'
                [WebGet]                     
                public IEnumerable<Products> GetProducts(string queryString)
                {                   
                    try
                    {
                       string baseUri = "/Products?";
                       string queryUri = string.Concat(baseUri, queryString);
                       IQueryable<Product> query = this.CurrentDataSource.CreateQuery<Products>(queryUri);
                       return query.AsEnumerable();
                    }
                    catch (Exception)
                    {
                      //TODO LOG SOMETHING HERE
                      return null;
                    }
                }
        }

For example if I was using MVC and wanted to display some data via my controller, and the Service is in a locally available dll, I shouldn't need to take a service reference on it. I should be able to just reference the dll and new up the service:

     public ActionResult Filter(string queryString)
     {
        AdventureWorksService service = new AdventureWorksService();
        IEnumerable<Products> products = service.GetProducts(queryString);
        return View("Products", products);
     }

Why would I want to do this? It would allow me to leverage the same fitler string that the service would and provide a single code path. I can also avoid the http call by using the local version of the service instead of taking a service reference on something that should already be available.

Has anyone tried anything like this and got it to work? Does anyone know of any issues with this approach?