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?
1 Answers
Answers 1
I think theres only currently one connected service available in the visual studio market place for MongoDB. Link Here.
ODBC Driver for MongoDB provides high-performance and feature-rich connectivity solution for ODBC-based applications to access MongoDB databases from Windows, MacOS, Linux. Full support for standard ODBC API functions, MongoDB data types and SQL queries implemented in our driver makes interaction of your database applications with MongoDB fast, easy and extremely handy.
Looks like it would handle all of the things you'd expect it to when connecting to MongoDB.
However it's worth noting that, that is only a trail and I've been unable to find any 'open source' versions
This comment has been removed by the author.
ReplyDeleteYou can try the driver from datadirect that has been certified by MongoDB
ReplyDelete