本文整理汇总了C#中Microsoft.OData.Edm.Library.EdmModel.FindDeclaredType方法的典型用法代码示例。如果您正苦于以下问题:C# EdmModel.FindDeclaredType方法的具体用法?C# EdmModel.FindDeclaredType怎么用?C# EdmModel.FindDeclaredType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.OData.Edm.Library.EdmModel
的用法示例。
在下文中一共展示了EdmModel.FindDeclaredType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateEntityInstanceTestDescriptors
/// <summary>
/// Creates a set of interesting entity instances along with metadata.
/// </summary>
/// <param name="settings">The test descriptor settings to use.</param>
/// <param name="model">If non-null, the method creates complex types for the complex values and adds them to the model.</param>
/// <param name="withTypeNames">true if the payloads should specify type names.</param>
/// <returns>List of test descriptors with interesting entity instances as payload.</returns>
public static IEnumerable<PayloadTestDescriptor> CreateEntityInstanceTestDescriptors(
EdmModel model,
bool withTypeNames)
{
IEnumerable<PrimitiveValue> primitiveValues = TestValues.CreatePrimitiveValuesWithMetadata(fullSet: false);
IEnumerable<ComplexInstance> complexValues = TestValues.CreateComplexValues(model, withTypeNames, fullSet: false);
IEnumerable<NamedStreamInstance> streamReferenceValues = TestValues.CreateStreamReferenceValues(fullSet: false);
IEnumerable<PrimitiveMultiValue> primitiveMultiValues = TestValues.CreatePrimitiveCollections(withTypeNames, fullSet: false);
IEnumerable<ComplexMultiValue> complexMultiValues = TestValues.CreateComplexCollections(model, withTypeNames, fullSet: false);
IEnumerable<NavigationPropertyInstance> navigationProperties = TestValues.CreateDeferredNavigationLinks();
// NOTE we have to copy the EntityModelTypeAnnotation on the primitive value to the NullPropertyInstance for null values since the
// NullPropertyInstance does not expose a value. We will later copy it back to the value we generate for the null property.
IEnumerable<PropertyInstance> primitiveProperties =
primitiveValues.Select((pv, ix) => PayloadBuilder.Property("PrimitiveProperty" + ix, pv).CopyAnnotation<PropertyInstance, EntityModelTypeAnnotation>(pv));
IEnumerable<PropertyInstance> complexProperties = complexValues.Select((cv, ix) => PayloadBuilder.Property("ComplexProperty" + ix, cv));
IEnumerable<PropertyInstance> primitiveMultiValueProperties = primitiveMultiValues.Select((pmv, ix) => PayloadBuilder.Property("PrimitiveMultiValueProperty" + ix, pmv));
IEnumerable<PropertyInstance> complexMultiValueProperties = complexMultiValues.Select((cmv, ix) => PayloadBuilder.Property("ComplexMultiValueProperty" + ix, cmv));
PropertyInstance[][] propertyMatrix = new PropertyInstance[6][];
propertyMatrix[0] = primitiveProperties.ToArray();
propertyMatrix[1] = complexProperties.ToArray();
propertyMatrix[2] = streamReferenceValues.ToArray();
propertyMatrix[3] = primitiveMultiValueProperties.ToArray();
propertyMatrix[4] = complexMultiValueProperties.ToArray();
propertyMatrix[5] = navigationProperties.ToArray();
IEnumerable<PropertyInstance[]> propertyCombinations = propertyMatrix.ColumnCombinations(0, 1, 6);
int count = 0;
foreach (PropertyInstance[] propertyCombination in propertyCombinations)
{
// build the entity type, add it to the model
EdmEntityType generatedEntityType = null;
string typeName = "PGEntityType" + count;
EdmEntityContainer container = null;
EdmEntitySet entitySet = null;
if (model != null)
{
// generate a new type with the auto-generated name, check that no type with this name exists and add the default key property to it.
Debug.Assert(model.FindDeclaredType(typeName) == null, "Entity type '" + typeName + "' already exists.");
generatedEntityType = new EdmEntityType("TestModel", typeName);
generatedEntityType.AddKeys(generatedEntityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));
model.AddElement(generatedEntityType);
container = model.EntityContainer as EdmEntityContainer;
if (container == null)
{
container = new EdmEntityContainer("TestModel", "DefaultNamespace");
model.AddElement(container);
}
entitySet = container.AddEntitySet(typeName, generatedEntityType);
}
EntityInstance entityInstance = PayloadBuilder.Entity("TestModel." + typeName)
.Property("Id", PayloadBuilder.PrimitiveValue(count).WithTypeAnnotation(EdmCoreModel.Instance.GetInt32(false)));
for (int i = 0; i < propertyCombination.Length; ++i)
{
PropertyInstance currentProperty = propertyCombination[i];
entityInstance.Add(currentProperty);
if (model != null)
{
if (entitySet == null)
{
entitySet = container.FindEntitySet(typeName) as EdmEntitySet;
}
switch (currentProperty.ElementType)
{
case ODataPayloadElementType.ComplexProperty:
ComplexProperty complexProperty = (ComplexProperty)currentProperty;
generatedEntityType.AddStructuralProperty(complexProperty.Name,
complexProperty.Value.GetAnnotation<EntityModelTypeAnnotation>().EdmModelType);
break;
case ODataPayloadElementType.PrimitiveProperty:
PrimitiveProperty primitiveProperty = (PrimitiveProperty)currentProperty;
if (primitiveProperty.Value == null)
{
generatedEntityType.AddStructuralProperty(
primitiveProperty.Name,
PayloadBuilder.PrimitiveValueType(null));
}
else
{
generatedEntityType.AddStructuralProperty(primitiveProperty.Name,
primitiveProperty.Value.GetAnnotation<EntityModelTypeAnnotation>().EdmModelType);
}
break;
case ODataPayloadElementType.NamedStreamInstance:
//.........这里部分代码省略.........
示例2: GetEnumModel
private static IEdmModel GetEnumModel()
{
EdmModel model = new EdmModel();
EdmEnumType enumType = new EdmEnumType("TestModel", "TestEnum");
enumType.AddMember(new EdmEnumMember(enumType, "FirstValue", new EdmIntegerConstant(0)));
enumType.AddMember(new EdmEnumMember(enumType, "FirstValue", new EdmIntegerConstant(1)));
model.AddElement(enumType);
model.SetAnnotationValue(model.FindDeclaredType("TestModel.TestEnum"), new ClrTypeAnnotation(typeof(TestEnum)));
return model;
}
示例3: GetComplexInstanceCollection
private static ComplexInstanceCollection GetComplexInstanceCollection(IRandomNumberGenerator random, EdmModel model = null, ODataVersion version = ODataVersion.V4)
{
var complex = GetComplexInstance(random, model, version);
int numinstances = random.ChooseFrom(new[] { 0, 1, 3 });
var payload = new ComplexInstanceCollection(GenerateSimilarComplexInstances(random, complex, numinstances).ToArray());
if (model != null)
{
var container = model.EntityContainersAcrossModels().Single() as EdmEntityContainer;
var collectionType = new EdmCollectionType((model.FindDeclaredType(complex.FullTypeName) as EdmComplexType).ToTypeReference());
var function = new EdmFunction(container.Namespace, "GetComplexInstances", collectionType.ToTypeReference());
var functionImport = container.AddFunctionImport("GetComplexInstances", function);
payload.AddAnnotation(new FunctionAnnotation() { FunctionImport = functionImport });
payload.AddAnnotation(new DataTypeAnnotation() { EdmDataType = collectionType });
}
return payload;
}
示例4: OnModelExtending
internal static EdmModel OnModelExtending(EdmModel model)
{
var ns = model.DeclaredNamespaces.First();
var product = model.FindDeclaredType(ns + "." + "Product");
var products = EdmCoreModel.GetCollection(product.GetEdmTypeReference(isNullable: false));
var mostExpensive = new EdmFunction(ns, "MostExpensive",
EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Double, isNullable: false), isBound: true,
entitySetPathExpression: null, isComposable: false);
mostExpensive.AddParameter("bindingParameter", products);
model.AddElement(mostExpensive);
return model;
}
示例5: GetComplexProperty
private static ComplexProperty GetComplexProperty(IRandomNumberGenerator random, EdmModel model = null, ODataVersion version = ODataVersion.V4)
{
var instance = GetComplexInstance(random, model, version);
var property = new ComplexProperty(instance.FullTypeName, instance);
property.WithTypeAnnotation(model.FindDeclaredType(instance.FullTypeName));
return property;
}
示例6: GetPrimitiveValue
private static PrimitiveValue GetPrimitiveValue(IRandomNumberGenerator random, EdmModel model = null)
{
var payload = random.ChooseFrom(TestValues.CreatePrimitiveValuesWithMetadata(true));
if (model != null)
{
EdmEntityType entity = model.FindDeclaredType("AllPrimitiveTypesEntity") as EdmEntityType;
if (entity == null)
{
entity = new EdmEntityType("TestModel", "AllPrimitiveTypesEntity");
entity.AddStructuralProperty("StringPropNullable", EdmPrimitiveTypeKind.String, true);
entity.AddStructuralProperty("StringProp", EdmPrimitiveTypeKind.String, false);
entity.AddStructuralProperty("Int32PropNullable", EdmPrimitiveTypeKind.Int32, true);
entity.AddStructuralProperty("Int32Prop", EdmPrimitiveTypeKind.Int32, false);
entity.AddStructuralProperty("BooleanPropNullable", EdmPrimitiveTypeKind.Boolean, true);
entity.AddStructuralProperty("BooleanProp", EdmPrimitiveTypeKind.Boolean, false);
entity.AddStructuralProperty("BytePropNullable", EdmPrimitiveTypeKind.Byte, true);
entity.AddStructuralProperty("ByteProp", EdmPrimitiveTypeKind.Byte, false);
entity.AddStructuralProperty("SBytePropNullable", EdmPrimitiveTypeKind.SByte, true);
entity.AddStructuralProperty("SByteProp", EdmPrimitiveTypeKind.SByte, false);
entity.AddStructuralProperty("Int16PropNullable", EdmPrimitiveTypeKind.Int16, true);
entity.AddStructuralProperty("Int16Prop", EdmPrimitiveTypeKind.Int16, false);
entity.AddStructuralProperty("DecimalPropNullable", EdmPrimitiveTypeKind.Decimal, true);
entity.AddStructuralProperty("DecimalProp", EdmPrimitiveTypeKind.Decimal, false);
entity.AddStructuralProperty("SinglePropNullable", EdmPrimitiveTypeKind.Single, true);
entity.AddStructuralProperty("SingleProp", EdmPrimitiveTypeKind.Single, false);
entity.AddStructuralProperty("Int64PropNullable", EdmPrimitiveTypeKind.Int64, true);
entity.AddStructuralProperty("Int64Prop", EdmPrimitiveTypeKind.Int64, false);
entity.AddStructuralProperty("DoublePropNullable", EdmPrimitiveTypeKind.Double, true);
entity.AddStructuralProperty("DoubleProp", EdmPrimitiveTypeKind.Double, false);
entity.AddStructuralProperty("BinaryPropNullable", EdmPrimitiveTypeKind.Binary, true);
entity.AddStructuralProperty("BinaryProp", EdmPrimitiveTypeKind.Binary, false);
entity.AddStructuralProperty("GuidPropNullable", EdmPrimitiveTypeKind.Guid, true);
entity.AddStructuralProperty("GuidProp", EdmPrimitiveTypeKind.Guid, false);
model.AddElement(entity);
}
payload.WithTypeAnnotation(entity);
}
return payload;
}
示例7: CreateEntitySetTestDescriptors
/// <summary>
/// Creates a set of interesting entity set instances along with metadata.
/// </summary>
/// <param name="settings">The test descriptor settings to use.</param>
/// <param name="model">If non-null, the method creates types as needed and adds them to the model.</param>
/// <param name="withTypeNames">true if the payloads should specify type names.</param>
/// <returns>List of test descriptors with interesting entity instances as payload.</returns>
public static IEnumerable<PayloadTestDescriptor> CreateEntitySetTestDescriptors(
EdmModel model,
bool withTypeNames)
{
EdmEntityType personType = null;
EdmComplexType carType = null;
var container = model.EntityContainer as EdmEntityContainer;
if (container == null)
{
container = new EdmEntityContainer("TestModel", "DefaultNamespace");
model.AddElement(container);
}
if (model != null)
{
personType = model.FindDeclaredType("TestModel.TFPerson") as EdmEntityType;
carType = model.FindDeclaredType("TestModel.TFCar") as EdmComplexType;
// Create the metadata types for the entity instance used in the entity set
if (carType == null)
{
carType = new EdmComplexType("TestModel", "TFCar");
carType.AddStructuralProperty("Make", EdmPrimitiveTypeKind.String);
carType.AddStructuralProperty("Color", EdmPrimitiveTypeKind.String);
model.AddElement(carType);
}
if (personType == null)
{
personType = new EdmEntityType("TestModel", "TFPerson");
var keyProperty = personType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32);
personType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
personType.AddStructuralProperty("Car", carType.ToTypeReference());
personType.AddKeys(keyProperty);
model.AddElement(personType);
container.AddEntitySet("TFPerson", personType);
}
}
ComplexInstance carInstance = PayloadBuilder.ComplexValue(withTypeNames ? "TestModel.TFCar" : null)
.Property("Make", PayloadBuilder.PrimitiveValue("Ford"))
.Property("Color", PayloadBuilder.PrimitiveValue("Blue"));
EntityInstance personInstance = PayloadBuilder.Entity(withTypeNames ? "TestModel.TFPerson" : null)
.Property("Id", PayloadBuilder.PrimitiveValue(1))
.Property("Name", PayloadBuilder.PrimitiveValue("John Doe"))
.Property("Car", carInstance);
// empty feed
yield return new PayloadTestDescriptor() { PayloadElement = PayloadBuilder.EntitySet().WithTypeAnnotation(personType), PayloadEdmModel = model };
// entity set with a single entity
yield return new PayloadTestDescriptor() { PayloadElement = PayloadBuilder.EntitySet().Append(personInstance).WithTypeAnnotation(personType), PayloadEdmModel = model };
// entity set with the person instance in the middle
yield return new PayloadTestDescriptor()
{
PayloadElement = PayloadBuilder.EntitySet()
.Append(personInstance.GenerateSimilarEntries(2))
.Append(personInstance)
.Append(personInstance.GenerateSimilarEntries(1))
.WithTypeAnnotation(personType),
PayloadEdmModel = model
};
// entity set with a single entity and a next link
yield return new PayloadTestDescriptor()
{
PayloadElement = PayloadBuilder.EntitySet().Append(personInstance).NextLink(nextLink).WithTypeAnnotation(personType),
PayloadEdmModel = model,
SkipTestConfiguration = tc => tc.IsRequest
};
// entity set with a single entity and inline count
yield return new PayloadTestDescriptor()
{
PayloadElement = PayloadBuilder.EntitySet().Append(personInstance).InlineCount(1).WithTypeAnnotation(personType),
PayloadEdmModel = model,
SkipTestConfiguration = tc => tc.IsRequest
};
// entity set with a single entity, a next link and inline count
yield return new PayloadTestDescriptor()
{
PayloadElement = PayloadBuilder.EntitySet().Append(personInstance).NextLink(nextLink).InlineCount(1).WithTypeAnnotation(personType),
PayloadEdmModel = model,
SkipTestConfiguration = tc => tc.IsRequest
};
// entity set with a single entity, a next link and a negative inline count
yield return new PayloadTestDescriptor()
{
PayloadElement = PayloadBuilder.EntitySet().Append(personInstance).NextLink(nextLink).InlineCount(-1).WithTypeAnnotation(personType),
PayloadEdmModel = model,
SkipTestConfiguration = tc => tc.IsRequest
};
// entity set containing many entities of types derived from the same base
//yield return new PayloadTestDescriptor()
//{
//.........这里部分代码省略.........
示例8: CreateBatchRequestTestDescriptors
/// <summary>
/// Creates several PayloadTestDescriptors containing Batch Requests
/// </summary>
/// <param name="requestManager">Used for building the requests</param>
/// <param name="model">The model to use for adding additional types.</param>
/// <param name="withTypeNames">Whether or not to use full type names.</param>
/// <returns>PayloadTestDescriptors</returns>
public static IEnumerable<PayloadTestDescriptor> CreateBatchRequestTestDescriptors(
IODataRequestManager requestManager,
EdmModel model,
bool withTypeNames = false)
{
ExceptionUtilities.CheckArgumentNotNull(requestManager, "requestManager");
EdmEntityType personType = null;
EdmComplexType carType = null;
EdmEntitySet personsEntitySet = null;
EdmEntityContainer container = model.EntityContainer as EdmEntityContainer;
if (model != null)
{
//TODO: Clone EdmModel
//model = model.Clone();
if (container == null)
{
container = new EdmEntityContainer("TestModel", "DefaultContainer");
model.AddElement(container);
}
personType = model.FindDeclaredType("TestModel.TFPerson") as EdmEntityType;
carType = model.FindDeclaredType("TestModel.TFCar") as EdmComplexType;
// Create the metadata types for the entity instance used in the entity set
if (carType == null)
{
carType = new EdmComplexType("TestModel", "TFCar");
model.AddElement(carType);
carType.AddStructuralProperty("Make", EdmPrimitiveTypeKind.String, true);
carType.AddStructuralProperty("Color", EdmPrimitiveTypeKind.String, true);
}
if (personType == null)
{
personType = new EdmEntityType("TestModel", "TFPerson");
model.AddElement(personType);
personType.AddKeys(personType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));
personType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String, true);
personType.AddStructuralProperty("Car", carType.ToTypeReference());
container.AddEntitySet("Customers", personType);
}
personsEntitySet = container.AddEntitySet("People", personType);
}
ComplexInstance carInstance = PayloadBuilder.ComplexValue(withTypeNames ? "TestModel.TFCar" : null)
.Property("Make", PayloadBuilder.PrimitiveValue("Ford"))
.Property("Color", PayloadBuilder.PrimitiveValue("Blue"));
ComplexProperty carProperty = (ComplexProperty)PayloadBuilder.Property("Car", carInstance)
.WithTypeAnnotation(personType);
EntityInstance personInstance = PayloadBuilder.Entity(withTypeNames ? "TestModel.TFPerson" : null)
.Property("Id", PayloadBuilder.PrimitiveValue(1))
.Property("Name", PayloadBuilder.PrimitiveValue("John Doe"))
.Property("Car", carInstance)
.WithTypeAnnotation(personType);
var carPropertyPayload = new PayloadTestDescriptor()
{
PayloadElement = carProperty,
PayloadEdmModel = model
};
var emptyPayload = new PayloadTestDescriptor()
{
PayloadEdmModel = CreateEmptyEdmModel()
};
var personPayload = new PayloadTestDescriptor()
{
PayloadElement = personInstance,
PayloadEdmModel = model
};
var root = ODataUriBuilder.Root(new Uri("http://www.odata.org/service.svc"));
var entityset = ODataUriBuilder.EntitySet(personsEntitySet);
// Get operations
var queryOperation1 = emptyPayload.InRequestOperation(HttpVerb.Get, new ODataUri(new ODataUriSegment[] { root }), requestManager);
var queryOperation2 = emptyPayload.InRequestOperation(HttpVerb.Get, new ODataUri(new ODataUriSegment[] { root }), requestManager);
// Post operation containing a complex property
var postOperation = carPropertyPayload.InRequestOperation(HttpVerb.Post, new ODataUri(new ODataUriSegment[] {root, entityset}) , requestManager, MimeTypes.ApplicationJsonLight);
// Delete operation with no payload
var deleteOperation = emptyPayload.InRequestOperation(HttpVerb.Delete, new ODataUri(new ODataUriSegment[] { root, entityset }), requestManager);
// Put operation where the payload is an EntityInstance
var putOperation = personPayload.InRequestOperation(HttpVerb.Put, new ODataUri(new ODataUriSegment[] { root, entityset }), requestManager);
// A changeset containing a delete with no payload and a put
var twoOperationsChangeset = BatchUtils.GetRequestChangeset(new IMimePart[] { postOperation, deleteOperation }, requestManager);
// A changeset containing a delete with no payload
//.........这里部分代码省略.........
示例9: CreateComplexCollections
/// <summary>
/// Creates a set of interesting collections with complex items.
/// </summary>
/// <param name="model">The model to add complex types to.</param>
/// <param name="withTypeNames">true if the complex value payloads should specify type names.</param>
/// <param name="fullSet">true if all available complex collections should be returned, false if only the most interesting subset should be returned.</param>
/// <returns>List of interesting collections.</returns>
public static IEnumerable<ComplexMultiValue> CreateComplexCollections(EdmModel model, bool withTypeNames, bool fullSet = true)
{
List<ComplexMultiValue> results = new List<ComplexMultiValue>();
string typeName;
ComplexMultiValue complexCollection;
EdmComplexType complexType = null;
// Empty complex collection without model or typename cannot be recognized and will be treated as a primitive collection
// (note that this is done by test infrastructure, not by the product, the product will simply report empty collection).
// TODO: What about empty complex collection with type name but without model?
typeName = "ComplexTypeForEmptyCollection";
if (model != null)
{
complexType = model.FindDeclaredType(GetFullTypeName(typeName)) as EdmComplexType;
if (complexType == null)
{
complexType = new EdmComplexType(NamespaceName, typeName);
model.AddElement(complexType);
}
complexCollection = PayloadBuilder.ComplexMultiValue(withTypeNames ? EntityModelUtils.GetCollectionTypeName(GetFullTypeName(typeName)) : null);
complexCollection.WithTypeAnnotation(EdmCoreModel.GetCollection(complexType.ToTypeReference()).CollectionDefinition());
if (!withTypeNames)
{
complexCollection.AddAnnotation(new SerializationTypeNameTestAnnotation() { TypeName = null });
}
results.Add(complexCollection);
}
// Collection with multiple complex items with property
typeName = "ComplexTypeForMultipleItemsCollection";
if (model != null)
{
complexType = model.FindDeclaredType(GetFullTypeName(typeName)) as EdmComplexType;
if (complexType == null)
{
complexType = new EdmComplexType(NamespaceName, typeName);
complexType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
model.AddElement(complexType);
}
}
List<ComplexInstance> instanceList = new List<ComplexInstance>()
{
PayloadBuilder.ComplexValue(withTypeNames ? GetFullTypeName(typeName) : null)
.PrimitiveProperty("Name", "Bart")
.WithTypeAnnotation(complexType),
PayloadBuilder.ComplexValue(withTypeNames ? GetFullTypeName(typeName) : null)
.PrimitiveProperty("Name", "Homer")
.WithTypeAnnotation(complexType),
PayloadBuilder.ComplexValue(withTypeNames ? GetFullTypeName(typeName) : null)
.PrimitiveProperty("Name", "Marge")
.WithTypeAnnotation(complexType)
};
if (!withTypeNames)
{
foreach (var item in instanceList)
{
item.AddAnnotation(new SerializationTypeNameTestAnnotation() { TypeName = null });
}
}
complexCollection = PayloadBuilder.ComplexMultiValue(withTypeNames ? EntityModelUtils.GetCollectionTypeName(GetFullTypeName(typeName)) : null);
complexCollection.Items(instanceList);
complexCollection.WithTypeAnnotation(complexType == null ? null : EdmCoreModel.GetCollection(new EdmComplexTypeReference(complexType, false)));
if (!withTypeNames)
{
complexCollection.AddAnnotation(new SerializationTypeNameTestAnnotation() { TypeName = null });
}
results.Add(complexCollection);
// Collection with multiple complex items with properties of various types
if (fullSet)
{
typeName = "ComplexTypeForMultipleComplexItemsCollection";
string innerTypeName = "InnerComplexTypeForMultipleComplexItemsCollection";
EdmComplexType innerComplexType = null;
if (model != null)
{
innerComplexType = model.FindDeclaredType(GetFullTypeName(innerTypeName)) as EdmComplexType;
if (innerComplexType == null)
{
innerComplexType = new EdmComplexType(NamespaceName, innerTypeName);
innerComplexType.AddStructuralProperty("Include", EdmPrimitiveTypeKind.Boolean);
model.AddElement(innerComplexType);
}
complexType = model.FindDeclaredType(GetFullTypeName(typeName)) as EdmComplexType;
//.........这里部分代码省略.........
示例10: CreateHomogeneousCollectionValues
/// <summary>
/// Creates a set of interesting homogeneous collection values with primitive and complex items.
/// </summary>
/// <param name="model">The model to add complex types to.</param>
/// <param name="withTypeNames">true if the collection and complex value payloads should specify type names.</param>
/// <param name="withExpectedType">true if an expected type annotation should be added to the generated payload element; otherwise false.</param>
/// <param name="withcollectionName">true if the collection is not in the top level, otherwise false</param>
/// <param name="fullSet">true if all available collection values should be returned, false if only the most interesting subset should be returned.</param>
/// <returns>List of interesting collection values.</returns>
public static IEnumerable<ODataPayloadElementCollection> CreateHomogeneousCollectionValues(
EdmModel model,
bool withTypeNames,
bool withExpectedType,
bool withcollectionName,
bool fullset = true)
{
IEdmTypeReference itemTypeAnnotationType = null;
IEdmTypeReference collectionTypeAnnotationType = null;
EdmOperationImport primitiveCollectionFunctionImport = null;
EdmEntityContainer defaultContainer = null;
if (model != null)
{
defaultContainer = model.FindEntityContainer("TestModel.TestContainer") as EdmEntityContainer;
if (defaultContainer == null)
{
defaultContainer = new EdmEntityContainer("TestModel", "TestContainer");
model.AddElement(defaultContainer);
}
itemTypeAnnotationType = EdmCoreModel.Instance.GetString(true);
collectionTypeAnnotationType = EdmCoreModel.GetCollection(itemTypeAnnotationType);
var function = new EdmFunction("TestModel", "PrimitiveCollectionFunctionImport", collectionTypeAnnotationType);
model.AddElement(function);
primitiveCollectionFunctionImport = defaultContainer.AddFunctionImport("PrimitiveCollectionFunctionImport", function);
}
// primitive collection with single null item
yield return new PrimitiveCollection(PayloadBuilder.PrimitiveValue(null).WithTypeAnnotation(itemTypeAnnotationType))
.WithTypeAnnotation(collectionTypeAnnotationType)
.ExpectedCollectionItemType(withExpectedType ? itemTypeAnnotationType : null)
.ExpectedFunctionImport(withExpectedType ? primitiveCollectionFunctionImport : null)
.CollectionName(withcollectionName ? "PrimitiveCollectionFunctionImport" : null);
// primitive collection with multiple items (same type)
yield return new PrimitiveCollection(
PayloadBuilder.PrimitiveValue("Vienna").WithTypeAnnotation(itemTypeAnnotationType),
PayloadBuilder.PrimitiveValue("Prague").WithTypeAnnotation(itemTypeAnnotationType),
PayloadBuilder.PrimitiveValue("Redmond").WithTypeAnnotation(itemTypeAnnotationType)
)
.WithTypeAnnotation(collectionTypeAnnotationType)
.ExpectedCollectionItemType(withExpectedType ? itemTypeAnnotationType : null)
.ExpectedFunctionImport(withExpectedType ? primitiveCollectionFunctionImport : null)
.CollectionName(withcollectionName ? "PrimitiveCollectionFunctionImport" : null);
if (fullset)
{
// empty primitive collection
yield return new PrimitiveCollection()
.WithTypeAnnotation(collectionTypeAnnotationType)
.ExpectedCollectionItemType(withExpectedType ? itemTypeAnnotationType : null)
.ExpectedFunctionImport(withExpectedType ? primitiveCollectionFunctionImport : null)
.CollectionName(withcollectionName ? "PrimitiveCollectionFunctionImport" : null);
// primitive collection with a single item
yield return new PrimitiveCollection(
PayloadBuilder.PrimitiveValue("Vienna").WithTypeAnnotation(itemTypeAnnotationType)
).WithTypeAnnotation(collectionTypeAnnotationType)
.ExpectedCollectionItemType(withExpectedType ? itemTypeAnnotationType : null)
.ExpectedFunctionImport(withExpectedType ? primitiveCollectionFunctionImport : null)
.CollectionName(withcollectionName ? "PrimitiveCollectionFunctionImport" : null);
// primitive collection with multiple null items
yield return new PrimitiveCollection(
PayloadBuilder.PrimitiveValue(null).WithTypeAnnotation(itemTypeAnnotationType),
PayloadBuilder.PrimitiveValue(null).WithTypeAnnotation(itemTypeAnnotationType),
PayloadBuilder.PrimitiveValue(null).WithTypeAnnotation(itemTypeAnnotationType)
).WithTypeAnnotation(collectionTypeAnnotationType)
.ExpectedCollectionItemType(withExpectedType ? itemTypeAnnotationType : null)
.ExpectedFunctionImport(withExpectedType ? primitiveCollectionFunctionImport : null)
.CollectionName(withcollectionName ? "PrimitiveCollectionFunctionImport" : null);
}
string localPersonTypeName = "ComplexCollectionPersonItemType";
string personTypeName = GetFullTypeName(localPersonTypeName);
EdmOperationImport complexCollectionFunctionImport = null;
if (model != null)
{
EdmComplexType complexItemType = model.FindDeclaredType(personTypeName) as EdmComplexType;
if (complexItemType == null)
{
complexItemType = new EdmComplexType(NamespaceName, localPersonTypeName);
complexItemType.AddStructuralProperty("FirstName", EdmCoreModel.Instance.GetString(true));
complexItemType.AddStructuralProperty("LastName", EdmCoreModel.Instance.GetString(true));
model.AddElement(complexItemType);
}
itemTypeAnnotationType = complexItemType.ToTypeReference();
collectionTypeAnnotationType = EdmCoreModel.GetCollection(itemTypeAnnotationType);
//.........这里部分代码省略.........
示例11: CreateComplexValues
/// <summary>
/// Creates a set of interesting complex values along with metadata.
/// </summary>
/// <param name="model">If non-null, the method creates complex types for the complex values and adds them to the model.</param>
/// <param name="withTypeNames">true if the complex value payloads should specify type names.</param>
/// <param name="fullSet">true if all available complex values should be returned, false if only the most interesting subset should be returned.</param>
/// <returns>List of interesting complex values.</returns>
public static IEnumerable<ComplexInstance> CreateComplexValues(EdmModel model, bool withTypeNames, bool fullSet = true)
{
var complexValuesList = new List<ComplexInstance>();
EdmComplexType complexType = null;
EdmComplexType innerComplexType = null;
string typeName;
ComplexInstance complexValue;
// Null complex value
if (fullSet)
{
typeName = "NullComplexType";
// Can't specify type name for a null complex value since the reader will not read it (in JSON it's not even in the payload anywhere)
complexValue = new ComplexInstance(null, true);
complexValuesList.Add(complexValue);
if (model != null)
{
complexType = (EdmComplexType)model.FindDeclaredType(GetFullTypeName(typeName));
if (complexType == null)
{
complexType = new EdmComplexType(NamespaceName, typeName);
model.AddElement(complexType);
}
complexValue.WithTypeAnnotation(new EdmComplexTypeReference(complexType, true));
}
}
if (fullSet)
{
// Complex value with one number property
typeName = "ComplexTypeWithNumberProperty";
complexValue = PayloadBuilder.ComplexValue(withTypeNames ? GetFullTypeName(typeName) : null)
.PrimitiveProperty("numberProperty", 42);
complexValuesList.Add(complexValue);
if (model != null)
{
complexType = model.FindDeclaredType(GetFullTypeName(typeName)) as EdmComplexType;
if (complexType == null)
{
complexType = new EdmComplexType(NamespaceName, typeName);
complexType.AddStructuralProperty("numberProperty", EdmPrimitiveTypeKind.Int32);
model.AddElement(complexType);
}
complexValue.WithTypeAnnotation(complexType);
}
}
// Complex value with three properties
typeName = "ComplexTypeWithNumberStringAndNullProperty";
complexValue = PayloadBuilder.ComplexValue(withTypeNames ? GetFullTypeName(typeName) : null)
.PrimitiveProperty("number", 42)
.PrimitiveProperty("string", "some")
.PrimitiveProperty("null", null);
complexValuesList.Add(complexValue);
if (model != null)
{
complexType = model.FindDeclaredType(GetFullTypeName(typeName)) as EdmComplexType;
if (complexType == null)
{
complexType = new EdmComplexType(NamespaceName, typeName);
complexType.AddStructuralProperty("number", EdmPrimitiveTypeKind.Int32);
complexType.AddStructuralProperty("string", EdmPrimitiveTypeKind.String);
complexType.AddStructuralProperty("null", EdmPrimitiveTypeKind.String);
model.AddElement(complexType);
}
complexValue.WithTypeAnnotation(complexType);
}
// Complex value with primitive and complex property
typeName = "ComplexTypeWithPrimitiveAndComplexProperty";
if (model != null)
{
innerComplexType = model.FindDeclaredType(GetFullTypeName("InnerComplexTypeWithStringProperty")) as EdmComplexType;
if (innerComplexType == null)
{
innerComplexType = new EdmComplexType(NamespaceName, "InnerComplexTypeWithStringProperty");
innerComplexType.AddStructuralProperty("foo", EdmPrimitiveTypeKind.String);
model.AddElement(innerComplexType);
}
complexType = model.FindDeclaredType(GetFullTypeName(typeName)) as EdmComplexType;
if (complexType == null)
{
complexType = new EdmComplexType(NamespaceName, typeName);
complexType.AddStructuralProperty("number", EdmPrimitiveTypeKind.Int32);
complexType.AddStructuralProperty("complex", innerComplexType.ToTypeReference());
model.AddElement(complexType);
}
//.........这里部分代码省略.........
示例12: SpatialPropertyErrorTest
public void SpatialPropertyErrorTest()
{
EdmModel model = new EdmModel();
var container = new EdmEntityContainer("TestModel", "DefaultContainer");
model.AddElement(container);
var owningType = new EdmEntityType("TestModel", "OwningType");
owningType.AddKeys(owningType.AddStructuralProperty("ID", EdmCoreModel.Instance.GetInt32(false)));
owningType.AddStructuralProperty("TopLevelProperty", EdmCoreModel.Instance.GetSpatial(EdmPrimitiveTypeKind.GeographyPoint, true));
model.AddElement(owningType);
IEdmEntityType edmOwningType = (IEdmEntityType)model.FindDeclaredType("TestModel.OwningType");
IEdmStructuralProperty edmTopLevelProperty = (IEdmStructuralProperty)edmOwningType.FindProperty("TopLevelProperty");
PayloadReaderTestDescriptor.ReaderMetadata readerMetadata = new PayloadReaderTestDescriptor.ReaderMetadata(edmTopLevelProperty);
string pointValue = SpatialUtils.GetSpatialStringValue(ODataFormat.Json, GeographyFactory.Point(33.1, -110.0).Build(), "Edm.GeographyPoint");
var explicitPayloads = new[]
{
new
{
Description = "Spatial value with type information and odata.type annotation - should fail.",
Json = "{ " +
"\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata#Edm.GeographyPoint\", " +
"\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataTypeAnnotationName + "\":\"Edm.GeographyPoint\"," +
"\"" + JsonLightConstants.ODataValuePropertyName + "\":" + pointValue +
"}",
ReaderMetadata = readerMetadata,
ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightPropertyAndValueDeserializer_ODataTypeAnnotationInPrimitiveValue", JsonLightConstants.ODataTypeAnnotationName)
},
new
{
Description = "Spatial value with odata.type annotation - should fail.",
Json = "{ " +
"\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata#Edm.GeographyPoint\", " +
"\"" + JsonLightConstants.ODataValuePropertyName + "\":" + pointValue +
"}",
ReaderMetadata = readerMetadata,
ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightPropertyAndValueDeserializer_ODataTypeAnnotationInPrimitiveValue", JsonLightConstants.ODataTypeAnnotationName)
},
};
var testDescriptors = explicitPayloads.Select(payload =>
{
return new NativeInputReaderTestDescriptor()
{
DebugDescription = payload.Description,
PayloadKind = ODataPayloadKind.Property,
InputCreator = (tc) =>
{
return payload.Json;
},
PayloadEdmModel = model,
// We use payload expected result just as a convenient way to run the reader for the Property payload kind
// since the reading should always fail, we don't need anything to compare the results to.
ExpectedResultCallback = (tc) =>
new PayloadReaderTestExpectedResult(this.Settings.ExpectedResultSettings)
{
ReaderMetadata = payload.ReaderMetadata,
ExpectedException = payload.ExpectedException,
},
};
});
this.CombinatorialEngineProvider.RunCombinations(
testDescriptors,
this.ReaderTestConfigurationProvider.JsonLightFormatConfigurations,
(testDescriptor, testConfiguration) =>
{
testDescriptor.RunTest(testConfiguration);
});
}
示例13: TopLevelPropertyTest
public void TopLevelPropertyTest()
{
var injectedProperties = new[]
{
new
{
InjectedJSON = string.Empty,
ExpectedException = (ExpectedException)null
},
new
{
InjectedJSON = "\"@custom.annotation\": null",
ExpectedException = (ExpectedException)null
},
new
{
InjectedJSON = "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataAnnotationNamespacePrefix + "unknown\": { }",
ExpectedException = (ExpectedException)null
},
new
{
InjectedJSON = "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataContextAnnotationName + "\": { }",
ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightPropertyAndValueDeserializer_UnexpectedAnnotationProperties", JsonLightConstants.ODataContextAnnotationName)
},
new
{
InjectedJSON = "\"@custom.annotation\": null, \"@custom.annotation\": 42",
ExpectedException = ODataExpectedExceptions.ODataException("DuplicatePropertyNamesChecker_DuplicateAnnotationNotAllowed", "custom.annotation")
},
};
var payloads = new[]
{
new
{
Json = "{{ " +
"\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata#Edm.Int32\", " +
"\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataNullAnnotationName + "\": " + JsonLightConstants.ODataNullAnnotationTrueValue +
"{1}{0}" +
"}}",
},
new
{
Json = "{{ " +
"\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata#Edm.Int32\", " +
"{0}{1}" +
"\"" + JsonLightConstants.ODataValuePropertyName + "\": 42" +
"}}",
},
new
{
Json = "{{ " +
"\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata#Edm.Int32\", " +
"\"" + JsonLightConstants.ODataValuePropertyName + "\": 42" +
"{1}{0}" +
"}}",
},
};
EdmModel model = new EdmModel();
var container = new EdmEntityContainer("TestModel", "DefaultContainer");
model.AddElement(container);
var owningType = new EdmEntityType("TestModel", "OwningType");
owningType.AddKeys(owningType.AddStructuralProperty("ID", EdmCoreModel.Instance.GetInt32(false)));
owningType.AddStructuralProperty("TopLevelProperty", EdmCoreModel.Instance.GetInt32(true));
owningType.AddStructuralProperty("TopLevelSpatialProperty", EdmCoreModel.Instance.GetSpatial(EdmPrimitiveTypeKind.GeographyPoint, false));
owningType.AddStructuralProperty("TopLevelCollectionProperty", EdmCoreModel.GetCollection(EdmCoreModel.Instance.GetInt32(false)));
model.AddElement(owningType);
IEdmEntityType edmOwningType = (IEdmEntityType)model.FindDeclaredType("TestModel.OwningType");
IEdmStructuralProperty edmTopLevelProperty = (IEdmStructuralProperty)edmOwningType.FindProperty("TopLevelProperty");
IEdmStructuralProperty edmTopLevelSpatialProperty = (IEdmStructuralProperty)edmOwningType.FindProperty("TopLevelSpatialProperty");
IEdmStructuralProperty edmTopLevelCollectionProperty = (IEdmStructuralProperty)edmOwningType.FindProperty("TopLevelCollectionProperty");
PayloadReaderTestDescriptor.ReaderMetadata readerMetadata = new PayloadReaderTestDescriptor.ReaderMetadata(edmTopLevelProperty);
PayloadReaderTestDescriptor.ReaderMetadata spatialReaderMetadata = new PayloadReaderTestDescriptor.ReaderMetadata(edmTopLevelSpatialProperty);
PayloadReaderTestDescriptor.ReaderMetadata collectionReaderMetadata = new PayloadReaderTestDescriptor.ReaderMetadata(edmTopLevelCollectionProperty);
var testDescriptors = payloads.SelectMany(payload => injectedProperties.Select(injectedProperty =>
{
return new NativeInputReaderTestDescriptor()
{
PayloadKind = ODataPayloadKind.Property,
InputCreator = (tc) =>
{
string input = string.Format(payload.Json, injectedProperty.InjectedJSON, string.IsNullOrEmpty(injectedProperty.InjectedJSON) ? string.Empty : ",");
return input;
},
PayloadEdmModel = model,
// We use payload expected result just as a convenient way to run the reader for the Property payload kind.
// We validate whether the reading succeeds or fails, but not the actual read values (at least not here).
ExpectedResultCallback = (tc) =>
new PayloadReaderTestExpectedResult(this.Settings.ExpectedResultSettings)
{
ReaderMetadata = readerMetadata,
ExpectedException = injectedProperty.ExpectedException,
}
};
//.........这里部分代码省略.........
示例14: BuildEntitySetsAndSingletons
private void BuildEntitySetsAndSingletons(InvocationContext context, EdmModel model)
{
var configuration = context.ApiContext.Configuration;
foreach (var property in this.publicProperties)
{
if (configuration.IsPropertyIgnored(property.Name))
{
continue;
}
var isEntitySet = IsEntitySetProperty(property);
if (!isEntitySet)
{
if (!IsSingletonProperty(property))
{
continue;
}
}
var propertyType = property.PropertyType;
if (isEntitySet)
{
propertyType = propertyType.GetGenericArguments()[0];
}
var entityType = model.FindDeclaredType(propertyType.FullName) as IEdmEntityType;
if (entityType == null)
{
// Skip property whose entity type has not been declared yet.
continue;
}
var container = model.EnsureEntityContainer(this.targetType);
if (isEntitySet)
{
if (container.FindEntitySet(property.Name) == null)
{
this.entitySetProperties.Add(property);
var addedEntitySet = container.AddEntitySet(property.Name, entityType);
this.addedNavigationSources.Add(addedEntitySet);
}
}
else
{
if (container.FindSingleton(property.Name) == null)
{
this.singletonProperties.Add(property);
var addedSingleton = container.AddSingleton(property.Name, entityType);
this.addedNavigationSources.Add(addedSingleton);
}
}
}
}
示例15: GetComplexMultiValueProperty
private static ComplexMultiValueProperty GetComplexMultiValueProperty(IRandomNumberGenerator random, EdmModel model = null, ODataVersion version = ODataVersion.V4)
{
int numinstances = random.ChooseFrom(new[] { 0, 1, 3 });
var instance = GetComplexInstance(random, model, version);
var instances = GenerateSimilarComplexInstances(random, instance, numinstances, true);
var propertyName = "ComplexMultivalue" + instance.FullTypeName;
var payload = new ComplexMultiValueProperty(propertyName,
new ComplexMultiValue(propertyName, false, instances.ToArray()));
if (model != null)
{
var entityDataType = instance.GetAnnotation<EntityModelTypeAnnotation>().EdmModelType.Definition as IEdmEntityType;
ExceptionUtilities.CheckObjectNotNull(entityDataType, "Complex Instance must have an EntityModelTypeAnnotation with an EntityDataType");
var entityType = model.FindDeclaredType(entityDataType.FullName()) as EdmEntityType;
ExceptionUtilities.CheckObjectNotNull(entityType, "entityType");
if (entityType.FindProperty(propertyName) != null)
{
entityType.AddStructuralProperty(propertyName, (model.FindDeclaredType(instance.FullTypeName) as EdmComplexType).ToTypeReference());
}
payload.WithTypeAnnotation(entityType);
}
return payload;
}