Permalink
Browse files

complete refactoring of api and metadata builders

  • Loading branch information...
1 parent 2675198 commit afe9090663fdab50937f8a35ab4dac19d6dcee92 @craiggwilson committed Aug 29, 2012
@@ -10,25 +10,35 @@
namespace MongoDB.OData.SampleHost
{
- public class BlogApi : TypedMongoDataService
+ [MongoDatabase("odata_blogs")]
+ public class BlogEntities
+ {
+ [MongoCollection("blogs")]
+ public MongoCollection<Blog> Blogs { get; set; }
+
+ [MongoCollection("posts")]
+ public MongoCollection<Post> Posts { get; set; }
+
+ [MongoCollection("users")]
+ public MongoCollection<User> Users { get; set; }
+ }
+
+ public class BlogApi : MongoDataService<BlogEntities>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
- TypedMongoDataService.Configure(config);
+ Configure(config);
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.UseVerboseErrors = true;
}
- protected override void BuildMetadata(TypedMongoDataServiceMetadataBuilder builder)
+ protected override BlogEntities CreateDataSource(MongoServer server)
{
- builder.SetContainer("MongoDB.Samples", "BlogApi");
- builder.AddResourceSet<Blog>("Blogs", "odata_blogs", "blogs");
- builder.AddResourceSet<Post>("Posts", "odata_blogs", "posts");
- builder.AddResourceSet<User>("Users", "odata_blogs", "users");
+ return new BlogEntities();
}
- protected override MongoServer CreateDataSource()
+ protected override MongoServer CreateMongoServer()
{
var server = MongoServer.Create();
@@ -39,7 +49,9 @@ protected override MongoServer CreateDataSource()
var postsCollection = db.GetCollection<Post>("posts");
if (userCollection.Count() > 0)
+ {
return server;
+ }
var user1 = new User
{
@@ -7,21 +7,35 @@
using System.Web;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
+using MongoDB.Driver.Linq;
using MongoDB.OData.SampleModels.HumanResources;
+using System.Data.Services.Providers;
namespace MongoDB.OData.SampleHost
{
- public class HumanResourcesApi : TypedMongoDataService
+ [MongoDatabase("odata_hr")]
+ public class HumanResourceEntities
{
- // This method is called only once to initialize service-wide policies.
- public static void InitializeService(DataServiceConfiguration config)
+ [MongoCollection("people")]
+ public MongoCollection<Person> People { get; set; }
+
+ [WebGet]
+ public IQueryable<Employee> GetEmployeesOfManager(string managerId)
{
- TypedMongoDataService.Configure(config);
- config.SetEntitySetAccessRule("*", EntitySetRights.All);
- config.UseVerboseErrors = true;
+ var manager = People.AsQueryable().OfType<Manager>().SingleOrDefault(m => m.Id == managerId);
+ if (manager == null)
+ {
+ throw new DataServiceException(404, string.Format("Manager with id {0} does not exist.", managerId));
+ }
+
+ var employeeIds = manager.Employees.Select(m => m.Id);
+ return People.AsQueryable().OfType<Employee>().Where(e => employeeIds.Contains(e.Id));
}
+ }
- protected override void BuildMetadata(TypedMongoDataServiceMetadataBuilder builder)
+ public class HumanResourcesApi : MongoDataService<HumanResourceEntities>
+ {
+ static HumanResourcesApi()
{
if (!BsonClassMap.IsClassMapRegistered(typeof(Person)))
{
@@ -34,12 +48,17 @@ protected override void BuildMetadata(TypedMongoDataServiceMetadataBuilder build
cm.AddKnownType(typeof(Contractor));
});
}
+ }
- builder.SetContainer("MongoDB.Samples", "HumanResources");
- builder.AddResourceSet<Person>("People", "odata_hr", "people");
+ // This method is called only once to initialize service-wide policies.
+ public static void InitializeService(DataServiceConfiguration config)
+ {
+ Configure(config);
+ config.SetEntitySetAccessRule("*", EntitySetRights.All);
+ config.UseVerboseErrors = true;
}
- protected override MongoServer CreateDataSource()
+ protected override MongoServer CreateMongoServer()
{
var server = MongoServer.Create();
@@ -73,7 +73,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Specification.cs" />
- <Compile Include="TypedMongoDataServiceMetadataTests\When_mapping_a_hierarchy.cs" />
+ <Compile Include="TypedMetadataBuilderTests\When_mapping_a_hierarchy.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
@@ -8,7 +8,7 @@
namespace MongoDB.OData.UnitTests
{
- public abstract class Specification<TSubject>
+ internal abstract class Specification<TSubject>
{
protected TSubject Subject { get; private set; }
@@ -1,84 +1,81 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-
+using FluentAssertions;
using MongoDB.Bson.Serialization.Attributes;
-
+using MongoDB.Driver;
+using MongoDB.OData.Typed;
using NUnit.Framework;
-using FluentAssertions;
+using System;
+using System.Collections.Generic;
using System.Data.Services.Providers;
-using MongoDB.Bson.Serialization;
+using System.Linq;
namespace MongoDB.OData.UnitTests
{
[TestFixture]
- public class When_mapping_a_hierarchy : Specification<TypedMongoDataServiceMetadataBuilder>
+ internal class When_mapping_a_hierarchy : Specification<TypedMetadataBuilder>
{
- private TypedMongoDataServiceMetadataProvider _provider;
+ private IDataServiceMetadataProvider _metadata;
- protected override TypedMongoDataServiceMetadataBuilder EstablishContext()
+ protected override TypedMetadataBuilder EstablishContext()
{
- return new TypedMongoDataServiceMetadataBuilder();
+ return new TypedMetadataBuilder(typeof(Hierarchy));
}
protected override void Because()
{
- Subject.AddResourceSet<Person>("People", "hr", "people");
- _provider = Subject.CreateMetadataProvider();
+ _metadata = Subject.BuildMetadata();
}
[Test]
public void should_add_only_1_resource_set()
{
- _provider.ResourceSets.Count().Should().Be(1);
+ _metadata.ResourceSets.Count().Should().Be(1);
}
[Test]
public void should_map_the_resource_set()
{
- var set = _provider.ResourceSets.Single();
+ var set = _metadata.ResourceSets.Single();
set.ResourceType.InstanceType.Should().Be(typeof(Person));
set.ResourceType.IsAbstract.Should().BeTrue();
}
[Test]
public void should_recognize_the_derived_types()
{
- var personType = _provider.Types.Single(x => x.InstanceType == typeof(Person));
- _provider.HasDerivedTypes(personType).Should().BeTrue();
- var derivedTypes = _provider.GetDerivedTypes(personType);
+ var personType = _metadata.Types.Single(x => x.InstanceType == typeof(Person));
+ _metadata.HasDerivedTypes(personType).Should().BeTrue();
+ var derivedTypes = _metadata.GetDerivedTypes(personType);
derivedTypes.Count().Should().Be(4);
var spouseType = derivedTypes.Single(x => x.InstanceType == typeof(Spouse));
- _provider.HasDerivedTypes(spouseType).Should().BeFalse();
+ _metadata.HasDerivedTypes(spouseType).Should().BeFalse();
var employeeType = derivedTypes.Single(x => x.InstanceType == typeof(Employee));
- _provider.HasDerivedTypes(employeeType).Should().BeTrue();
- derivedTypes = _provider.GetDerivedTypes(employeeType);
+ _metadata.HasDerivedTypes(employeeType).Should().BeTrue();
+ derivedTypes = _metadata.GetDerivedTypes(employeeType);
derivedTypes.Count().Should().Be(2);
var managerType = derivedTypes.Single(x => x.InstanceType == typeof(Manager));
- _provider.HasDerivedTypes(managerType).Should().BeFalse();
+ _metadata.HasDerivedTypes(managerType).Should().BeFalse();
var contractorType = derivedTypes.Single(x => x.InstanceType == typeof(Contractor));
- _provider.HasDerivedTypes(contractorType).Should().BeFalse();
+ _metadata.HasDerivedTypes(contractorType).Should().BeFalse();
- var personRefType = _provider.Types.Single(x => x.InstanceType == typeof(PersonRef));
- _provider.HasDerivedTypes(personRefType).Should().BeTrue();
- derivedTypes = _provider.GetDerivedTypes(personRefType);
+ var personRefType = _metadata.Types.Single(x => x.InstanceType == typeof(PersonRef));
+ _metadata.HasDerivedTypes(personRefType).Should().BeTrue();
+ derivedTypes = _metadata.GetDerivedTypes(personRefType);
derivedTypes.Count().Should().Be(1);
var spouseRefType = derivedTypes.Single(x => x.InstanceType == typeof(SpouseRef));
- _provider.HasDerivedTypes(spouseRefType).Should().BeFalse();
+ _metadata.HasDerivedTypes(spouseRefType).Should().BeFalse();
}
[Test]
public void should_map_each_type_correctly()
{
- _provider.Types.Count().Should().Be(8);
+ _metadata.Types.Count().Should().Be(8);
- var personType = _provider.Types.Single(x => x.InstanceType == typeof(Person));
+ var personType = _metadata.Types.Single(x => x.InstanceType == typeof(Person));
personType.IsReadOnly.Should().BeTrue();
personType.BaseType.Should().BeNull();
personType.KeyProperties.Count.Should().Be(1);
@@ -88,7 +85,7 @@ public void should_map_each_type_correctly()
personProperties.Should().Contain(x => x.Name == "Id");
personProperties.Should().Contain(x => x.Name == "Name");
- var employeeType = _provider.Types.Single(x => x.InstanceType == typeof(Employee));
+ var employeeType = _metadata.Types.Single(x => x.InstanceType == typeof(Employee));
employeeType.IsReadOnly.Should().BeTrue();
employeeType.BaseType.Should().Be(personType);
var employeeProperties = employeeType.PropertiesDeclaredOnThisType;
@@ -97,28 +94,28 @@ public void should_map_each_type_correctly()
employeeProperties.Should().Contain(x => x.Name == "Salary");
employeeProperties.Should().Contain(x => x.Name == "Spouse");
- var spouseType = _provider.Types.Single(x => x.InstanceType == typeof(Spouse));
+ var spouseType = _metadata.Types.Single(x => x.InstanceType == typeof(Spouse));
spouseType.IsReadOnly.Should().BeTrue();
spouseType.BaseType.Should().Be(personType);
var spouseProperties = spouseType.PropertiesDeclaredOnThisType;
spouseProperties.Count.Should().Be(1);
spouseProperties.Should().Contain(x => x.Name == "SpousesId");
- var managerType = _provider.Types.Single(x => x.InstanceType == typeof(Manager));
+ var managerType = _metadata.Types.Single(x => x.InstanceType == typeof(Manager));
managerType.IsReadOnly.Should().BeTrue();
managerType.BaseType.Should().Be(employeeType);
var managerProperties = managerType.PropertiesDeclaredOnThisType;
managerProperties.Count.Should().Be(1);
managerProperties.Should().Contain(x => x.Name == "Employees");
- var contractorType = _provider.Types.Single(x => x.InstanceType == typeof(Contractor));
+ var contractorType = _metadata.Types.Single(x => x.InstanceType == typeof(Contractor));
contractorType.IsReadOnly.Should().BeTrue();
contractorType.BaseType.Should().Be(employeeType);
var contractorProperties = contractorType.PropertiesDeclaredOnThisType;
contractorProperties.Count.Should().Be(1);
contractorProperties.Should().Contain(x => x.Name == "Address");
- var nameType = _provider.Types.Single(x => x.InstanceType == typeof(Name));
+ var nameType = _metadata.Types.Single(x => x.InstanceType == typeof(Name));
nameType.IsReadOnly.Should().BeTrue();
nameType.BaseType.Should().BeNull();
nameType.KeyProperties.Count.Should().Be(0);
@@ -127,7 +124,7 @@ public void should_map_each_type_correctly()
nameProperties.Should().Contain(x => x.Name == "First");
nameProperties.Should().Contain(x => x.Name == "Last");
- var personRefType = _provider.Types.Single(x => x.InstanceType == typeof(PersonRef));
+ var personRefType = _metadata.Types.Single(x => x.InstanceType == typeof(PersonRef));
personRefType.IsReadOnly.Should().BeTrue();
personRefType.BaseType.Should().BeNull();
personRefType.KeyProperties.Count.Should().Be(0);
@@ -136,14 +133,19 @@ public void should_map_each_type_correctly()
personRefProperties.Should().Contain(x => x.Name == "Id");
personRefProperties.Should().Contain(x => x.Name == "Name");
- var spouseRefType = _provider.Types.Single(x => x.InstanceType == typeof(SpouseRef));
+ var spouseRefType = _metadata.Types.Single(x => x.InstanceType == typeof(SpouseRef));
spouseRefType.IsReadOnly.Should().BeTrue();
spouseRefType.BaseType.Should().Be(personRefType);
var spouseRefProperties = spouseRefType.PropertiesDeclaredOnThisType;
spouseRefProperties.Count.Should().Be(1);
spouseRefProperties.Should().Contain(x => x.Name == "MarriageDate");
}
+ private class Hierarchy
+ {
+ public MongoCollection<Person> People { get; set; }
+ }
+
[BsonKnownTypes(typeof(Employee), typeof(Spouse))]
private abstract class Person
{
@@ -0,0 +1,15 @@
+using System;
+
+namespace MongoDB.OData
+{
+ [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
+ public class MongoCollectionAttribute : Attribute
+ {
+ public string Name { get; private set; }
+
+ public MongoCollectionAttribute(string name)
+ {
+ Name = name;
+ }
+ }
+}
@@ -68,14 +68,14 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
- <Compile Include="TypedMongoResourcePropertyAnnotation.cs" />
- <Compile Include="TypedMongoResourceSetAnnotation.cs" />
- <Compile Include="TypedMongoDataServiceMetadataProvider.cs" />
- <Compile Include="TypedMongoDataServiceQueryProvider.cs" />
+ <Compile Include="MongoCollectionAttribute.cs" />
+ <Compile Include="MongoDatabaseAttribute.cs" />
+ <Compile Include="Typed\TypedMetadataBuilder.cs" />
+ <Compile Include="Typed\TypedResourceSetAnnotation.cs" />
+ <Compile Include="Typed\TypedMetadata.cs" />
+ <Compile Include="Typed\TypedQueryProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
- <Compile Include="TypedMongoDataService.cs" />
- <Compile Include="TypedMongoDataServiceMetadataBuilder.cs" />
- <Compile Include="TypedMongoResourceTypeAnnotation.cs" />
+ <Compile Include="MongoDataService.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Oops, something went wrong.

0 comments on commit afe9090

Please sign in to comment.