Nancy icon indicating copy to clipboard operation
Nancy copied to clipboard

How to use NancyFx to create a queryable REST provider

Open nologinatgit opened this issue 7 years ago • 4 comments

Description

I intend to use my Windows Service/Kestrel-hosted Nancy code to provide REST data with Entity Framework Core.

I have created the necessary database and I try to make my GET methods to be QUERYABLE. It means that I could get only some of the data and I don't have to create each method to have TAKE, SKIP etc. functionality.

I guess the only(?) way is ODATA support: by default, ODATA providers support $select, $expand etc. to provide data for various clients (for example)

However, it requires the Queryable attribute - I don't even know how to add attributes to Nancy Get 'methods' - as they are only delegates.

What would you recommend to create such a solution? I can't use the WIKI or the samples as they are outdated.

Steps to Reproduce

Here is my code where I select the data as a DbSet (using Postman, I get all the data):

public HomeModule(IContext context)
{
  MyDbContext = context;
  Get("/", parameters => "Hello Nancy");


  Get("/db", (args) =>
  {
    return MyDbContext.TestData;
  });
}

If I change the code to this, I get nothing:

public HomeModule(IContext context)
{
  MyDbContext = context;
  Get("/", parameters => "Hello Nancy");


  Get("/db", (args) =>
  {
    return MyDbContext.TestData.AsQueryable();
  });
}

System Configuration

  • Nancy version: 2.0.0-clienteastwood

  • Nancy host

    • [ ] Nancy.Owin (Kestrel)
  • Other Nancy packages and versions:

  • Environment (Operating system, version and so on): Windows 10 x64

  • .NET Framework version: .Net Core 2.0

  • Additional information:

nologinatgit avatar May 23 '18 12:05 nologinatgit

There's no support for ODATA in Nancy, and I doubt someone will sit down and write it either. If you need ODATA support, you probably want to use WebAPI :Smile

khellang avatar May 23 '18 13:05 khellang

Yeah, I kind of figured it by now 😃 I'm currently considering creating a REST API that handles all what I need (GET, PUT functions for each entity), I just mourn about losing the ability to sort and skip by default.

I need to choose one of the following solutions:

  • NancyFx: cross-platform technology, but I have to write each GET, PUT function and all their LINQ implementation, though it provides fine granulation
  • WebAPI: can be cross platform (with partial OData support), but I have to write each GET, PUT function and all their LINQ implementation, though it provides fine granulations
  • WCF Data Services: Windows only, every OData function is done, only needs the models

Which approach would you recommend to take?

nologinatgit avatar May 23 '18 14:05 nologinatgit

I wouldn't recommend using ODATA at all, but that's just my opinion.

If you have to use it, I'd probably go for a fourth option:

  • Use ASP.NET Core with Microsoft.AspNetCore.OData. It'll be more or less the same code as WebAPI, but on a more modern platform.

See https://dotnetthoughts.net/getting-started-with-odata-in-aspnet-core/ for some pointers.

khellang avatar May 23 '18 14:05 khellang

Yeah, ASP.NET Core looks promising, I 'm about to create a service based on that. I wonder whether NancyFX can be configured to use that OData NuGet package (utilizing the underlying ASP.NET Core platform), or it is not feasable currently.

As I figure you're not planning to introduce any OData approach :)

nologinatgit avatar May 30 '18 10:05 nologinatgit