.NET, OData4, eXpress Persistent Objects
Switch branches/tags
Nothing to show
Clone or download
Latest commit 5e0c2d0 Dec 6, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
CS fix format Dec 6, 2018
.gitattributes first commit Jun 18, 2018
.gitignore first commit Jun 18, 2018
LICENSE first commit Jun 18, 2018
config.json Disable VB code auto generation Jun 25, 2018
readme.md Updated Readme.md (#7) Nov 8, 2018

readme.md

How to implement OData4 service with XPO

This example describes how to implement an OData4 service with XPO. This example is an ASP.NET MVC 5 Web API project and provides simple REST API for data access.

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));
        }
    }
  6. Modify the WebApiConfig.cs file: create an ODataModelBuilder instance and register an EntitySet for each persistent class:

    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;
    }
  7. 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.

  8. Implement the required methods in controllers (e.g., Get, Post, Put, Path, Delete, etc.). For reference, use existing controllers in this example. For example: CS\ODataService\Controllers\CustomersController.cs.