Coder Social home page Coder Social logo

devexpress-examples / xpo_how-to-implement-odata4-service-with-xpo-netcore Goto Github PK

View Code? Open in Web Editor NEW
9.0 57.0 9.0 82 KB

How to Implement OData v4 Service with XPO

License: Other

C# 100.00%
xpo orm odata webapi crud rest-api database netcore netcore2

xpo_how-to-implement-odata4-service-with-xpo-netcore's Introduction

How to Implement OData v4 Service with XPO (.NET Core 3.1)

Note: It is much easier to use the Web API Service with integrated authorization & CRUD operations based on ASP.NET Core OData 8.0 (OData v4) powered by EF Core and XPO ORM library instead. For more information, see A 1-Click Solution for CRUD Web API Services with Role-based Access Control via EF Core & XPO (FREE).


This example demonstrates how to create an ASP.NET Core 3.1 Web API project and provide a simple REST API using the XPO ORM for data access. For the .NET Framework-based example, refer to How to Implement OData v4 Service with XPO (.NET Framework).

Prerequisites

Steps To Implement

Step 1: Create Solution and Add Required Dependencies

  • Create a new ASP.NET Core Web Application project and select the API project template.
  • Install the following NuGet packages:
    • DevExpress.Xpo
    • Microsoft.AspNetCore.OData
  • Add files from the CS\ODataService\Helpers folder in this example to your project (Quick Tip: Add files to Visual Studio projects the easy way). These files contain helpers for demo data generation, LINQ and OData API extensions that will be used later.

Step 2: Define XPO and EDM Data Model

Step 3. Initialize Data Layer and Configure ASP.NET Core Middleware

  • Specify a connection string for your database in the CS\ODataService\appsettings.json file (Microsoft SQL Server LocalDB is used by default).
  "ConnectionStrings": {
    "MSSqlServer": "XpoProvider=MSSqlServer;data source=(LocalDB)\\MSSQLLocalDB;Integrated Security=true;MultipleActiveResultSets=true;initial catalog=ODataTest"
  }
  • Modify the ConfigureServices() method in the Startup.cs file to initialize the data layer and register XPO UnitOfWork and OData services in Dependency Injection.
  public void ConfigureServices(IServiceCollection services) {
  	services.AddOData();
  	services.AddODataQueryFilter();
      services.AddMvc(options => {
          options.EnableEndpointRouting = false;
          options.ModelValidatorProviders.Clear();
      });
  
      services.AddSingleton<IObjectModelValidator, CustomModelValidator>();
  
      services.AddXpoDefaultUnitOfWork(true, (DataLayerOptionsBuilder options) =>
          options.UseConnectionString(Configuration.GetConnectionString("MSSqlServer"))
          .UseAutoCreationOption(AutoCreateOption.DatabaseAndSchema) // debug only
          .UseEntityTypes(ConnectionHelper.GetPersistentTypes()));
  }
  • Modify the Configure() method in the Startup.cs file to add middleware for OData services and specify mapping for the service route. Note that we will pass the EDM model defined on the second step as a parameter (SingletonEdmModel.GetEdmModel()).
  public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
      if (env.IsDevelopment())
      {
          app.UseDeveloperExceptionPage();
      }
  
      app.UseODataBatching();
  
      app.UseMvc(b =>
      {
          b.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
          b.MapODataServiceRoute("odata", "odata", SingletonEdmModel.GetEdmModel(), new DefaultODataBatchHandler());
      });
  }

Step 4: Implement OData Controllers for CRUD and Actions/Functions

  • In the Controllers folder, add classes inherited from Microsoft.AspNet.OData.ODataController for each data model class created on the second step.
  • Implement the required methods in OData controllers (e.g., Get, Post, Put, Patch, Delete, etc.) as shown in this example (for instance, CS\ODataService\Controllers\CustomersController.cs).
  • Implement methods in an OData Controller for required OData Actions and Functions as shown in CS\ODataService\Controllers\ActionsController.cs.

xpo_how-to-implement-odata4-service-with-xpo-netcore's People

Contributors

andreykozhevnikov avatar dennis-garavsky avatar devexpressexamplebot avatar donchak avatar goshafighten avatar hamitenes avatar kill4kan avatar maksimkarpenko avatar natakazakova avatar uriahas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.