-
Notifications
You must be signed in to change notification settings - Fork 163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds dependency injection support in non-model scenario #439
base: release-8.x
Are you sure you want to change the base?
Conversation
|
I agree, I don't like it too. I chose it because it was the method name I used with v7. A suggestion for another name ?
I have a simple webservice with |
Nice work! it's exactly what I needed. Please if you can merge it and create the release it as soon as possible. Thanks a lot! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like this change, but I think it is very important to add a test case
d436efa
to
c8013bd
Compare
test/Microsoft.AspNetCore.OData.E2E.Tests/NonEdm/ConfigureServiceCollectionTest.cs
Outdated
Show resolved
Hide resolved
I left a couple of small comments, but I really like this change, thanks for adding the test case. @xuzhg, since you are more familiar with this repo can you take a look to make sure it fits? |
@kakone @corranrogue9 @xuzhg Could we get some traction on this? We've encountered this issue as well. In our case, we're using the AddODataQueryFilter extension method to enable OData queries on non-OData controllers. We end up with basically the same problem, i.e., there's no way to inject a StringAsEnumResolver so that our OData query filters are case-insensitive. |
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.OData.Query; | ||
using System.Collections.Generic; | ||
using System.Linq; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copy right head
using Microsoft.AspNetCore.OData.Query; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move using System.* ahead
and sort
Gender = e%2 == 0 ? Gender.Female : Gender.Male, | ||
}).ToList(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put it into a new file
public IActionResult Get(ODataQueryOptions<Customer> options) | ||
{ | ||
return Ok(options.ApplyTo(NonEdmDbContext.GetCustomers().AsQueryable())); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a test using [EnableQuery] attribute
} | ||
|
||
[Fact] | ||
public async Task EnableConfigureServiceCollectionTest() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// </summary> | ||
/// <param name="configureServices">The configuring action to add the services to the container.</param> | ||
/// <returns>The current <see cref="ODataOptions"/> instance to enable fluent configuration.</returns> | ||
public ODataOptions ConfigureServiceCollection(Action<IServiceCollection> configureServices) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -62,6 +62,19 @@ public class ODataOptions | |||
/// </summary> | |||
public ODataRouteOptions RouteOptions { get; } = new ODataRouteOptions(); | |||
|
|||
private IServiceProvider ServiceProvider { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make it a field, it seems we don't need a property
/// <returns>The built service provider.</returns> | ||
private IServiceProvider BuildRouteContainer(IEdmModel model, Action<IServiceCollection> setupAction) | ||
private IServiceProvider BuildContainer(Action<IServiceCollection> setupAction, IEdmModel model = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// <returns>The current <see cref="ODataOptions"/> instance to enable fluent configuration.</returns> | ||
public ODataOptions ConfigureServiceCollection(Action<IServiceCollection> configureServices) | ||
{ | ||
ServiceProvider = BuildContainer(configureServices); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🕐
{ | ||
scope.ServiceProvider.GetRequiredService<HttpRequestScope>().HttpRequest = request; | ||
return null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
insert whiteline after {} block
{ | ||
// non-model scenario with dependency injection non enabled | ||
return null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
insert whiteline
@kakone It looks ok to me but would you please take a look for my comments. For example: services.AddOData(); // this can inject the odata serivces into the global level service provider?? In reply to: 1169031180 |
@kakone Did you get a chance to look at the comments? |
@kakone please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement ( “Agreement” ) is agreed to by the party signing below ( “You” ), 1. Definitions. “Code” means the computer software code, whether in human-readable or machine-executable form, “Project” means any of the projects owned or managed by .NET Foundation and offered under a license “Submit” is the act of uploading, submitting, transmitting, or distributing code or other content to any “Submission” means the Code and any other copyrightable material Submitted by You, including any 2. Your Submission. You must agree to the terms of this Agreement before making a Submission to any 3. Originality of Work. You represent that each of Your Submissions is entirely Your original work. Should You wish to Submit materials that are not Your original work, You may Submit them separately 4. Your Employer. References to “employer” in this Agreement include Your employer or anyone else 5. Licenses. a. Copyright License. You grant .NET Foundation, and those who receive the Submission directly license in the Submission to reproduce, prepare derivative works of, publicly display, publicly perform, b. Patent License. You grant .NET Foundation, and those who receive the Submission directly or c. Other Rights Reserved. Each party reserves all rights not expressly granted in this Agreement. 6. Representations and Warranties. You represent that You are legally entitled to grant the above 7. Notice to .NET Foundation. You agree to notify .NET Foundation in writing of any facts or 8. Information about Submissions. You agree that contributions to Projects and information about 9. Governing Law/Jurisdiction. This Agreement is governed by the laws of the State of Washington, and 10. Entire Agreement/Assignment. This Agreement is the entire agreement between the parties, and .NET Foundation dedicates this Contribution License Agreement to the public domain according to the Creative Commons CC0 1. |
@microsoft-github-policy-service agree company="Berger-Levrault" |
Yes, changes done. |
please merge this pr quickly |
Great! It would be nice to have this PR merged and released! Thanks for your hard work! |
Adds a
EnableDependencyInjection(Action<IServiceCollection> configureServices)
method inODataOptions
to be able to add services (StringAsEnumResolver
for example) in non-model scenario.Fixes #422
Usage :