Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions CS/ODataService/Controllers/ActionsController.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Linq;
using DevExpress.Xpo;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Routing;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using Microsoft.AspNetCore.OData.Routing.Attributes;
using ODataService.Helpers;
using ODataService.Models;

Expand All @@ -16,15 +13,15 @@ public class ActionsController : ODataController {
public ActionsController(UnitOfWork uow) {
this.Session = uow;
}
[ODataRoute("InitializeDatabase")]
[Route("odata/InitializeDatabase")]
public IActionResult InitializeDatabase() {
DemoDataHelper.CleanupDatabase(Session);
DemoDataHelper.CreateDemoData(Session);
return Ok();
}

[HttpGet]
[ODataRoute("TotalSalesByYear(year={year})")]
[Route("odata/TotalSalesByYear(year={year})")]
public IActionResult TotalSalesByYear(int year) {
decimal result = Session.Query<Order>()
.Where(o => o.Date.Value.Year == year)
Expand Down
6 changes: 5 additions & 1 deletion CS/ODataService/Controllers/ContractController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System;
using System.Linq;
using DevExpress.Xpo;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Deltas;
using Microsoft.AspNetCore.OData.Formatter;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Results;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using ODataService.Helpers;
using ODataService.Models;

Expand Down
6 changes: 5 additions & 1 deletion CS/ODataService/Controllers/CustomerController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System;
using System.Linq;
using DevExpress.Xpo;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Deltas;
using Microsoft.AspNetCore.OData.Formatter;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Results;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using ODataService.Helpers;
using ODataService.Models;

Expand Down
5 changes: 4 additions & 1 deletion CS/ODataService/Controllers/DocumentsController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.Linq;
using DevExpress.Xpo;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Formatter;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Results;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using ODataService.Helpers;
using ODataService.Models;

Expand Down
9 changes: 6 additions & 3 deletions CS/ODataService/Controllers/OrderController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System.Linq;
using DevExpress.Xpo;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Routing;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Deltas;
using Microsoft.AspNetCore.OData.Formatter;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Results;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using ODataService.Helpers;
using ODataService.Models;

Expand Down Expand Up @@ -100,7 +103,7 @@ public IActionResult Patch([FromODataUri] int key, Delta<Order> order) {

[HttpPost]
[HttpPut]
[ODataRoute("Order({key})/OrderDetails")]
[Route("Order({key})/OrderDetails")]
public IActionResult AddToOrderDetails([FromODataUri] int key, OrderDetail orderDetail) {
Order order = Session.GetObjectByKey<Order>(key);
if(order == null) {
Expand Down
6 changes: 5 additions & 1 deletion CS/ODataService/Controllers/OrderDetailController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System.Linq;
using DevExpress.Xpo;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Deltas;
using Microsoft.AspNetCore.OData.Formatter;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Results;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using ODataService.Helpers;
using ODataService.Models;

Expand Down
6 changes: 5 additions & 1 deletion CS/ODataService/Controllers/ProductController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System;
using System.Linq;
using DevExpress.Xpo;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Deltas;
using Microsoft.AspNetCore.OData.Formatter;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Results;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using ODataService.Helpers;
using ODataService.Models;

Expand Down
2 changes: 1 addition & 1 deletion CS/ODataService/Helpers/ApiHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.Collections;
using System.Net;
using DevExpress.Xpo;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.OData.Deltas;

namespace ODataService.Helpers {
public static class ApiHelper {
Expand Down
5 changes: 0 additions & 5 deletions CS/ODataService/Helpers/UriHelper.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNet.OData.Routing;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.OData.UriParser;
using ODataService.Models;

Expand Down
4 changes: 1 addition & 3 deletions CS/ODataService/Models/SingletonEdmModel.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using DevExpress.Data.Filtering;
using DevExpress.Xpo;
using DevExpress.Xpo.Metadata;
using Microsoft.AspNet.OData.Builder;
using Microsoft.OData.Edm;
using Microsoft.OData.ModelBuilder;
using ODataService.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ODataService.Models {
Expand Down
8 changes: 4 additions & 4 deletions CS/ODataService/ODataService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DevExpress.Xpo" Version="24.2.6" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.7.8" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="9.3.2" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.2" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.10.0"/>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.10.0"/>
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.10.0"/>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.10.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.10.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.10.0" />
</ItemGroup>
</Project>
33 changes: 17 additions & 16 deletions CS/ODataService/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using System;
using System.Linq;
using DevExpress.Xpo.DB;
using Microsoft.AspNet.OData.Batch;
using Microsoft.AspNet.OData.Extensions;
using DevExpress.Xpo.DB;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using Microsoft.AspNetCore.OData;
using Microsoft.AspNetCore.OData.Batch;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ODataService.Helpers;
using ODataService.Models;
using System;
using System.Linq;

namespace ODataService
{
Expand All @@ -27,12 +26,15 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddOData();
services.AddODataQueryFilter();
services.AddMvc(options => {
options.EnableEndpointRouting = false;
options.ModelValidatorProviders.Clear();
});
services.AddControllers()
.AddOData(opt => opt
.Select()
.Filter()
.OrderBy()
.Expand()
.Count()
.SetMaxTop(null)
.AddRouteComponents("odata", SingletonEdmModel.GetEdmModel()));

services.AddSingleton<IObjectModelValidator, CustomModelValidator>();

Expand All @@ -50,12 +52,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseDeveloperExceptionPage();
}

app.UseODataBatching();
app.UseRouting();

app.UseMvc(b =>
app.UseEndpoints(endpoints =>
{
b.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
b.MapODataServiceRoute("odata", "odata", SingletonEdmModel.GetEdmModel(), new DefaultODataBatchHandler());
endpoints.MapControllers();
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion CS/Tests/ODataTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected Container GetODataContainer() {
[OneTimeSetUp]
public void OneTimeSetup() {
string appPath = Path.GetDirectoryName(this.GetType().Assembly.Location);
appPath = Path.GetFullPath(Path.Combine(appPath, "..", "..", "..", "..", "ODataService", "bin", "debug", "netcoreapp3.1"));
appPath = Path.GetFullPath(Path.Combine(appPath, "..", "..", "..", "..", "ODataService", "bin", "debug", "net8"));
dotnetProcess = Process.Start(new ProcessStartInfo("dotnet") { WorkingDirectory = appPath, Arguments = "ODataService.dll" });
}

Expand Down
2 changes: 1 addition & 1 deletion CS/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net8</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
Expand Down
73 changes: 37 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
<!-- default badges list -->
![](https://img.shields.io/endpoint?url=https://codecentral.devexpress.com/api/v1/VersionRange/223364607/23.2.1%2B)
[![](https://img.shields.io/badge/Open_in_DevExpress_Support_Center-FF7200?style=flat-square&logo=DevExpress&logoColor=white)](https://supportcenter.devexpress.com/ticket/details/T835143)
[![](https://img.shields.io/badge/📖_How_to_use_DevExpress_Examples-e9f6fc?style=flat-square)](https://docs.devexpress.com/GeneralInformation/403183)
[![](https://img.shields.io/badge/💬_Leave_Feedback-feecdd?style=flat-square)](#does-this-example-address-your-development-requirementsobjectives)
<!-- default badges end -->
# How to Implement OData v4 Service with XPO (.NET Core 3.1)
# How to Implement OData v4 Service with XPO (.NET 8)

> **Note**: It is much easier to use the **[Web API Service](https://docs.devexpress.com/eXpressAppFramework/403394/backend-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)](https://community.devexpress.com/blogs/news/archive/2022/06/20/a-one-click-solution-for-role-based-access-control-asp-net-core-web-api-services-via-entity-framework-core-and-xpo-v22-1.aspx).

----------------

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)](https://github.com/DevExpress-Examples/XPO_how-to-implement-odata4-service-with-xpo).
This example demonstrates how to create **an ASP.NET 8** 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)](https://github.com/DevExpress-Examples/XPO_how-to-implement-odata4-service-with-xpo).

## Prerequisites

* [Visual Studio 2019](https://visualstudio.microsoft.com/vs/) with the following workloads:
* [Visual Studio 2022](https://visualstudio.microsoft.com/vs/) with the following workloads:
* **ASP.NET and web development**
* **.NET Core cross-platform development**
* [.NET Core SDK 3.1 or later](https://www.microsoft.com/net/download/all)
* **.NET cross-platform development**
* [.NET 8.0 SDK or later](https://www.microsoft.com/net/download/all)

## Steps To Implement

Expand All @@ -41,38 +40,38 @@ This example demonstrates how to create **an ASP.NET Core 3.1 Web API** project
```
- Modify the `ConfigureServices()` method in the *Startup.cs* file to initialize the data layer and register XPO UnitOfWork and OData services in Dependency Injection.
```cs
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()));
}
public void ConfigureServices(IServiceCollection services) {
services.AddControllers()
.AddOData(opt => opt
.Select()
.Filter()
.OrderBy()
.Expand()
.Count()
.SetMaxTop(null)
.AddRouteComponents("odata", SingletonEdmModel.GetEdmModel()));

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()`).
- Modify the Configure() method in the Startup.cs file to configure routing and map controllers:
```cs
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());
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}

app.UseRouting();

app.UseEndpoints(endpoints => {
endpoints.MapControllers();
});
}
```

### Step 4: Implement OData Controllers for CRUD and Actions/Functions
Expand All @@ -86,3 +85,5 @@ This example demonstrates how to create **an ASP.NET Core 3.1 Web API** project

(you will be redirected to DevExpress.com to submit your response)
<!-- feedback end -->