Skip to content

Commit da30e83

Browse files
committed
rename PropertyDescripor and refactor
1 parent f87be69 commit da30e83

33 files changed

+423
-121
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace AspNetCoreODataSample.Web.Controllers
2+
{
3+
internal class DataConnection
4+
{
5+
public DataConnection()
6+
{
7+
}
8+
}
9+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using LinqToDB.Data;
2+
using Microsoft.AspNet.OData;
3+
using Microsoft.AspNetCore.Mvc;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
using WebApplication1;
9+
10+
namespace AspNetCoreODataSample.Web.Controllers
11+
{
12+
public class RequestsController:ODataController
13+
{
14+
[EnableQuery()]
15+
public IActionResult Get()
16+
{
17+
var con = new DataConnection();
18+
return new ObjectResult(con.GetTable<Requests>());
19+
}
20+
21+
[EnableQuery()]
22+
public IActionResult Get(int key)
23+
{
24+
var con = new DataConnection();
25+
return new ObjectResult(con.GetTable<Requests>().Where(_ => _.Id == key));
26+
}
27+
28+
[EnableQuery()]
29+
public IActionResult GetTasks(int key)
30+
{
31+
var con = new DataConnection();
32+
return new ObjectResult(con.GetTable<Requests>().Where(_ => _.Id == key).SelectMany(_=>_.Tasks()));
33+
}
34+
}
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using LinqToDB.Data;
2+
using Microsoft.AspNet.OData;
3+
using Microsoft.AspNetCore.Mvc;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
9+
namespace WebApplication1.Controllers
10+
{
11+
public class TasksController:ODataController
12+
{
13+
[EnableQuery]
14+
public IActionResult Get()
15+
{
16+
var con = new DataConnection();
17+
return new ObjectResult(con.GetTable<Tasks>());
18+
}
19+
20+
[EnableQuery()]
21+
public IActionResult Get(int key)
22+
{
23+
var con = new DataConnection();
24+
return new ObjectResult(con.GetTable<Tasks>().Where(_=>_.Id == key));
25+
}
26+
27+
public IActionResult GetStagesFromTasks(int key)
28+
{
29+
var con = new DataConnection();
30+
return new ObjectResult(con.GetTable<Tasks>().Where(_ => _.Id == key).SelectMany(_=>_.Stages()));
31+
}
32+
}
33+
}

samples/WebApplication1/Model.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using LinqToDB.Mapping;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Linq.Expressions;
6+
using System.Threading.Tasks;
7+
8+
namespace WebApplication1
9+
{
10+
[Table(Schema ="bda", IsColumnAttributeRequired =false)]
11+
public class Requests
12+
{
13+
[PrimaryKey]public int Id { get; set; }
14+
}
15+
16+
[Table(Schema ="awt", IsColumnAttributeRequired =false)]
17+
public class Tasks
18+
{
19+
public int Id { get; set; }
20+
public int TargetId { get; set; }
21+
22+
[Association(ExpressionPredicate = nameof(ActualTaskExp), CanBeNull = true)]
23+
public TaskStages ActualStage { get; set; }
24+
25+
private static Expression<Func<Tasks, TaskStages, bool>> ActualTaskExp()
26+
=> (t, ts) => t.Id == ts.TaskId
27+
&& ts.Actual == true;
28+
29+
[Association(ThisKey = "TargetId", OtherKey = "Id")]
30+
public Requests Request { get; set; }
31+
}
32+
33+
[Table(Schema = "awt", IsColumnAttributeRequired = false)]
34+
public class TaskStages
35+
{
36+
public int Id { get; set; }
37+
public int TaskId { get; set; }
38+
public bool Actual { get; set; }
39+
}
40+
41+
public static class Extensions {
42+
43+
[Association(OtherKey ="Id", ThisKey ="TargetId")]
44+
public static Requests Request(this Tasks src)
45+
{
46+
throw new InvalidOperationException("Use this metod in LINQ expressions.");
47+
}
48+
49+
[Association(ThisKey ="Id", OtherKey ="TaskId")]
50+
public static IEnumerable<TaskStages> Stages(this Tasks src) {
51+
throw new InvalidOperationException("Use this metod in LINQ expressions.");
52+
}
53+
54+
[Association(ThisKey = "Id", OtherKey = "TargetId")]
55+
public static IEnumerable<Tasks> Tasks(this Requests src)
56+
{
57+
throw new InvalidOperationException("Use this metod in LINQ expressions.");
58+
}
59+
}
60+
}

samples/WebApplication1/Program.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore;
7+
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.Logging;
10+
11+
namespace WebApplication1
12+
{
13+
public class Program
14+
{
15+
public static void Main(string[] args)
16+
{
17+
BuildWebHost(args).Run();
18+
}
19+
20+
public static IWebHost BuildWebHost(string[] args) =>
21+
WebHost.CreateDefaultBuilder(args)
22+
.UseStartup<Startup>()
23+
.Build();
24+
}
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:63313/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": false,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"WebApplication1": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"environmentVariables": {
22+
"ASPNETCORE_ENVIRONMENT": "Development"
23+
},
24+
"applicationUrl": "http://localhost:63314/"
25+
}
26+
}
27+
}

samples/WebApplication1/Startup.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using LinqToDB.Data;
6+
using LinqToDB.DataProvider.SqlServer;
7+
using Microsoft.AspNetCore.Builder;
8+
using Microsoft.AspNetCore.Hosting;
9+
using Microsoft.AspNet.OData;
10+
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.AspNet.OData.Extensions;
12+
using Microsoft.AspNet.OData.Builder;
13+
using Microsoft.OData.Edm;
14+
using Microsoft.Extensions.Logging;
15+
16+
namespace WebApplication1
17+
{
18+
public class Startup
19+
{
20+
// This method gets called by the runtime. Use this method to add services to the container.
21+
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
22+
public void ConfigureServices(IServiceCollection services)
23+
{
24+
DataConnection.AddConfiguration("1", "server=(localdb)\\MSSQLLocalDb; Database=Satellite; Integrated Security=true", new SqlServerDataProvider("sql", SqlServerVersion.v2012));
25+
DataConnection.DefaultConfiguration = "1";
26+
27+
28+
services.AddOData();
29+
services.AddMvc();
30+
}
31+
32+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
33+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory logFactory)
34+
{
35+
var logger = logFactory.CreateLogger<DataConnection>();
36+
DataConnection.WriteTraceLine=(a1, a2) => logger.LogInformation(a1);
37+
DataConnection.TurnTraceSwitchOn();
38+
if (env.IsDevelopment())
39+
{
40+
app.UseDeveloperExceptionPage();
41+
}
42+
43+
app.UseMvc(router =>
44+
{
45+
router.Select().Expand().Filter().OrderBy().MaxTop(100).Count();
46+
router.MapODataServiceRoute("d", "", GetEdmModel());
47+
});
48+
}
49+
50+
private IEdmModel GetEdmModel()
51+
{
52+
var builder = new ODataConventionModelBuilder();
53+
54+
//builder.EnumType<Assignments>("")
55+
builder.EntitySet<Tasks>("Tasks");
56+
builder.EntityType<Tasks>().HasRequired(r=>r.Request);
57+
builder.EntityType<Tasks>().HasRequired(r=>r.ActualStage);
58+
builder.EntityType<Tasks>().HasMany(r=>r.Stages());
59+
60+
builder.EntitySet<Requests>("Requests");
61+
builder.EntityType<Requests>().HasMany<Tasks>(_=>_.Tasks());
62+
63+
return builder.GetEdmModel();
64+
}
65+
}
66+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Folder Include="wwwroot\" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="linq2db" Version="2.1.0" />
13+
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.8" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.OData\Microsoft.AspNetCore.OData.csproj" />
18+
</ItemGroup>
19+
20+
</Project>

sln/WebApiOData.AspNetCore.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Microsoft.AspNet.OData.Test
1919
EndProject
2020
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.OData.Test", "..\test\UnitTest\Microsoft.AspNetCore.OData.Test\Microsoft.AspNetCore.OData.Test.csproj", "{2B8085F7-3625-489E-A963-E29C2BD1AA76}"
2121
EndProject
22+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApplication1", "..\samples\WebApplication1\WebApplication1.csproj", "{58C42EC8-7694-405B-814E-67E764F83BD0}"
23+
EndProject
2224
Global
2325
GlobalSection(SharedMSBuildProjectFiles) = preSolution
2426
..\src\Microsoft.AspNet.OData.Shared\Microsoft.AspNet.OData.Shared.projitems*{b6b951b6-c3f0-4b8e-8955-e039145e7dec}*SharedItemsImports = 13
@@ -48,6 +50,12 @@ Global
4850
{2B8085F7-3625-489E-A963-E29C2BD1AA76}.Debug|Any CPU.Build.0 = Debug|Any CPU
4951
{2B8085F7-3625-489E-A963-E29C2BD1AA76}.Release|Any CPU.ActiveCfg = Release|Any CPU
5052
{2B8085F7-3625-489E-A963-E29C2BD1AA76}.Release|Any CPU.Build.0 = Release|Any CPU
53+
{58C42EC8-7694-405B-814E-67E764F83BD0}.CodeAnalysis|Any CPU.ActiveCfg = Debug|Any CPU
54+
{58C42EC8-7694-405B-814E-67E764F83BD0}.CodeAnalysis|Any CPU.Build.0 = Debug|Any CPU
55+
{58C42EC8-7694-405B-814E-67E764F83BD0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
56+
{58C42EC8-7694-405B-814E-67E764F83BD0}.Debug|Any CPU.Build.0 = Debug|Any CPU
57+
{58C42EC8-7694-405B-814E-67E764F83BD0}.Release|Any CPU.ActiveCfg = Release|Any CPU
58+
{58C42EC8-7694-405B-814E-67E764F83BD0}.Release|Any CPU.Build.0 = Release|Any CPU
5159
EndGlobalSection
5260
GlobalSection(SolutionProperties) = preSolution
5361
HideSolutionNode = FALSE
@@ -58,6 +66,7 @@ Global
5866
{0D53FCCE-2173-4FDB-A552-C3BBB71DDC49} = {3789AD84-4A87-45C1-BD5C-3B4BE39157C8}
5967
{D909E7AB-3281-46EA-929B-F35FE7E94B0F} = {2F6228EC-37EE-4FCB-A38A-B94EE472E70E}
6068
{2B8085F7-3625-489E-A963-E29C2BD1AA76} = {2F6228EC-37EE-4FCB-A38A-B94EE472E70E}
69+
{58C42EC8-7694-405B-814E-67E764F83BD0} = {3789AD84-4A87-45C1-BD5C-3B4BE39157C8}
6170
EndGlobalSection
6271
GlobalSection(ExtensibilityGlobals) = postSolution
6372
SolutionGuid = {731B144D-90AE-4567-B337-7CFE9965AACC}

src/Microsoft.AspNet.OData.Shared/Builder/EdmModelHelperMethods.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,12 @@ private static string ConvertBindingPath(EdmTypeMap edmMap, NavigationPropertyBi
185185
}
186186
else if (propertyInfo != null)
187187
{
188-
PropertyDescriptor propDescr = new PropertyDescriptor(propertyInfo);
188+
MemberDescriptor propDescr = new MemberDescriptor(propertyInfo);
189189
bindings.Add(edmMap.EdmProperties[propDescr].Name);
190190
}
191191
else if (methodInfo != null)
192192
{
193-
PropertyDescriptor propDescr = new PropertyDescriptor(methodInfo);
193+
MemberDescriptor propDescr = new MemberDescriptor(methodInfo);
194194
bindings.Add(edmMap.EdmProperties[propDescr].Name);
195195
}
196196
}
@@ -431,7 +431,7 @@ private static Dictionary<Type, IEdmType> AddTypes(this EdmModel model, EdmTypeM
431431
model.AddClrTypeAnnotations(edmTypes);
432432

433433
// add annotation for properties
434-
Dictionary<PropertyDescriptor, IEdmProperty> edmProperties = edmTypeMap.EdmProperties;
434+
Dictionary<MemberDescriptor, IEdmProperty> edmProperties = edmTypeMap.EdmProperties;
435435
model.AddClrPropertyInfoAnnotations(edmProperties);
436436
model.AddPropertyRestrictionsAnnotations(edmTypeMap.EdmPropertiesRestrictions);
437437
model.AddPropertiesQuerySettings(edmTypeMap.EdmPropertiesQuerySettings);
@@ -495,12 +495,12 @@ private static void AddClrTypeAnnotations(this EdmModel model, Dictionary<Type,
495495
}
496496
}
497497

498-
private static void AddClrPropertyInfoAnnotations(this EdmModel model, Dictionary<PropertyDescriptor, IEdmProperty> edmProperties)
498+
private static void AddClrPropertyInfoAnnotations(this EdmModel model, Dictionary<MemberDescriptor, IEdmProperty> edmProperties)
499499
{
500-
foreach (KeyValuePair<PropertyDescriptor, IEdmProperty> edmPropertyMap in edmProperties)
500+
foreach (KeyValuePair<MemberDescriptor, IEdmProperty> edmPropertyMap in edmProperties)
501501
{
502502
IEdmProperty edmProperty = edmPropertyMap.Value;
503-
PropertyDescriptor clrProperty = edmPropertyMap.Key;
503+
MemberDescriptor clrProperty = edmPropertyMap.Key;
504504
if (clrProperty.MethodInfo != null || edmProperty.Name != clrProperty.Name)
505505
{
506506
model.SetAnnotationValue(edmProperty, new ClrPropertyInfoAnnotation(clrProperty));

0 commit comments

Comments
 (0)