9

I am looking for a sample Odata application by using WebApi, but without using EF. My requirement is to implement a couple of action methods inside a controller (derived from ODataController) that return complex objects that can be queried. These action methods should also be able to take multiple parameters. The data returned is being pulled from multiple data sources, so using entity framework is not an option. I would like to implement a method like this:

public List RetrieveCustomersByFilter(string name, string lastName, CustomerTypeEnum) { ...business logic goes here ... }

I have done a lot of research online, but I am still not able to find a concrete example. Most of them show simple methods that don't accept any parameters (or an id/key) and return a List of standard objects.

Can anyone provide a sample or point me to a link that shows how to go about this?

Thanks

AgentHunt
  • 669
  • 3
  • 11
  • 28
  • 1
    Check the [tour] to learn what is and isn't on topic here, kthx –  Jun 10 '15 at 00:31
  • 1
    @AgentHut, I add some sample codes at [here](https://github.com/xuzhg/WebApiSample/commit/b5ec977825d9df85bcf75bf9fe87e37ce126f60a). Hope it can help you. And you can refer to [Web API OData](https://github.com/OData/WebApi/blob/master/OData/test/UnitTest/System.Web.OData.Test/OData/Formatter/ODataFunctionTests.cs) repository for more function related test cases. Thanks. – Sam Xu Jun 10 '15 at 08:17
  • possible duplicate of [ASP .NET MVC 4 WebApi: Manually handle OData queries](http://stackoverflow.com/questions/10781309/asp-net-mvc-4-webapi-manually-handle-odata-queries) – qujck Jun 10 '15 at 12:20

2 Answers2

0

check this out has the latest Web API 2 with ODATA sample with all steps ODATA v4 sample with asp.net web api 2

Sundara Prabu
  • 2,361
  • 1
  • 21
  • 20
-2

I guess you need to use agile development. create first a model class.

public class Model{
    public string ID {get; set;}
    public string name {get; set;}
}

then create an dal class,

public class Dal{
     private Model model = new Model();
     public List<Model>getAllRecords()
    {
       List<model> list = new List<model>();
       .....
       .....
       list.Add(new Model{
            ID =.....,
            name = ...
        });
    }
}

then create a logic class that will get the data from dal, and use linq for querying the data and you can also filter the data you want to get inside

  public class Logic{
        private DAL dal = new DAL();

        public dynamic dataGet(int id, string name){
         var a = (from data where dal.getAllRecords()
                   where data.ID == id && data.name == name
                   select new{
                        dataid = data.ID
                        daname = data.name   
                   }).ToList();
          return a;
       }
 }

I hope it will helps you

dada
  • 273
  • 2
  • 14
  • Thanks for your response. I have this part (model+dal) already setup. What I need to implement is an OdataController that has a method which makes a call to dataGet(id,name) has proper routing setup. – AgentHunt Jun 10 '15 at 18:23
  • just create odata controller using visual studio. Change the EF logic inside the controller to your data logic. You can return your data with, AsQuaryable() at the end. – Ege Aydın Dec 23 '15 at 12:56