本文整理汇总了C#中Microsoft.OData.Service.Providers.ResourceType.SetReadOnly方法的典型用法代码示例。如果您正苦于以下问题:C# ResourceType.SetReadOnly方法的具体用法?C# ResourceType.SetReadOnly怎么用?C# ResourceType.SetReadOnly使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.OData.Service.Providers.ResourceType
的用法示例。
在下文中一共展示了ResourceType.SetReadOnly方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DataServiceProviderWrapperShouldFailOnMultipleActionsWithSameNameAndBindingType
public void DataServiceProviderWrapperShouldFailOnMultipleActionsWithSameNameAndBindingType()
{
var entityType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Fake.NS", "Type", false) { CanReflectOnInstanceType = false };
entityType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int))) {CanReflectOnInstanceTypeProperty = false});
entityType.SetReadOnly();
var resourceSet = new ResourceSet("MyEntitySet", entityType);
resourceSet.SetReadOnly();
ResourceType stringType = ResourceType.GetPrimitiveResourceType(typeof(string));
var duplicateAction1 = new ServiceAction("Duplicate", stringType, OperationParameterBindingKind.Always, new[] { new ServiceActionParameter("p1", entityType), new ServiceActionParameter("p2", stringType) }, null);
duplicateAction1.SetReadOnly();
var duplicateAction2 = new ServiceAction("Duplicate", ResourceType.GetPrimitiveResourceType(typeof(int)), OperationParameterBindingKind.Sometimes, new[] { new ServiceActionParameter("p1", entityType) }, null);
duplicateAction2.SetReadOnly();
var actionProvider = new TestActionProvider
{
GetServiceActionsCallback = ctx => new[] { duplicateAction1, duplicateAction2 }
};
var providerWrapper = CreateProviderWrapper(actionProvider, p => p.AddResourceSet(resourceSet));
Action getVisibleOperations = () => providerWrapper.GetVisibleOperations().ToList();
getVisibleOperations.ShouldThrow<DataServiceException>()
.WithMessage(ErrorStrings.DataServiceActionProviderWrapper_DuplicateAction("Duplicate"))
.And.StatusCode.Should().Be(500);
}
示例2: OperationSerializerTests
public OperationSerializerTests()
{
ResourceType intType = ResourceType.GetPrimitiveResourceType(typeof(int));
var customerType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "FQ.NS", "Customer", false);
customerType.CanReflectOnInstanceType = false;
customerType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, intType) { CanReflectOnInstanceTypeProperty = false });
customerType.SetReadOnly();
var operation = new ServiceAction("Action", intType, OperationParameterBindingKind.Sometimes, new[] { new ServiceActionParameter("P1", customerType), new ServiceActionParameter("P2", intType) }, null);
operation.SetReadOnly();
this.baseTypeOperation = new OperationWrapper(operation);
var bestCustomerType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, customerType, "FQ.NS", "BestCustomer", false);
bestCustomerType.SetReadOnly();
operation = new ServiceAction("Action", intType, OperationParameterBindingKind.Sometimes, new[] { new ServiceActionParameter("P1", bestCustomerType) }, null);
operation.SetReadOnly();
this.derivedTypeOperation = new OperationWrapper(operation);
operation = new ServiceAction("Unambiguous", intType, OperationParameterBindingKind.Sometimes, new[] { new ServiceActionParameter("P1", customerType) }, null);
operation.SetReadOnly();
this.unambiguousOperation = new OperationWrapper(operation);
this.entityToSerialize = EntityToSerialize.CreateFromExplicitValues(new object(), bestCustomerType, new TestSerializedEntityKey("http://odata.org/Service.svc/Customers(0)/", bestCustomerType.FullName));
this.testSubject = CreateOperationSerializer(AlwaysAdvertiseActions);
}
示例3: OperationLinkBuilderTests
public OperationLinkBuilderTests()
{
ResourceType intType = ResourceType.GetPrimitiveResourceType(typeof(int));
var customerType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "FQ.NS", "Customer", false);
customerType.CanReflectOnInstanceType = false;
customerType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, intType) { CanReflectOnInstanceTypeProperty = false });
customerType.SetReadOnly();
var operation = new ServiceAction("Action", intType, OperationParameterBindingKind.Sometimes, new[] { new ServiceActionParameter("P1", customerType), new ServiceActionParameter("P2", intType) }, null);
operation.SetReadOnly();
this.operationWithParameters = new OperationWrapper(operation);
var typeWithEscapedName = new ResourceType(typeof(object), ResourceTypeKind.ComplexType, null, "FQ NS", "+ /", false);
typeWithEscapedName.CanReflectOnInstanceType = false;
typeWithEscapedName.AddProperty(new ResourceProperty("Number", ResourcePropertyKind.Primitive, intType) { CanReflectOnInstanceTypeProperty = false });
typeWithEscapedName.SetReadOnly();
operation = new ServiceAction("Action", intType, OperationParameterBindingKind.Sometimes, new[] { new ServiceActionParameter("P1", customerType), new ServiceActionParameter("P2", typeWithEscapedName) }, null);
operation.SetReadOnly();
this.operationWithEscapedParameter = new OperationWrapper(operation);
var bestCustomerType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, customerType, "FQ.NS", "BestCustomer", false);
bestCustomerType.SetReadOnly();
operation = new ServiceAction("Action", intType, OperationParameterBindingKind.Sometimes, new[] { new ServiceActionParameter("P1", customerType) }, null);
operation.SetReadOnly();
this.operationBoundToBaseType = new OperationWrapper(operation);
this.entityToSerialize = EntityToSerialize.CreateFromExplicitValues(new object(), bestCustomerType, new TestSerializedEntityKey("http://odata.org/Service.svc/Customers/", bestCustomerType.FullName));
var metadataUri = new Uri("http://odata.org/Service.svc/$metadata");
this.testSubject = new OperationLinkBuilder("MyContainer", metadataUri);
}
示例4: VerifyVersionOfEntityWithGeographyPropertyIsV3
public void VerifyVersionOfEntityWithGeographyPropertyIsV3()
{
ResourceType rt = new ResourceType(typeof(IDictionary<string, string>), ResourceTypeKind.EntityType, null, "TestNamespace", "TestEntity", false);
var keyProperty = new ResourceProperty("ID", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, ResourceType.GetPrimitiveResourceType(typeof(int))) {CanReflectOnInstanceTypeProperty = false };
var geographyProperty = new ResourceProperty("GeographyProperty", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(Geography))) { CanReflectOnInstanceTypeProperty = false };
rt.AddProperty(keyProperty);
rt.AddProperty(geographyProperty);
rt.SetReadOnly();
Assert.AreEqual(v4, rt.MetadataVersion, "MetadataVersion must be 4.0");
Assert.AreEqual(MetadataEdmSchemaVersion.Version4Dot0, rt.SchemaVersion, "Schema version must be 4.0");
}
示例5: CreateEntityToSerialize
private static EntityToSerialize CreateEntityToSerialize(bool shouldIncludeTypeSegment)
{
ResourceType baseType = new ResourceType(typeof(MyType), ResourceTypeKind.EntityType, null, "TestNamespace", "BaseType", /*isAbstract*/ false);
baseType.AddProperty(new ResourceProperty("ID", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, new ResourceType(typeof(int), ResourceTypeKind.Primitive, null, "int")));
baseType.SetReadOnly();
Uri serviceUri = new Uri("http://dummy");
KeySerializer keySerializer = KeySerializer.Create(UrlConvention.CreateWithExplicitValue(false));
Func<ResourceProperty, object> getPropertyValue = p => "fakePropertyValue";
return EntityToSerialize.Create(new MyType { ID = 42 }, baseType, "MySet", shouldIncludeTypeSegment, getPropertyValue, keySerializer, serviceUri);
}
示例6: InvalidCasesTest
public void InvalidCasesTest()
{
ResourceProperty id = new ResourceProperty("ID", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, ResourceType.GetPrimitiveResourceType(typeof(int))) { CanReflectOnInstanceTypeProperty = false };
ResourceType customerType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Foo", "CustomerType", false);
customerType.AddProperty(id);
ResourceType orderType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Foo", "OrderType", false);
orderType.AddProperty(id);
ResourceProperty customerOrders = new ResourceProperty("Orders", ResourcePropertyKind.ResourceSetReference, orderType);
ResourceProperty orderCustomer = new ResourceProperty("Customer", ResourcePropertyKind.ResourceReference, customerType);
customerType.AddProperty(customerOrders);
orderType.AddProperty(orderCustomer);
customerOrders.CanReflectOnInstanceTypeProperty = false;
orderCustomer.CanReflectOnInstanceTypeProperty = false;
customerType.SetReadOnly();
orderType.SetReadOnly();
ResourceSet customerSet = new ResourceSet("Customers", customerType);
ResourceSet orderSet = new ResourceSet("Orders", orderType);
ExceptionUtils.CheckInvalidConstructorParameters(
typeof(ResourceAssociationSetEnd),
"Value cannot be null.\r\nParameter name: resourceSet",
null, customerType, customerOrders);
ExceptionUtils.CheckInvalidConstructorParameters(
typeof(ResourceAssociationSetEnd),
"Value cannot be null.\r\nParameter name: resourceType",
customerSet, null, customerOrders);
ExceptionUtils.CheckInvalidConstructorParameters(
typeof(ResourceAssociationSetEnd),
"The resourceProperty parameter must be a navigation property on the resource type specified by the resourceType parameter.",
customerSet, customerType, id);
ExceptionUtils.CheckInvalidConstructorParameters(
typeof(ResourceAssociationSetEnd),
"The resourceProperty parameter must be a navigation property on the resource type specified by the resourceType parameter.",
customerSet, customerType, orderCustomer);
ExceptionUtils.CheckInvalidConstructorParameters(
typeof(ResourceAssociationSetEnd),
"The resourceType parameter must be a type that is assignable to the resource set specified by the resourceSet parameter.",
customerSet,
orderType,
orderCustomer);
}
示例7: InvalidCasesTest
public void InvalidCasesTest()
{
ResourceProperty id = new ResourceProperty("ID", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, ResourceType.GetPrimitiveResourceType(typeof(int))) { CanReflectOnInstanceTypeProperty = false };
ResourceType customerType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Foo", "CustomerType", false);
customerType.AddProperty(id);
ResourceType orderType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Foo", "OrderType", false);
orderType.AddProperty(id);
ResourceProperty customerOrders = new ResourceProperty("Orders", ResourcePropertyKind.ResourceSetReference, orderType);
ResourceProperty orderCustomer = new ResourceProperty("Customer", ResourcePropertyKind.ResourceReference, customerType);
customerType.AddProperty(customerOrders);
orderType.AddProperty(orderCustomer);
customerOrders.CanReflectOnInstanceTypeProperty = false;
orderCustomer.CanReflectOnInstanceTypeProperty = false;
customerType.SetReadOnly();
orderType.SetReadOnly();
ResourceSet customerSet = new ResourceSet("Customers", customerType);
ResourceSet orderSet = new ResourceSet("Orders", orderType);
ResourceAssociationSetEnd end1 = new ResourceAssociationSetEnd(customerSet, customerType, null);
ResourceAssociationSetEnd end2 = new ResourceAssociationSetEnd(orderSet, orderType, null);
ExceptionUtils.CheckInvalidConstructorParameters(
typeof(ResourceAssociationSet),
"Value cannot be null or empty.\r\nParameter name: name",
null, end1, end2);
ExceptionUtils.CheckInvalidConstructorParameters(
typeof(ResourceAssociationSet),
"Value cannot be null or empty.\r\nParameter name: name",
string.Empty, end1, end2);
ExceptionUtils.CheckInvalidConstructorParameters(
typeof(ResourceAssociationSet),
"Value cannot be null.\r\nParameter name: end1",
"Customer_Order", null, end2);
ExceptionUtils.CheckInvalidConstructorParameters(
typeof(ResourceAssociationSet),
"Value cannot be null.\r\nParameter name: end2",
"Customer_Order", end1, null);
ExceptionUtils.CheckInvalidConstructorParameters(
typeof(ResourceAssociationSet),
"The ResourceProperty of the ResourceAssociationEnds cannot both be null.",
"Customer_Order", end1, end2);
}
示例8: CreateResourceTypeWithKeyProperties
private static ResourceType CreateResourceTypeWithKeyProperties(params string[] keyPropertyNames)
{
var resourceType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "FQ.NS", "EntityType", false) {CanReflectOnInstanceType = false};
foreach (var keyPropertyName in keyPropertyNames)
{
resourceType.AddProperty(new ResourceProperty(keyPropertyName, ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int))) {CanReflectOnInstanceTypeProperty = false});
}
resourceType.SetReadOnly();
return resourceType;
}
示例9: EnforceMetadataCache
/// <summary>
/// Ensures that the product caches resource types based on their names.
/// </summary>
/// <param name="type">The resource type which may or may not be cached.</param>
/// <param name="throwIfAlreadyCached">Whether or not to throw if the resource type is already cached.</param>
/// <param name="addToCache">Whether or not to add the type to the cache. Certain code paths do not cache all the resource type they could.</param>
/// <returns>The same resource type if not cached, or a copy if it was.</returns>
private ResourceType EnforceMetadataCache(ResourceType type, bool throwIfAlreadyCached, bool addToCache)
{
if (type == null)
{
return null;
}
if (!ProviderImplementationSettings.Current.EnforceMetadataCaching)
{
return type;
}
if (type.ResourceTypeKind != ResourceTypeKind.ComplexType && type.ResourceTypeKind != ResourceTypeKind.EntityType)
{
return type;
}
bool cached;
if (addToCache)
{
cached = !this.resourceTypeCache.Add(type.FullName);
}
else
{
cached = this.resourceTypeCache.Contains(type.FullName);
}
if (!cached)
{
return type;
}
ExceptionUtilities.Assert(!throwIfAlreadyCached, "Resource type '{0}' was resolved more than once", type.Name);
var copy = new ResourceType(typeof(object), type.ResourceTypeKind, null, type.Namespace, type.Name, !type.IsAbstract);
copy.SetReadOnly();
return copy;
}
示例10: AddingPropertiesTest
public void AddingPropertiesTest()
{
ResourceType complexType = new ResourceType(typeof(object), ResourceTypeKind.ComplexType, null, "namespace", "Address", false);
ResourceType entityType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Namespace", "Order", false);
IEnumerable<ResourceProperty> primitiveResourceProperties =
ResourceTypeUtils.GetPrimitiveResourceTypes().Where(rt => rt.FullName != "Edm.Stream").Select(rt => new ResourceProperty("PrimitiveProperty", ResourcePropertyKind.Primitive, rt));
IEnumerable<ResourceProperty> keyResourceProperties =
GetKeyResourceTypes().Where(rt => !rt.FullName.StartsWith("Edm.Geography") && !rt.FullName.StartsWith("Edm.Geometry")).Select(rt => new ResourceProperty("KeyProperty", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, rt));
IEnumerable<ResourceProperty> etagResourceProperties =
ResourceTypeUtils.GetPrimitiveResourceTypes().Where(rt => !rt.FullName.StartsWith("Edm.Geography") && !rt.FullName.StartsWith("Edm.Geometry") && rt.FullName != "Edm.Stream").Select(rt => new ResourceProperty("ETagProperty", ResourcePropertyKind.Primitive | ResourcePropertyKind.ETag, rt));
IEnumerable<ResourceProperty> complexProperties = new ResourceProperty[] {
new ResourceProperty("ComplexProperty", ResourcePropertyKind.ComplexType, complexType) };
IEnumerable<ResourceProperty> navigationProperties = new ResourceProperty[] {
new ResourceProperty("ResourceReferenceProperty", ResourcePropertyKind.ResourceReference, entityType),
new ResourceProperty("ResourceSetReferenceProperty", ResourcePropertyKind.ResourceSetReference, entityType)};
IEnumerable<ResourceProperty> collectionProperties = ResourceTypeUtils.GetPrimitiveResourceTypes().Except(new [] { ResourceType.GetPrimitiveResourceType(typeof(System.IO.Stream)) }).Concat(new ResourceType[] { complexType })
.Select(rt => new ResourceProperty("CollectionProperty", ResourcePropertyKind.Collection, ResourceType.GetCollectionResourceType(rt)));
// Primitive resource type - no properties can be added
foreach (ResourceType primitiveResourceType in ResourceTypeUtils.GetPrimitiveResourceTypes())
foreach (ResourceProperty property in primitiveResourceProperties.Concat(keyResourceProperties).Concat(etagResourceProperties)
.Concat(complexProperties).Concat(navigationProperties).Concat(collectionProperties))
{
ExceptionUtils.ThrowsException<InvalidOperationException>(
() => primitiveResourceType.AddProperty(property),
string.Format("Adding property {0} to primitive resource type {1} should fail.", property.Name, primitiveResourceType.FullName));
}
// Collection resource type - no properties can be added
foreach (ResourceType CollectionResourceType in ResourceTypeUtils.GetPrimitiveResourceTypes().Except(new[] { ResourceType.GetPrimitiveResourceType(typeof(System.IO.Stream)) }).Concat(new ResourceType[] { complexType })
.Select(rt => ResourceType.GetCollectionResourceType(rt)))
foreach (ResourceProperty property in primitiveResourceProperties.Concat(keyResourceProperties).Concat(etagResourceProperties)
.Concat(complexProperties).Concat(navigationProperties).Concat(collectionProperties))
{
ExceptionUtils.ThrowsException<InvalidOperationException>(
() => CollectionResourceType.AddProperty(property),
string.Format("Adding property {0} to a collection resource type {1} should fail.", property.Name, CollectionResourceType.FullName));
}
// Collection resource type - no properties can be added
foreach (ResourceType collectionResourceType in (new ResourceType[] { entityType }).Select(rt => ResourceType.GetEntityCollectionResourceType(rt)))
foreach (ResourceProperty property in primitiveResourceProperties.Concat(keyResourceProperties).Concat(etagResourceProperties)
.Concat(complexProperties).Concat(navigationProperties).Concat(collectionProperties))
{
ExceptionUtils.ThrowsException<InvalidOperationException>(
() => collectionResourceType.AddProperty(property),
string.Format("Adding property {0} to a collection resource type {1} should fail.", property.Name, collectionResourceType.FullName));
}
// Complex resource type - only primitive, complex and collection properties can be added
foreach (ResourceProperty property in primitiveResourceProperties.Concat(complexProperties).Concat(collectionProperties))
{
ResourceType t = new ResourceType(typeof(object), ResourceTypeKind.ComplexType, null, "Ns", "Address", false);
t.AddProperty(property);
}
// It is possible to add navigation property to a complex type.
// Once this is fixed remove this foreach and add navigationProperties to the list for the next foreach.
foreach (var p in navigationProperties)
{
ResourceType t = new ResourceType(typeof(object), ResourceTypeKind.ComplexType, null, "Ns", "Address", false);
t.AddProperty(p);
t.SetReadOnly();
}
foreach (ResourceProperty property in keyResourceProperties.Concat(etagResourceProperties))
{
ResourceType t = new ResourceType(typeof(object), ResourceTypeKind.ComplexType, null, "Ns", "Address", false);
ExceptionUtils.ThrowsException<InvalidOperationException>(
() => t.AddProperty(property),
string.Format("Adding key or etag property {0} to complex resource type should fail.", property.Name));
}
// Entity resource type
foreach (ResourceProperty property in primitiveResourceProperties.Concat(keyResourceProperties).Concat(etagResourceProperties)
.Concat(complexProperties).Concat(navigationProperties).Concat(collectionProperties))
{
ResourceType t = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Ns", "Order", false);
if (property.ResourceType.InstanceType == typeof(System.IO.Stream) && (property.Kind & ResourcePropertyKind.Stream) != ResourcePropertyKind.Stream)
{
ExceptionUtils.ThrowsException<InvalidOperationException>(
() =>
{
t.AddProperty(property);
},
"Adding Stream type as primitive property should fail");
}
else
{
t.AddProperty(property);
}
}
}
示例11: AddNamedStreamValidationTest
public void AddNamedStreamValidationTest()
{
ResourceProperty idProperty = new ResourceProperty("ID", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int)));
ResourceProperty name = new ResourceProperty("Name", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(string)));
idProperty.CanReflectOnInstanceTypeProperty = false;
name.CanReflectOnInstanceTypeProperty = false;
ResourceType entityType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "namespace", "entityType", false);
// Adding named stream to readonly type
entityType.AddProperty(idProperty);
entityType.SetReadOnly();
ResourceProperty stream1 = new ResourceProperty("Stream1", ResourcePropertyKind.Stream, ResourceType.GetPrimitiveResourceType(typeof(System.IO.Stream)));
ExceptionUtils.ExpectedException<InvalidOperationException>(
() => entityType.AddProperty(stream1),
string.Format("The resource type '{0}' cannot be modified since it is already set to read-only.", entityType.FullName),
"Adding to a sealed type should fail.");
// Adding a named stream which will collide with properties declared on this type
ResourceType type1 = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "namespace", "type1", true);
type1.AddProperty(idProperty);
ResourceProperty idStream = new ResourceProperty("ID", ResourcePropertyKind.Stream, ResourceType.GetPrimitiveResourceType(typeof(System.IO.Stream)));
ExceptionUtils.ExpectedException<InvalidOperationException>(
() => type1.AddProperty(idStream),
"A property with same name 'ID' already exists in type 'namespace.type1'. Please make sure that there is no property with the same name defined in one of the base types.",
"Named stream can't have the same name as a property.");
// Adding a named stream which will collide with properties declared on base type
type1 = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "namespace", "type1", true);
type1.AddProperty(idProperty);
ResourceType type2 = new ResourceType(typeof(object), ResourceTypeKind.EntityType, type1, "namespace", "type2", false);
// We don't detect name collision between named streams and properties until we enumerate properties after the type is set to readonly.
idStream = new ResourceProperty("ID", ResourcePropertyKind.Stream, ResourceType.GetPrimitiveResourceType(typeof(System.IO.Stream)));
type2.AddProperty(idStream);
type2.Properties.Count();
type2.SetReadOnly();
// Detect name collision now.
ExceptionUtils.ExpectedException<InvalidOperationException>(
() => type2.Properties.Count(),
"A property with same name 'ID' already exists in type 'namespace.type2'. Please make sure that there is no property with the same name defined in one of the base types.",
"Named stream can't have the same name as a property on base type.");
// Adding a named stream which will collide with named streams declared on this type
type1 = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "namespace", "type1", true);
type1.AddProperty(idProperty);
ResourceProperty nameStream = new ResourceProperty("Name", ResourcePropertyKind.Stream, ResourceType.GetPrimitiveResourceType(typeof(System.IO.Stream)));
type1.AddProperty(nameStream);
// Adding the same named stream instance
ExceptionUtils.ExpectedException<InvalidOperationException>(
() => type1.AddProperty(nameStream),
"A property with same name 'Name' already exists in type 'namespace.type1'. Please make sure that there is no property with the same name defined in one of the base types.",
"Named stream can't have the same name as another named stream.");
// Adding a new instance with the same name
ExceptionUtils.ExpectedException<InvalidOperationException>(
() => type1.AddProperty(new ResourceProperty("Name", ResourcePropertyKind.Stream, ResourceType.GetPrimitiveResourceType(typeof(System.IO.Stream)))),
"A property with same name 'Name' already exists in type 'namespace.type1'. Please make sure that there is no property with the same name defined in one of the base types.",
"Named stream can't have the same name as another named stream - new instance.");
// Adding a named stream which will collide with named streams declared on base type
type1 = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "namespace", "type1", true);
type1.AddProperty(idProperty);
type1.AddProperty(nameStream);
type2 = new ResourceType(typeof(object), ResourceTypeKind.EntityType, type1, "namespace", "type2", false);
type2.AddProperty(nameStream); // Adding the same instance
type2.SetReadOnly();
// Delay detection until ValidateType(), this is because you can add named streams to the base type that collides with those on the derived type.
ExceptionUtils.ExpectedException<InvalidOperationException>(
() => type2.Properties.Count(),
"A property with same name 'Name' already exists in type 'namespace.type2'. Please make sure that there is no property with the same name defined in one of the base types.",
"Named stream can't have the same name as another named stream - new instance.");
type2 = new ResourceType(typeof(object), ResourceTypeKind.EntityType, type1, "namespace", "type2", false);
type2.AddProperty(new ResourceProperty(nameStream.Name, ResourcePropertyKind.Stream, ResourceType.GetPrimitiveResourceType(typeof(System.IO.Stream)))); // Adding a new instance with the same name
type2.SetReadOnly();
// Delay detection until ValidaType(), this is because you can add named streams to the base type that collides with those on the derived type.
ExceptionUtils.ExpectedException<InvalidOperationException>(
() => type2.Properties.Count(),
"A property with same name 'Name' already exists in type 'namespace.type2'. Please make sure that there is no property with the same name defined in one of the base types.",
"Named stream can't have the same name as another named stream - new instance.");
// Adding a property which will collide with named streams declared on this type
type1 = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "namespace", "type1", true);
type1.AddProperty(idProperty);
nameStream = new ResourceProperty("Name", ResourcePropertyKind.Stream, ResourceType.GetPrimitiveResourceType(typeof(System.IO.Stream)));
type1.AddProperty(nameStream);
ExceptionUtils.ExpectedException<InvalidOperationException>(
() => type1.AddProperty(name),
"A property with same name 'Name' already exists in type 'namespace.type1'. Please make sure that there is no property with the same name defined in one of the base types.",
"Can't add property with the same name as a named stream.");
// Adding a property which will collide with named streams declared on base type
type1 = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "namespace", "type1", true);
type1.AddProperty(idProperty);
type1.AddProperty(nameStream);
// We don't detect name collision between named streams and properties until we enumerate properties after the type is set to readonly.
type2 = new ResourceType(typeof(object), ResourceTypeKind.EntityType, type1, "namespace", "type2", false);
type2.AddProperty(name);
type2.Properties.Count();
type2.SetReadOnly();
// Detect name collision now.
ExceptionUtils.ExpectedException<InvalidOperationException>(
//.........这里部分代码省略.........
示例12: AddNamedStreamTest
public void AddNamedStreamTest()
{
ResourceType type1 = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "namespace", "type1", true);
type1.CanReflectOnInstanceType = false;
ResourceProperty id = new ResourceProperty("ID", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int)));
type1.AddProperty(id);
id.CanReflectOnInstanceTypeProperty = false;
ResourceType type2 = new ResourceType(typeof(object), ResourceTypeKind.EntityType, type1, "namespace", "type2", false);
type2.AddProperty(new ResourceProperty("Stream1", ResourcePropertyKind.Stream, ResourceType.GetPrimitiveResourceType(typeof(System.IO.Stream))));
type2.AddProperty(new ResourceProperty("Stream2", ResourcePropertyKind.Stream, ResourceType.GetPrimitiveResourceType(typeof(System.IO.Stream))));
ResourceType type3 = new ResourceType(typeof(object), ResourceTypeKind.EntityType, type2, "namespace", "type3", false);
type3.AddProperty(new ResourceProperty("Stream3", ResourcePropertyKind.Stream, ResourceType.GetPrimitiveResourceType(typeof(System.IO.Stream))));
Assert.AreEqual(0, type1.GetNamedStreams().Count(), "Wrong count");
Assert.AreEqual(0, type1.GetNamedStreamsDeclaredOnThisType().Count(), "Wrong count");
Assert.AreEqual(2, type2.GetNamedStreams().Count(), "Wrong count");
Assert.IsNotNull(type2.GetNamedStreams().Single(s => s.Name == "Stream1"), "Unexpected null");
Assert.IsNotNull(type2.GetNamedStreams().Single(s => s.Name == "Stream2"), "Unexpected null");
Assert.AreEqual(2, type2.GetNamedStreamsDeclaredOnThisType().Count(), "Wrong count");
Assert.IsNotNull(type2.GetNamedStreamsDeclaredOnThisType().Single(s => s.Name == "Stream1"), "Unexpected null");
Assert.IsNotNull(type2.GetNamedStreamsDeclaredOnThisType().Single(s => s.Name == "Stream2"), "Unexpected null");
Assert.AreEqual(3, type3.GetNamedStreams().Count(), "Wrong count");
Assert.IsNotNull(type3.GetNamedStreams().Single(s => s.Name == "Stream1"), "Unexpected null");
Assert.IsNotNull(type3.GetNamedStreams().Single(s => s.Name == "Stream2"), "Unexpected null");
Assert.IsNotNull(type3.GetNamedStreams().Single(s => s.Name == "Stream3"), "Unexpected null");
Assert.AreEqual(1, type3.GetNamedStreamsDeclaredOnThisType().Count(), "Wrong count");
Assert.IsNotNull(type3.GetNamedStreamsDeclaredOnThisType().Single(s => s.Name == "Stream3"), "Unexpected null");
type3.SetReadOnly();
Assert.AreEqual(0, type1.GetNamedStreams().Count(), "Wrong count");
Assert.AreEqual(0, type1.GetNamedStreamsDeclaredOnThisType().Count(), "Wrong count");
Assert.AreEqual(2, type2.GetNamedStreams().Count(), "Wrong count");
Assert.IsNotNull(type2.GetNamedStreams().Single(s => s.Name == "Stream1"), "Unexpected null");
Assert.IsNotNull(type2.GetNamedStreams().Single(s => s.Name == "Stream2"), "Unexpected null");
Assert.AreEqual(2, type2.GetNamedStreamsDeclaredOnThisType().Count(), "Wrong count");
Assert.IsNotNull(type2.GetNamedStreamsDeclaredOnThisType().Single(s => s.Name == "Stream1"), "Unexpected null");
Assert.IsNotNull(type2.GetNamedStreamsDeclaredOnThisType().Single(s => s.Name == "Stream2"), "Unexpected null");
Assert.AreEqual(3, type3.GetNamedStreams().Count(), "Wrong count");
Assert.IsNotNull(type3.GetNamedStreams().Single(s => s.Name == "Stream1"), "Unexpected null");
Assert.IsNotNull(type3.GetNamedStreams().Single(s => s.Name == "Stream2"), "Unexpected null");
Assert.IsNotNull(type3.GetNamedStreams().Single(s => s.Name == "Stream3"), "Unexpected null");
Assert.AreEqual(1, type3.GetNamedStreamsDeclaredOnThisType().Count(), "Wrong count");
Assert.IsNotNull(type3.GetNamedStreamsDeclaredOnThisType().Single(s => s.Name == "Stream3"), "Unexpected null");
}
示例13: ServiceActionConstructorTests
public void ServiceActionConstructorTests()
{
ResourceProperty id = new ResourceProperty("ID", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, ResourceType.GetPrimitiveResourceType(typeof(int)));
ResourceType customerType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Foo", "Customer", false);
customerType.AddProperty(id);
customerType.SetReadOnly();
ResourceSet customerSet = new ResourceSet("Customers", customerType);
ResourceType orderType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Foo", "Order", false);
orderType.AddProperty(id);
orderType.SetReadOnly();
ResourceType complexType = new ResourceType(typeof(object), ResourceTypeKind.ComplexType, null, "foo", "Address", false);
ResourceType collectionOfPrimitive = ResourceType.GetCollectionResourceType(ResourceType.GetPrimitiveResourceType(typeof(int)));
ResourceType collectionOfComplex = ResourceType.GetCollectionResourceType(complexType);
ResourceType collectionOfCustomer = ResourceType.GetEntityCollectionResourceType(customerType);
ResourceType collectionOfOrder = ResourceType.GetEntityCollectionResourceType(orderType);
var types = ResourceTypeUtils.GetPrimitiveResourceTypes().Concat(new ResourceType[] { null, customerType, orderType, complexType, collectionOfPrimitive, collectionOfComplex, collectionOfCustomer, collectionOfOrder });
var resultSetsOrPathExpressions = new object[] { null, customerSet, new ResourceSetPathExpression("p1/Foo") };
var parameters = types.Except(new[] { ResourceType.GetPrimitiveResourceType(typeof(System.IO.Stream)) }).Select(t => t != null ? new ServiceActionParameter[] { new ServiceActionParameter("p1", t) } : new ServiceActionParameter[0]);
parameters = parameters.Concat(types.Except(new[] { ResourceType.GetPrimitiveResourceType(typeof(System.IO.Stream)), null }).ToList().Combinations(2).Select(tt => new ServiceActionParameter[] { new ServiceActionParameter("p1", tt[0]), new ServiceActionParameter("p2", tt[1]) }));
var operationParameterBindings = new OperationParameterBindingKind[] { OperationParameterBindingKind.Never, OperationParameterBindingKind.Sometimes, OperationParameterBindingKind.Always };
AstoriaTestNS.TestUtil.RunCombinations(
types, resultSetsOrPathExpressions, parameters, operationParameterBindings,
(returnType, resultSetOrPathExpression, paramList, operationParameterBindingKind) =>
{
ServiceAction action = null;
ResourceSet resourceSet = resultSetOrPathExpression as ResourceSet;
ResourceSetPathExpression pathExpression = resultSetOrPathExpression as ResourceSetPathExpression;
bool bindable = (operationParameterBindingKind == OperationParameterBindingKind.Always || operationParameterBindingKind == OperationParameterBindingKind.Sometimes);
Exception e = null;
try
{
if (pathExpression != null)
{
bindable = true;
action = new ServiceAction("foo", returnType, OperationParameterBindingKind.Sometimes, paramList, pathExpression);
}
else
{
action = new ServiceAction("foo", returnType, resourceSet, operationParameterBindingKind, paramList);
}
}
catch (Exception ex)
{
e = ex;
}
if (resourceSet != null && operationParameterBindingKind != OperationParameterBindingKind.Never)
{
ExceptionUtils.IsExpectedException<ArgumentException>(e, "When 'returnType' is an entity type or an entity collection type, 'resultSetPathExpression' and 'resultSet' cannot be both null and the resource type of the result set must be assignable from 'returnType'.");
}
else if (returnType != null && (returnType.ResourceTypeKind == ResourceTypeKind.EntityType || returnType.ResourceTypeKind == ResourceTypeKind.EntityCollection) &&
(resourceSet == null && pathExpression == null ||
resourceSet != null && returnType.ResourceTypeKind == ResourceTypeKind.EntityType && resourceSet.ResourceType != returnType ||
resourceSet != null && returnType.ResourceTypeKind == ResourceTypeKind.EntityCollection && resourceSet.ResourceType != ((EntityCollectionResourceType)returnType).ItemType))
{
ExceptionUtils.IsExpectedException<ArgumentException>(e, "When 'returnType' is an entity type or an entity collection type, 'resultSetPathExpression' and 'resultSet' cannot be both null and the resource type of the result set must be assignable from 'returnType'.");
}
else if ((returnType == null || returnType.ResourceTypeKind != ResourceTypeKind.EntityCollection && returnType.ResourceTypeKind != ResourceTypeKind.EntityType) && resourceSet != null)
{
ExceptionUtils.IsExpectedException<ArgumentException>(e, "'resultSet' must be null when 'returnType' is null, not an entity type or not an entity collection type.");
}
else if ((returnType == null || returnType.ResourceTypeKind != ResourceTypeKind.EntityCollection && returnType.ResourceTypeKind != ResourceTypeKind.EntityType) && pathExpression != null)
{
ExceptionUtils.IsExpectedException<ArgumentException>(e, "'resultSetPathExpression' must be null when 'returnType' is null, not an entity type or not an entity collection type.");
}
else if (returnType == ResourceType.GetPrimitiveResourceType(typeof(System.IO.Stream)))
{
ExceptionUtils.IsExpectedException<ArgumentException>(e, "The resource type 'Edm.Stream' is not a type that can be returned by a function or action. A function or action can only return values of an entity type, an entity collection type, a complex type, a collection type or any primitive type, other than the stream type.\r\nParameter name: returnType");
}
else if (paramList.Length > 0 && paramList.Skip(bindable ? 1 : 0).Any(p => p.ParameterType.ResourceTypeKind == ResourceTypeKind.EntityType || p.ParameterType.ResourceTypeKind == ResourceTypeKind.EntityCollection))
{
var param = paramList.Skip(bindable ? 1 : 0).First(p => p.ParameterType.ResourceTypeKind == ResourceTypeKind.EntityType || p.ParameterType.ResourceTypeKind == ResourceTypeKind.EntityCollection);
var parameterTypeKind = param.ParameterType.ResourceTypeKind;
ExceptionUtils.IsExpectedException<ArgumentException>(e, string.Format("The '{0}' parameter is of resource type kind '{1}' and it is not the binding parameter. Parameter of type kind '{1}' is only supported for the binding parameter.", param.Name, parameterTypeKind));
}
else if (pathExpression != null && !bindable)
{
ExceptionUtils.IsExpectedException<ArgumentException>(e, "The binding parameter type must be an entity type or an entity collection type when 'resultSetPathExpression' is not null.");
}
else if (bindable && paramList.Length == 0)
{
ExceptionUtils.IsExpectedException<ArgumentException>(e, "Bindable actions or functions must have at least one parameter, where the first parameter is the binding parameter.\r\nParameter name: operationParameterBindingKind");
}
else if (pathExpression != null && bindable && paramList.First().ParameterType.ResourceTypeKind != ResourceTypeKind.EntityType && paramList.First().ParameterType.ResourceTypeKind != ResourceTypeKind.EntityCollection)
{
ExceptionUtils.IsExpectedException<ArgumentException>(e, "The binding parameter type must be an entity type or an entity collection type when 'resultSetPathExpression' is not null.");
}
else if (paramList.Length > 0 && bindable && paramList[0].ParameterType.ResourceTypeKind != ResourceTypeKind.EntityType && paramList[0].ParameterType.ResourceTypeKind != ResourceTypeKind.EntityCollection)
{
ExceptionUtils.IsExpectedException<ArgumentException>(e, "An action's binding parameter must be of type Entity or EntityCollection.\r\nParameter name: parameters");
}
else
{
Assert.IsNull(e, "Received exception but expected none. Exception message: {0}", e == null ? string.Empty : e.Message);
Assert.IsNotNull(action, "Action should be constructed.");
Assert.AreEqual("foo", action.Name, "unexpected name");
//.........这里部分代码省略.........
示例14: PopulateTestMetadata
private void PopulateTestMetadata()
{
ResourceType entityType = new ResourceType(typeof(TestEntityType), ResourceTypeKind.EntityType, null, "AstoriaUnitTests.Tests.Server", "TestEntityType", false);
entityType.CanReflectOnInstanceType = true;
entityType.AddProperty(new ResourceProperty("ID", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(Int32))));
entityType.AddProperty(new ResourceProperty("Name", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(String))));
entityType.AddProperty(new ResourceProperty("Spatial", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(Geography))));
entityType.AddProperty(new ResourceProperty("Point", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(GeographyPoint))));
entityType.SetReadOnly();
serviceFactory.AddResourceType(entityType);
ResourceSet entitySet = new ResourceSet("Entities", entityType);
entitySet.SetReadOnly();
serviceFactory.AddResourceSet(entitySet);
}
示例15: CreateDerivedType
private static ResourceType CreateDerivedType(string name, ResourceType baseType)
{
var resourceType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, baseType, "FQ.NS", name, false) { CanReflectOnInstanceType = false };
resourceType.SetReadOnly();
return resourceType;
}