Using OData in .NET Core Web API for MongoDB

Viewed 7763

OData is now supported in .NET Core and 7.2.0 was released. But can it be used with MongoDB? I have searched, but I could not find anything that says one way or the other.

EDIT:

I've found a nuget package https://www.nuget.org/packages/microsoft.aspnetcore.odata and in ConfigureServices I've added this:

And this seems to work for me:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddOData();
    services.AddSingleton<IODataModelManger, ODataModelManager>(DefineEdmModel);
    ...
}

private ODataModelManager DefineEdmModel(IServiceProvider services)
{
    var modelManager = new ODataModelManager();

    var builder = new ODataConventionModelBuilder();
    builder.EntitySet<TestDTO>(nameof(TestDTO));
    builder.EntityType<TestDTO>().HasKey(ai => ai.Id); // the call to HasKey is mandatory
    modelManager.AddModel(nameof(Something), builder.GetEdmModel());

    return modelManager;
}

Controller

[HttpGet("all")]
public async Task<IQueryable<TestDTO>> Get()
{
    // plug your entities source (database or whatever)
    var test = await TestService.GetTest();

    var modelManager = (IODataModelManger)HttpContext.RequestServices.GetService(typeof(IODataModelManger));
    var model = modelManager.GetModel(nameof(Something));
    var queryContext = new ODataQueryContext(model, typeof(TestDTO), null);
    var queryOptions = new ODataQueryOptions(queryContext, HttpContext.Request, Provider);

    return queryOptions
        .ApplyTo(test, new ODataQuerySettings
        {
            HandleNullPropagation = HandleNullPropagationOption.True
        }, null)
        .Cast<TestDTO>();
}

Service

public async Task<IQueryable<TestDTO>> GetTest()
{
    return await GenericRepository.TestAll();
}

Repositories

public async Task<IQueryable<TEntity>> TestAll()
{
    var res = new GetManyResult<TEntity>();
    try
    {
        DateTime startTime = DateTime.Now;
        var collection = GetCollection<TEntity>().AsQueryable();
        var entities = collection.ToArray<TEntity>().AsQueryable();
        return entities
}

But is this the best way to do it?

I mean, shouldn't the collection contain only the elements that meet the filters, beeing more optimised?

If yes, how do I achieve this?

2 Answers

MongoDB OData Connector http://cdn.cdata.com/help/DGB/cd/ Its not free https://www.cdata.com/drivers/mongodb/download/ Overview

The MongoDB OData Connector application enables you to securely access data from MongoDB in popular formats like OData, JSONP, SOAP, RSS, and more. The Getting Started section explains how to establish the connection to MongoDB. In this section, you will find a guide to setting required connection properties and allowing the OData connector to access MongoDB tables. The Supported OData section shows the OData syntax supported by the OData connector and points out any limitations when querying live data. The OData connector can be installed as a stand-alone application or integrated with your server. In the Server Configuration section you will find information on how to install the OData connector on an existing server configuration. System requirements are also listed here. You will also find instructions on how to manage users and deploy SSL. Logging details the available logging resources. The OData API enables access to your data from any application with Web connectivity. The OData connector supports all major authentication schemes. This section documents HTTP methods supported by the server, server responses, and supported authentication schemes. The Data Model section lists the tables, views, and stored procedures available for the application.

Related