Skip to content

Commit

Permalink
hierarchies
Browse files Browse the repository at this point in the history
  • Loading branch information
pblachut committed Mar 30, 2017
1 parent df05ffc commit 40ee2f4
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 6 deletions.
54 changes: 50 additions & 4 deletions README.md
Expand Up @@ -169,17 +169,17 @@ Document hierarchies mechanism gives possibility to define inheritance between d
TODO verify example + add query example + add comment

```csharp
public class User { }
public class Employee : User {}
public class Administrator : User {}
public class GeneralUser { }
public class Employee : GeneralUser {}
public class Administrator : GeneralUser {}

public interface IVehicle {}
public class Car : IVehicle {}
public class Toyota : Car {}

var documentStore = DocumentStore.For(storeOptions =>
{
storeOptions.Schema.For<User>()
storeOptions.Schema.For<GeneralUser>()
.AddSubClass<Employee>()
.AddSubClass(typeof (Administrator));

Expand All @@ -192,6 +192,52 @@ var documentStore = DocumentStore.For(storeOptions =>

```

Tables are created for the most general type. Each table has columns named `mt_doc_type` and `mt_dotnet_doc_type` to select appriopraite rows in the query and to deserialize JSON into appriopriate type.

get vehicles

```sql
select d.data, d.id, d.mt_doc_type, d.mt_version from public.mt_doc_ivehicle as d

```

get cars

```sql
select d.data, d.id, d.mt_doc_type, d.mt_version from public.mt_doc_ivehicle as d where d.mt_doc_type = 'toyota' or d.mt_doc_type = 'car'

```

get toyotas

```sql
select d.data, d.id, d.mt_doc_type, d.mt_version from public.mt_doc_ivehicle as d where d.mt_doc_type = 'toyota'

```

get general users

```sql
select d.data, d.id, d.mt_doc_type, d.mt_version from public.mt_doc_generaluser as d

```

get admins

```sql

select d.data, d.id, d.mt_doc_type, d.mt_version from public.mt_doc_generaluser as d where d.mt_doc_type = 'administrator'
```

get employees

```sql

select d.data, d.id, d.mt_doc_type, d.mt_version from public.mt_doc_generaluser as d where d.mt_doc_type = 'employee'

```


## Index configuration

TODO describe how to configure indexing.
Expand Down
138 changes: 138 additions & 0 deletions src/MartenWebApp/Controllers/DocumentDbController.cs
Expand Up @@ -128,6 +128,144 @@ public class BatchUsers
public List<User> Users { get; set; }
}

[HttpGet]
[Route("employees")]
public async Task<IList<Employee>> GetEmployees()
{
using (var session = _documentStore.LightweightSession())
{
return await session.Query<Employee>().ToListAsync();
}
}

[HttpPost]
[Route("employees")]
public async Task<Guid> CreateEmployee(string name)
{
var employee = new Employee
{
FirstName = name,
LastName = name
};

using (var session = _documentStore.LightweightSession())
{
session.Store(employee);

session.SaveChanges();
}

return employee.Id;
}

[HttpPost]
[Route("administrators")]
public async Task<Guid> CreateAdministrator(string name)
{
var admin = new Administrator
{
FirstName = name,
LastName = name
};

using (var session = _documentStore.LightweightSession())
{
session.Store(admin);

session.SaveChanges();
}

return admin.Id;
}

[HttpGet]
[Route("administrators")]
public async Task<IList<Administrator>> GetAdmins()
{
using (var session = _documentStore.LightweightSession())
{
return await session.Query<Administrator>().ToListAsync();
}
}

[HttpGet]
[Route("generalUsers")]
public async Task<IList<GeneralUser>> GetGeneralUsers()
{
using (var session = _documentStore.LightweightSession())
{
return await session.Query<GeneralUser>().ToListAsync();
}
}

[HttpPost]
[Route("cars")]
public async Task<Guid> CreateCar(string name)
{
var car = new Car
{
Name = name
};

using (var session = _documentStore.LightweightSession())
{
session.Store(car);

session.SaveChanges();
}

return car.Id;
}

[HttpGet]
[Route("cars")]
public async Task<IList<Car>> GetCars()
{
using (var session = _documentStore.LightweightSession())
{
return await session.Query<Car>().ToListAsync();
}
}

[HttpPost]
[Route("toyotas")]
public async Task<Guid> CreateToyota(string name)
{
var toyota = new Toyota
{
Name = name
};

using (var session = _documentStore.LightweightSession())
{
session.Store(toyota);

session.SaveChanges();
}

return toyota.Id;
}

[HttpGet]
[Route("toyotas")]
public async Task<IList<Toyota>> GetToyotas()
{
using (var session = _documentStore.LightweightSession())
{
return await session.Query<Toyota>().ToListAsync();
}
}

[HttpGet]
[Route("vehicles")]
public async Task<IList<IVehicle>> GetVehicles()
{
using (var session = _documentStore.LightweightSession())
{
return await session.Query<IVehicle>().ToListAsync();
}
}


}
}
39 changes: 37 additions & 2 deletions src/MartenWebApp/DocumentDatabaseStoreFactory.cs
@@ -1,4 +1,5 @@
using System.Configuration;
using System;
using System.Configuration;
using Marten;

namespace MartenWebApp
Expand All @@ -11,13 +12,47 @@ public static IDocumentStore CreateDocumentStore()

return DocumentStore.For(options =>
{
options.Logger(new WebConsoleMartenLogger());
options.Connection(connectionString);
options.AutoCreateSchemaObjects = AutoCreate.All;
options.Schema.For<User>().ForeignKey<Company>(x => x.CompanyId);
options.Schema.For<User>().ForeignKey<Company>(x => x.CompanyId2);
options.Schema.For<GeneralUser>()
.AddSubClass<Employee>()
.AddSubClass(typeof(Administrator));
options.Schema.For<IVehicle>()
.AddSubClassHierarchy(
typeof(Car),
typeof(Toyota)
);
});
}
}

public class GeneralUser
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Employee : GeneralUser { }
public class Administrator : GeneralUser { }

public interface IVehicle
{
Guid Id { get; set; }
string Name { get; set; }
}

public class Car : IVehicle {
public string Name { get; set; }
public Guid Id { get; set; }
}

public class Toyota : Car { }

}
1 change: 1 addition & 0 deletions src/MartenWebApp/MartenWebApp.csproj
Expand Up @@ -90,6 +90,7 @@
<Compile Include="Repository.cs" />
<Compile Include="Startup.cs" />
<Compile Include="User.cs" />
<Compile Include="WebConsoleMartenLogger.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Web.config">
Expand Down
42 changes: 42 additions & 0 deletions src/MartenWebApp/WebConsoleMartenLogger.cs
@@ -0,0 +1,42 @@
using System;
using System.Diagnostics;
using System.Linq;
using Marten;
using Marten.Services;
using Npgsql;

namespace MartenWebApp
{
public class WebConsoleMartenLogger : IMartenLogger, IMartenSessionLogger
{
public IMartenSessionLogger StartSession(IQuerySession session)
{
return (IMartenSessionLogger)this;
}

public void SchemaChange(string sql)
{
Debug.WriteLine("Executing DDL change:");
Debug.WriteLine(sql);
Debug.WriteLine(" ");
}

public void LogSuccess(NpgsqlCommand command)
{
Debug.WriteLine(command.CommandText);
}

public void LogFailure(NpgsqlCommand command, Exception ex)
{
Debug.WriteLine("Postgresql command failed!");
Debug.WriteLine(command.CommandText);
Debug.WriteLine((object)ex);
}

public void RecordSavedChanges(IDocumentSession session, IChangeSet commit)
{
IChangeSet changeSet = commit;
Debug.WriteLine(string.Format("Persisted {0} updates, {1} inserts, and {2} deletions", (object)changeSet.Updated.Count<object>(), (object)changeSet.Inserted.Count<object>(), (object)changeSet.Deleted.Count<IDeletion>()));
}
}
}

0 comments on commit 40ee2f4

Please sign in to comment.