Skip to content

Commit

Permalink
Created simple blog sample (#8)
Browse files Browse the repository at this point in the history
* Fixed context sample.

* Added service collection extensions.

* Created simple blog sample first draft.

* Added sample projects again.
  • Loading branch information
nscheibe authored Dec 5, 2019
1 parent 8157a84 commit cd94cb7
Show file tree
Hide file tree
Showing 24 changed files with 333 additions and 107 deletions.
33 changes: 33 additions & 0 deletions samples/Context/DataAccess/BlogRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Models;
using MongoDB.Driver;

namespace DataAccess
{
public class BlogRepository
{
private IMongoCollection<Blog> _mongoCollection;

public BlogRepository(SimpleBlogDbContext simpleBlogDbContext)
{
if (simpleBlogDbContext == null)
throw new ArgumentNullException(nameof(simpleBlogDbContext));

_mongoCollection = simpleBlogDbContext.CreateCollection<Blog>();
}

public async Task AddBlogAsync(
Blog blog, CancellationToken cancellationToken)
{
var insertOneOptions = new InsertOneOptions()
{
BypassDocumentValidation = false
};

await _mongoCollection
.InsertOneAsync(blog, insertOneOptions, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Models;
using MongoDB.Driver;
using MongoDB.Extensions.Context;

namespace DataAccess
{
internal class BlogCollectionConfiguration : IMongoCollectionConfiguration<Blog>
{
public void Configure(IMongoCollectionBuilder<Blog> mongoCollectionBuilder)
{
mongoCollectionBuilder
.WithCollectionName("blogs")
.AddBsonClassMap<Blog>(cm =>
{
cm.AutoMap();
cm.MapIdMember<string>(c => c.Id);
})
.WithMongoCollectionSettings(settings => settings.ReadConcern = ReadConcern.Majority)
.WithMongoCollectionSettings(settings => settings.ReadPreference = ReadPreference.Nearest)
.WithMongoCollectionConfiguration(collection =>
{
var timestampIndex = new CreateIndexModel<Blog>(
Builders<Blog>.IndexKeys.Ascending(blog => blog.TimeStamp),
new CreateIndexOptions { Unique = false });

collection.Indexes.CreateOne(timestampIndex);
});
}
}
}

This file was deleted.

31 changes: 0 additions & 31 deletions samples/Context/DataAccess/Configuration/ShopDbContext.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Models;
using MongoDB.Driver;
using MongoDB.Extensions.Context;
using Tag = Models.Tag;

namespace DataAccess
{
internal class TagCollectionConfiguration : IMongoCollectionConfiguration<Tag>
{
public void Configure(IMongoCollectionBuilder<Tag> mongoCollectionBuilder)
{
mongoCollectionBuilder
.AddBsonClassMap<Tag>(cm => cm.AutoMap())
.WithMongoCollectionSettings(setting =>
{
setting.ReadPreference = ReadPreference.Nearest;
setting.ReadConcern = ReadConcern.Available;
setting.WriteConcern = WriteConcern.Acknowledged;
})
.WithMongoCollectionConfiguration(collection =>
{
var timestampIndex = new CreateIndexModel<Tag>(
Builders<Tag>.IndexKeys.Ascending(tag => tag.Name),
new CreateIndexOptions { Unique = false });

collection.Indexes.CreateOne(timestampIndex);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using Models;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using MongoDB.Extensions.Context;

namespace DataAccess
{
internal class UserCollectionConfiguration : IMongoCollectionConfiguration<User>
{
public void Configure(IMongoCollectionBuilder<User> mongoCollectionBuilder)
{
mongoCollectionBuilder
.WithCollectionName("users")
.AddBsonClassMap<User>(ConfigureUserClassMap())
.WithMongoCollectionSettings(ConfigureCollectionSettings())
.WithMongoCollectionConfiguration(ConfigureIndexes());
}

private static Action<MongoCollectionSettings> ConfigureCollectionSettings()
{
return setting =>
{
setting.WriteConcern = WriteConcern.WMajority.With(journal: true);
setting.ReadConcern = ReadConcern.Majority;
setting.ReadPreference = ReadPreference.Primary;
};
}

private static Action<IMongoCollection<User>> ConfigureIndexes()
{
return collection =>
{
var emailIndex = new CreateIndexModel<User>(
Builders<User>.IndexKeys.Ascending(user => user.Email),
new CreateIndexOptions { Unique = true });

var nicknameIndex = new CreateIndexModel<User>(
Builders<User>.IndexKeys.Ascending(user => user.Nickname),
new CreateIndexOptions { Unique = true });

var firstname = new CreateIndexModel<User>(
Builders<User>.IndexKeys.Ascending(user => user.Firstname),
new CreateIndexOptions { Unique = false });

var secondname = new CreateIndexModel<User>(
Builders<User>.IndexKeys.Ascending(user => user.Lastname),
new CreateIndexOptions { Unique = false });

collection.Indexes.CreateMany(
new[] { emailIndex, nicknameIndex, firstname, secondname });
};
}

private static Action<BsonClassMap<User>> ConfigureUserClassMap()
{
return cm =>
{
cm.AutoMap();
cm.MapIdMember(c => c.Id);
};
}
}
}
8 changes: 6 additions & 2 deletions samples/Context/DataAccess/DataAccess.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>SimpleBlog.DataAccess</AssemblyName>
<RootNamespace>SimpleBlog.DataAccess</RootNamespace>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.0" />
<PackageReference Include="MongoDB.Extensions.Context" Version="0.1.0-preview1" />
</ItemGroup>
Expand Down
18 changes: 0 additions & 18 deletions samples/Context/DataAccess/ProductRepository.cs

This file was deleted.

11 changes: 8 additions & 3 deletions samples/Context/DataAccess/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Extensions.Context;

namespace DataAccess
{
Expand All @@ -8,9 +9,13 @@ public static class ServiceCollectionExtensions
private static IServiceCollection AddShopDatabase(
this IServiceCollection services, IConfiguration configuration)
{
//MongoOptions dbOptions = configuration
// .GetSection(Wellknown.Configuration.Sections.Database)
// .Get<MongoOptions<IDocuStoreDbContext>>()
MongoOptions shopDbOptions = configuration
.GetSection("Shop:Database")
.Get<MongoOptions>();

services.AddSingleton<MongoOptions>(sp => shopDbOptions);
services.AddSingleton<SimpleBlogDbContext>();
services.AddSingleton<BlogRepository>();

return services;
}
Expand Down
26 changes: 26 additions & 0 deletions samples/Context/DataAccess/SimpleBlogDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver;
using MongoDB.Extensions.Context;

namespace DataAccess
{
public class SimpleBlogDbContext : MongoDbContext
{
public SimpleBlogDbContext(MongoOptions mongoOptions) : base(mongoOptions)
{
}

protected override void OnConfiguring(IMongoDatabaseBuilder mongoDatabaseBuilder)
{
mongoDatabaseBuilder
.RegisterCamelCaseConventionPack()
.RegisterSerializer(new DateTimeOffsetSerializer())
.ConfigureConnection(con => con.ReadConcern = ReadConcern.Majority)
.ConfigureConnection(con => con.WriteConcern = WriteConcern.WMajority)
.ConfigureConnection(con => con.ReadPreference = ReadPreference.Primary)
.ConfigureCollection(new UserCollectionConfiguration())
.ConfigureCollection(new BlogCollectionConfiguration())
.ConfigureCollection(new TagCollectionConfiguration());
}
}
}
34 changes: 34 additions & 0 deletions samples/Context/DataAccess/TagRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;
using Tag = Models.Tag;

namespace DataAccess
{
public class TagRepository
{
private InsertOneOptions _insertOneOptions;
private IMongoCollection<Tag> _mongoCollection;

public TagRepository(SimpleBlogDbContext simpleBlogDbContext)
{
if (simpleBlogDbContext == null)
throw new ArgumentNullException(nameof(simpleBlogDbContext));

_mongoCollection = simpleBlogDbContext.CreateCollection<Tag>();

_insertOneOptions = new InsertOneOptions()
{
BypassDocumentValidation = false
};
}

public async Task AddTagAsync(
Tag tag, CancellationToken cancellationToken)
{
await _mongoCollection
.InsertOneAsync(tag, _insertOneOptions, cancellationToken);
}
}
}
34 changes: 34 additions & 0 deletions samples/Context/DataAccess/UserRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Models;
using MongoDB.Driver;

namespace DataAccess
{
public class UserRepository
{
private InsertOneOptions _insertOneOptions;
private IMongoCollection<User> _mongoCollection;

public UserRepository(SimpleBlogDbContext simpleBlogDbContext)
{
if (simpleBlogDbContext == null)
throw new ArgumentNullException(nameof(simpleBlogDbContext));

_mongoCollection = simpleBlogDbContext.CreateCollection<User>();

_insertOneOptions = new InsertOneOptions()
{
BypassDocumentValidation = false
};
}

public async Task AddUserAsync(
User user, CancellationToken cancellationToken)
{
await _mongoCollection
.InsertOneAsync(user, _insertOneOptions, cancellationToken);
}
}
}
5 changes: 4 additions & 1 deletion samples/Context/Domain/Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>SimpleBlog.Domain</AssemblyName>
<RootNamespace>SimpleBlog.Domain</RootNamespace>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Models\Models.csproj" />
</ItemGroup>

</Project>
</Project>
Loading

0 comments on commit cd94cb7

Please sign in to comment.