Giter Club home page Giter Club logo

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

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

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 describes how to implement an OData v4 service with XPO and .NET Framework 4.5. This example is an ASP.NET MVC 5 Web API project and provides a simple REST API for data access. For the .NET Core-based example, refer to How to Implement OData v4 Service with XPO (.NET Core).

Steps to Implement

  1. Create a new ASP.NET Web Application project and select the Web API project template (refer to the Create the Visual Studio Project section in this example for details.
  2. Install the following nuget packages:
    • DevExpress.Xpo
    • Microsoft.AspNet.OData
  3. Define your data model - implement persistent classes and initialize the data layer. If you are new to XPO, refer to the following articles to learn how to do this: Create Persistent Class, Map to Existing Tables.
  4. 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).
  5. Modify the Application_Start() method declared in the Global.asax file: register the model body validator class and initialize the Data Access Layer.
protected void Application_Start() {
	GlobalConfiguration.Configuration.Services.Replace(typeof(IBodyModelValidator), new CustomBodyModelValidator());
	GlobalConfiguration.Configure(WebApiConfig.Register);
	XpoDefault.DataLayer = ConnectionHelper.CreateDataLayer(AutoCreateOption.SchemaAlreadyExists, true);
}

public class CustomBodyModelValidator : DefaultBodyModelValidator {
	readonly ConcurrentDictionary<Type, bool> persistentTypes = new ConcurrentDictionary<Type, bool>();
	public override bool ShouldValidateType(Type type) {
		return persistentTypes.GetOrAdd(type, t => !typeof(IXPSimpleObject).IsAssignableFrom(t));
	}
}
  1. Modify the WebApiConfig.cs file: create an ODataModelBuilder instance and register an EntitySet for each persistent class (refer to the WebApiConfig.cs file in this repository to learn how to automatically register all persistent classes):
public static void Register(HttpConfiguration config) {
	config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
	ODataModelBuilder modelBuilder = CreateODataModelBuilder();

	ODataBatchHandler batchHandler =
		new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer);

	config.MapODataServiceRoute(
		routeName: "ODataRoute",
		routePrefix: null,
		model: modelBuilder.GetEdmModel(),
		batchHandler: batchHandler);
}

static ODataModelBuilder CreateODataModelBuilder() { 

	// Include persistent classes to the EdmModel:
	ODataModelBuilder builder = new ODataConventionModelBuilder();
	var customers = builder.EntitySet<Customer>("Customers");
	customers.EntityType.HasKey(t => t.CustomerID);
	// ..

	// Include custom actions and functions into the EdmModel.
	builder.Function("TotalSalesByYear")
		.Returns<decimal>()
		.Parameter<int>("year");

	return builder;
}
  1. Add OData controllers to the Controllers folder. An OData controller is a class inherited from the Microsoft.AspNet.OData.ODataController class. Each controller represents a separate data model class created on the third step.
  2. Implement the required methods in controllers (e.g., Get, Post, Put, Patch, Delete, etc.). For reference, use existing controllers in this example. For example: CS\ODataService\Controllers\CustomersController.cs.

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

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

Contributors

andreykozhevnikov avatar dennis-garavsky avatar devexpressexamplebot avatar donchak avatar dxmi avatar hamitenes avatar maksimkarpenko avatar natakazakova avatar uriahas avatar

Stargazers

 avatar  avatar  avatar  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  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.