Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Microsoft Restier ODataV4 on PostgreSQL

License

Notifications You must be signed in to change notification settings

ogd-software/Npgsql.Restier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Npgsql.Restier

Microsoft Restier ODataV4 on PostgreSQL

Includes RESTier related packages, and Npgsql.

RESTier is a RESTful API development framework for building standardized, OData V4 based RESTful services on .NET.

Npgsql is a .NET data provider for PostgreSQL.

Introduction

OData stands for the Open Data Protocol. It was initiated by Microsoft and is now an ISO and OASIS standard. OData enables the creation and consumption of RESTful APIs, which allow resources, defined in a data model and identified by using URLs, to be published and edited by Web clients using simple HTTP requests.

RESTier is a RESTful API development framework for building standardized, OData V4 based RESTful services on .NET platform. It can be seen as a middle-ware on top of Web API OData. RESTier provides facilities to bootstrap an OData service like what WCF Data Services (which is sunset) does, beside this, it supports to add business logic in several simple steps, has flexibility and easy customization like what Web API OData do. It also supports to add additional publishers to support other protocols and additional providers to support other data sources.

For more information about OData, please refer to the following resources:

For more information about OData .Net Library, refer to OData .Net Library document.

For more information about Web API OData Library, refer to Web API OData Library document.

For more information about RESTier, refer to RESTier document.

Please be noted that currently RESTier is still a preview version.

Bootstrap an OData service

The RESTier document has a comprehensive how-to for how to bootstrap an OData service in combination with an MS SQL database. Bootstrapping an OData service in combination with a PostgreSQL database is much the same, but on some points a little different as it is not supported out-of-the-box by Microsoft Visual Studio.

Install Npgsql PostgreSQL Integration

Before you start, you’ll have to install the Npgsql PostgreSQL Integration extension into Visual Studio.

  1. Open Visual Studio 2017 or Visual Studio 2015. If you use Visual Studio 2017 the screens might be slightly different from the screenshots, but the procedures are essentially the same.

  2. From the TOOLS menu, click Extensions and Updates….

  3. In the Extensions and Updates dialog box, click Online.

  4. In the search box type npgsql and press Enter.

  5. Click on the Npgsql PostgreSQL Integration extension and click on Download.

    Install Npgsql PostgreSQL Integration
  6. In the Download and Install dialog box, click Install.

  7. After installation Visual Studio will ask for a restart. Click Restart Now.

Create a project and a web app

Continue with the step-by-step description to Create a project and a web app.

Install the Npgsql.Restier packages

  1. In the Solution Explorer window, right click the project HelloWorld and select Manage NuGet Packages….

  2. In the NuGet Package Manager window, select the Include prerelease checkbox.

  3. Type npgsql restier in the Search Box beside and press Enter.

  4. Select Npgsql.Restier and click the Install button.

    Install Npgsql Restier
  5. In the Preview dialog box, click the OK button

    Preview dialog
  6. In the License Acceptance dialog box, click the I Accept button.

    License Acceptance dialog

Add Npgsql data provider and connection string to web.config

We need to add the data provider and connection string manually, as the connection builder crashes when trying to build a connection string to PostgreSQL. See this issue.

  1. Open the project’s web.config.

  2. Add the Npgsql data provider factory.

    <system.data>
        <DbProviderFactories>
            <add name="Npgsql Data Provider"
                 invariant="Npgsql"
                 description="Data Provider for PostgreSQL"
                 type="Npgsql.NpgsqlFactory, Npgsql" />
        </DbProviderFactories>
    </system.data>
  3. Add a connection string and set the correct values.

    <connectionStrings>
        <add name="defaultConnection"
             connectionString="Host=[host];Port=5432;Database=[database];Username=[username];Password=***"
             providerName="Npgsql" />
    </connectionStrings>

Generate the model classes

  1. In the Solution Explorer window, right click the Models folder under the project HelloWorld and select Add > New Item.

  2. In the Add New Item - HelloWorld dialog box, click C# > Data > ADO.NET Entity Data Model.

  3. Name the model HelloWorldData.

  4. Click the Add button.

    Add New Item HelloWorld dialog
  5. In the Entity Data Model Wizard window, select the item Code First from database.

  6. Click the Next button.

    Code First dialog
  7. The connection string should be selected by default.

  8. Uncheck the Save connection settings in Web.Config as: checkbox.

  9. Click the Next button.

    Entity Data Model Wizard Select Connection
  10. Select the Tables check box and click the Finish button.

    Entity Data Model Wizard Choose Database Objects

Configure the OData Endpoint

In the Solution Explorer window, click HelloWorld > App_Start > WebApiConfig.cs. Replace the WebApiConfig class the following code.

namespace HelloWorld
{
    public static class WebApiConfig
    {
        public static async void Register(HttpConfiguration config)
        {
            // enable query options for all properties
            config.Filter().Expand().Select().OrderBy().MaxTop(null).Count();
            await config.MapRestierRoute<EntityFrameworkApi<HelloWorldData>>(
                "defaultConnection",
                "api/odata",
                new RestierBatchHandler(GlobalConfiguration.DefaultServer));
        }
    }
}

The configuration config.Filter().Expand().Select().OrderBy().MaxTop(null).Count(); is enabling filter/expand/select/orderby/count on all properties. Starting 1.0 release, there are more smaller granularity control on the properties which can be used in query option, and all properties are disabled to be used by default. Users can add configured in CLR class or during model build to configure which properties are allowed to be used in filter/expand/select/orderby/count. Refer to Model bound document for more details.

After these steps, you will have finished bootstrapping an OData service endpoint. You can then Run the project and an OData service is started. Then you can start by accessing the URL http://localhost:<ISS Express port>/api/odata to view all available entity sets, and try with other basic OData CRUD operations. For instance, you may try querying any of the entity sets using the $select, $filter, $orderby, $top, $skip or $apply query string parameters.

Documentation

Please visit the RESTier pages. It has detailed descriptions on each feature provided by RESTier.

Sample services

Refer to sample service github for end to end sample service. The source code also contains end to end service for end to end test purpose. All the sample service can be run with visual studio 2015.

Support