Skip to content

Commit 0204e70

Browse files
authored
[Endpoint routing review]First draft to enable endpoint routing using dynamic controller (OData#2035)
* Enable endpoint routing using dynamic controller * Update to 7.4.0 * Update the PublicAPI test cases * Add the E2E test cases * Change the startup to test the per-route configuration * Some test codes about policy * Use MatcherPolicy Instead of EndpointSelector to resolve the ambiguity * Address the comments and add LinkGenerator test cases * Revert the EF Core version to 2.2.0 to make Aggregation E2E test pass
1 parent 813349a commit 0204e70

File tree

47 files changed

+3349
-92
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+3349
-92
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
9+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" />
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="3.1.0" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.OData\Microsoft.AspNetCore.OData.csproj" />
15+
</ItemGroup>
16+
17+
18+
</Project>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using AspNetCore3xEndpointSample.Web.Models;
7+
using Microsoft.AspNet.OData;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.EntityFrameworkCore;
10+
11+
namespace AspNetCore3xEndpointSample.Web.Controllers
12+
{
13+
public class CustomersController : ODataController
14+
{
15+
private readonly CustomerOrderContext _context;
16+
17+
public CustomersController(CustomerOrderContext context)
18+
{
19+
_context = context;
20+
21+
if (_context.Customers.Count() == 0)
22+
{
23+
IList<Customer> customers = new List<Customer>
24+
{
25+
new Customer
26+
{
27+
Name = "Jonier",
28+
HomeAddress = new Address { City = "Redmond", Street = "156 AVE NE"},
29+
FavoriteAddresses = new List<Address>
30+
{
31+
new Address { City = "Redmond", Street = "256 AVE NE"},
32+
new Address { City = "Redd", Street = "56 AVE NE"},
33+
},
34+
Order = new Order { Title = "104m" },
35+
Orders = Enumerable.Range(0, 2).Select(e => new Order { Title = "abc" + e }).ToList()
36+
},
37+
new Customer
38+
{
39+
Name = "Sam",
40+
HomeAddress = new Address { City = "Bellevue", Street = "Main St NE"},
41+
FavoriteAddresses = new List<Address>
42+
{
43+
new Address { City = "Red4ond", Street = "456 AVE NE"},
44+
new Address { City = "Re4d", Street = "51 NE"},
45+
},
46+
Order = new Order { Title = "Zhang" },
47+
Orders = Enumerable.Range(0, 2).Select(e => new Order { Title = "xyz" + e }).ToList()
48+
},
49+
new Customer
50+
{
51+
Name = "Peter",
52+
HomeAddress = new Address { City = "Hollewye", Street = "Main St NE"},
53+
FavoriteAddresses = new List<Address>
54+
{
55+
new Address { City = "R4mond", Street = "546 NE"},
56+
new Address { City = "R4d", Street = "546 AVE"},
57+
},
58+
Order = new Order { Title = "Jichan" },
59+
Orders = Enumerable.Range(0, 2).Select(e => new Order { Title = "ijk" + e }).ToList()
60+
},
61+
};
62+
63+
foreach (var customer in customers)
64+
{
65+
_context.Customers.Add(customer);
66+
_context.Orders.Add(customer.Order);
67+
_context.Orders.AddRange(customer.Orders);
68+
}
69+
70+
_context.SaveChanges();
71+
}
72+
}
73+
74+
[EnableQuery]
75+
public IActionResult Get()
76+
{
77+
// Be noted: without the NoTracking setting, the query for $select=HomeAddress with throw exception:
78+
// A tracking query projects owned entity without corresponding owner in result. Owned entities cannot be tracked without their owner...
79+
_context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
80+
81+
return Ok(_context.Customers);
82+
}
83+
84+
[EnableQuery]
85+
public IActionResult Get(int key)
86+
{
87+
return Ok(_context.Customers.FirstOrDefault(c => c.Id == key));
88+
}
89+
}
90+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using AspNetCore3xEndpointSample.Web.Models;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Extensions.Logging;
10+
11+
namespace AspNetCore3xEndpointSample.Web.Controllers
12+
{
13+
/// <summary>
14+
/// Keep this one to test that the non-odata request can dispatch correctly.
15+
/// </summary>
16+
[ApiController]
17+
[Route("[controller]")]
18+
public class WeatherForecastController : ControllerBase
19+
{
20+
private static readonly string[] Summaries = new[]
21+
{
22+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
23+
};
24+
25+
private readonly ILogger<WeatherForecastController> _logger;
26+
27+
public WeatherForecastController(ILogger<WeatherForecastController> logger)
28+
{
29+
_logger = logger;
30+
}
31+
32+
[HttpGet]
33+
public IEnumerable<WeatherForecast> Get()
34+
{
35+
var rng = new Random();
36+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
37+
{
38+
Date = DateTime.Now.AddDays(index),
39+
TemperatureC = rng.Next(-20, 55),
40+
Summary = Summaries[rng.Next(Summaries.Length)]
41+
})
42+
.ToArray();
43+
}
44+
}
45+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using System.ComponentModel.DataAnnotations.Schema;
6+
using Microsoft.EntityFrameworkCore;
7+
8+
namespace AspNetCore3xEndpointSample.Web.Models
9+
{
10+
public class Customer
11+
{
12+
public int Id { get; set; }
13+
14+
public string Name { get; set; }
15+
16+
public virtual Address HomeAddress { get; set; }
17+
18+
public virtual IList<Address> FavoriteAddresses { get; set; }
19+
20+
public virtual Order Order { get; set; }
21+
22+
public virtual IList<Order> Orders { get; set; }
23+
}
24+
25+
public class Order
26+
{
27+
public int Id { get; set; }
28+
29+
public string Title { get; set; }
30+
}
31+
32+
[Owned, ComplexType]
33+
public class Address
34+
{
35+
public string City { get; set; }
36+
37+
public string Street { get; set; }
38+
}
39+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using Microsoft.EntityFrameworkCore;
5+
6+
namespace AspNetCore3xEndpointSample.Web.Models
7+
{
8+
public class CustomerOrderContext : DbContext
9+
{
10+
public CustomerOrderContext(DbContextOptions<CustomerOrderContext> options)
11+
: base(options)
12+
{
13+
}
14+
15+
public DbSet<Customer> Customers { get; set; }
16+
17+
public DbSet<Order> Orders { get; set; }
18+
19+
protected override void OnModelCreating(ModelBuilder modelBuilder)
20+
{
21+
modelBuilder.Entity<Customer>().OwnsOne(c => c.HomeAddress).WithOwner();
22+
}
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using Microsoft.AspNet.OData.Builder;
5+
using Microsoft.OData.Edm;
6+
7+
namespace AspNetCore3xEndpointSample.Web.Models
8+
{
9+
public static class EdmModelBuilder
10+
{
11+
private static IEdmModel _edmModel;
12+
13+
public static IEdmModel GetEdmModel()
14+
{
15+
if (_edmModel == null)
16+
{
17+
var builder = new ODataConventionModelBuilder();
18+
builder.EntitySet<Customer>("Customers");
19+
builder.EntitySet<Order>("Orders");
20+
_edmModel = builder.GetEdmModel();
21+
}
22+
23+
return _edmModel;
24+
}
25+
26+
}
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
6+
namespace AspNetCore3xEndpointSample.Web.Models
7+
{
8+
public class WeatherForecast
9+
{
10+
public DateTime Date { get; set; }
11+
12+
public int TemperatureC { get; set; }
13+
14+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
15+
16+
public string Summary { get; set; }
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.Extensions.Hosting;
6+
7+
namespace AspNetCore3xEndpointSample.Web
8+
{
9+
public class Program
10+
{
11+
public static void Main(string[] args)
12+
{
13+
CreateHostBuilder(args).Build().Run();
14+
}
15+
16+
public static IHostBuilder CreateHostBuilder(string[] args) =>
17+
Host.CreateDefaultBuilder(args)
18+
.ConfigureWebHostDefaults(webBuilder =>
19+
{
20+
webBuilder.UseStartup<Startup>();
21+
});
22+
}
23+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:2087",
8+
"sslPort": 0
9+
}
10+
},
11+
"profiles": {
12+
"IIS Express": {
13+
"commandName": "IISExpress",
14+
"launchBrowser": true,
15+
"launchUrl": "weatherforecast",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"AspNetCore3xEndpointSample.Web": {
21+
"commandName": "Project",
22+
"launchBrowser": false,
23+
"launchUrl": "odata/Customers",
24+
"applicationUrl": "http://localhost:5000",
25+
"environmentVariables": {
26+
"ASPNETCORE_ENVIRONMENT": "Development"
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)