-
Notifications
You must be signed in to change notification settings - Fork 467
Expand file tree
/
Copy pathEntityTypeConfigurationOfTEntityType.cs
More file actions
162 lines (145 loc) · 7.01 KB
/
EntityTypeConfigurationOfTEntityType.cs
File metadata and controls
162 lines (145 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.OData.Edm;
namespace Microsoft.AspNet.OData.Builder
{
/// <summary>
/// Represents an <see cref="IEdmEntityType"/> that can be built using <see cref="ODataModelBuilder"/>.
/// </summary>
/// <typeparam name="TEntityType">The backing CLR type for this <see cref="IEdmEntityType"/>.</typeparam>
public class EntityTypeConfiguration<TEntityType> : StructuralTypeConfiguration<TEntityType> where TEntityType : class
{
private EntityTypeConfiguration _configuration;
private EntityCollectionConfiguration<TEntityType> _collection;
private ODataModelBuilder _modelBuilder;
/// <summary>
/// Initializes a new instance of <see cref="EntityTypeConfiguration"/>.
/// </summary>
/// <param name="modelBuilder">The <see cref="ODataModelBuilder"/> being used.</param>
internal EntityTypeConfiguration(ODataModelBuilder modelBuilder)
: this(modelBuilder, new EntityTypeConfiguration(modelBuilder, typeof(TEntityType)))
{
}
internal EntityTypeConfiguration(ODataModelBuilder modelBuilder, EntityTypeConfiguration configuration)
: base(configuration)
{
Contract.Assert(modelBuilder != null);
Contract.Assert(configuration != null);
_modelBuilder = modelBuilder;
_configuration = configuration;
_collection = new EntityCollectionConfiguration<TEntityType>(configuration);
}
/// <summary>
/// Gets the base type of this entity type.
/// </summary>
public EntityTypeConfiguration BaseType
{
get
{
return _configuration.BaseType;
}
}
/// <summary>
/// Gets the collection of <see cref="NavigationPropertyConfiguration"/> of this entity type.
/// </summary>
public IEnumerable<NavigationPropertyConfiguration> NavigationProperties
{
get { return _configuration.NavigationProperties; }
}
/// <summary>
/// Used to access a Collection of Entities through which you can configure
/// actions and functions that are bindable to EntityCollections.
/// </summary>
public EntityCollectionConfiguration<TEntityType> Collection
{
get { return _collection; }
}
/// <summary>
/// Marks this entity type as abstract.
/// </summary>
/// <returns>Returns itself so that multiple calls can be chained.</returns>
public EntityTypeConfiguration<TEntityType> Abstract()
{
_configuration.IsAbstract = true;
return this;
}
/// <summary>
/// Marks this entity type as media type.
/// </summary>
/// <returns>Returns itself so that multiple calls can be chained.</returns>
public EntityTypeConfiguration<TEntityType> MediaType()
{
_configuration.HasStream = true;
return this;
}
/// <summary>
/// Sets the base type of this entity type to <c>null</c> meaning that this entity type
/// does not derive from anything.
/// </summary>
/// <returns>Returns itself so that multiple calls can be chained.</returns>
public EntityTypeConfiguration<TEntityType> DerivesFromNothing()
{
_configuration.DerivesFromNothing();
return this;
}
/// <summary>
/// Sets the base type of this entity type.
/// </summary>
/// <typeparam name="TBaseType">The base entity type.</typeparam>
/// <returns>Returns itself so that multiple calls can be chained.</returns>
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "typeof(TBaseType) is used and getting it as a generic argument is cleaner")]
public EntityTypeConfiguration<TEntityType> DerivesFrom<TBaseType>() where TBaseType : class
{
EntityTypeConfiguration<TBaseType> baseEntityType = _modelBuilder.EntityType<TBaseType>();
_configuration.DerivesFrom(baseEntityType._configuration);
return this;
}
/// <summary>
/// Configures the key property(s) for this entity type.
/// </summary>
/// <typeparam name="TKey">The type of key.</typeparam>
/// <param name="keyDefinitionExpression">A lambda expression representing the property to be used as the primary key. For example, in C# t => t.Id and in Visual Basic .Net Function(t) t.Id.</param>
/// <returns>Returns itself so that multiple calls can be chained.</returns>
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "Nested generic appropriate here")]
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Explicit Expression generic type is more clear")]
public EntityTypeConfiguration<TEntityType> HasKey<TKey>(Expression<Func<TEntityType, TKey>> keyDefinitionExpression)
{
ICollection<MemberInfo> properties = PropertySelectorVisitor.GetSelectedProperties(keyDefinitionExpression, false);
foreach (PropertyInfo property in properties)
{
_configuration.HasKey(property);
}
return this;
}
/// <summary>
/// Create an Action that binds to this EntityType.
/// </summary>
/// <param name="name">The name of the action.</param>
/// <returns>The ActionConfiguration to allow further configuration of the new Action.</returns>
public ActionConfiguration Action(string name)
{
Contract.Assert(_configuration != null && _configuration.ModelBuilder != null);
ActionConfiguration action = _configuration.ModelBuilder.Action(name);
action.SetBindingParameter(BindingParameterConfiguration.DefaultBindingParameterName, _configuration);
return action;
}
/// <summary>
/// Create a Function that binds to this EntityType.
/// </summary>
/// <param name="name">The name of the function.</param>
/// <returns>The FunctionConfiguration to allow further configuration of the new Function.</returns>
public FunctionConfiguration Function(string name)
{
Contract.Assert(_configuration != null && _configuration.ModelBuilder != null);
FunctionConfiguration function = _configuration.ModelBuilder.Function(name);
function.SetBindingParameter(BindingParameterConfiguration.DefaultBindingParameterName, _configuration);
return function;
}
}
}