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.
nscheibe authored Dec 5, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 8157a84 commit cd94cb7
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>
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
{
@@ -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;
}
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
@@ -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>
19 changes: 19 additions & 0 deletions samples/Context/Models/Blog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Models
{
public class Blog
{
public string Id { get; set; }

public DateTime TimeStamp { get; set; }

public IEnumerable<Tag> Tags { get; set; }

public string Text { get; set; }

public string UserId { get; set; }
}
}
5 changes: 4 additions & 1 deletion samples/Context/Models/Models.csproj
Original file line number Diff line number Diff line change
@@ -2,6 +2,9 @@

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

</Project>
</Project>
13 changes: 0 additions & 13 deletions samples/Context/Models/Product.cs

This file was deleted.

9 changes: 9 additions & 0 deletions samples/Context/Models/Tag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Models
{
public class Tag
{
public string Id { get; set; }

public string Name { get; set; }
}
}
17 changes: 17 additions & 0 deletions samples/Context/Models/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace Models
{
public class User
{
public Guid Id { get; set; }

public string Email { get; set; }

public string Nickname { get; set; }

public string Firstname { get; set; }

public string Lastname { get; set; }
}
}
9 changes: 6 additions & 3 deletions samples/MongoDB.Extensions.Samples.sln
Original file line number Diff line number Diff line change
@@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "2_Domain", "2_Domain", "{22
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "3_DataAccess", "3_DataAccess", "{A880C40E-8DAF-4EFF-AE50-B8983AF424D3}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SimpleBlog", "SimpleBlog", "{1B6D9C2E-9D39-49E2-8522-29049C3D4821}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -43,9 +45,10 @@ Global
{B08692DA-8E74-4F3D-9B8E-66C5962BD7D1} = {A880C40E-8DAF-4EFF-AE50-B8983AF424D3}
{932A6303-B363-4739-9051-C195C54D9C1C} = {229E6D6C-AB33-4095-A09B-647434079280}
{D571B3C8-F426-4A5B-BEB7-0B2D5B95A6C4} = {229E6D6C-AB33-4095-A09B-647434079280}
{C4DEA4A3-B50A-4ACD-9017-4919576CE069} = {27E20B8D-3948-41A2-9271-69D850357C35}
{229E6D6C-AB33-4095-A09B-647434079280} = {27E20B8D-3948-41A2-9271-69D850357C35}
{A880C40E-8DAF-4EFF-AE50-B8983AF424D3} = {27E20B8D-3948-41A2-9271-69D850357C35}
{C4DEA4A3-B50A-4ACD-9017-4919576CE069} = {1B6D9C2E-9D39-49E2-8522-29049C3D4821}
{229E6D6C-AB33-4095-A09B-647434079280} = {1B6D9C2E-9D39-49E2-8522-29049C3D4821}
{A880C40E-8DAF-4EFF-AE50-B8983AF424D3} = {1B6D9C2E-9D39-49E2-8522-29049C3D4821}
{1B6D9C2E-9D39-49E2-8522-29049C3D4821} = {27E20B8D-3948-41A2-9271-69D850357C35}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E28A6DDE-2575-483D-8C8B-7929357BA44A}
6 changes: 3 additions & 3 deletions src/Context.Tests/MongoCollectionBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -138,7 +138,7 @@ public void WithMongoCollectionSettings_ChangeCollectionSettings_MongoCollection
var mongoCollectionBuilder = new MongoCollectionBuilder<Order>(_mongoDatabase);

// Act
mongoCollectionBuilder.WithMongoCollectionSettings(mongoCollectionSettings =>
mongoCollectionBuilder.WithCollectionSettings(mongoCollectionSettings =>
{
mongoCollectionSettings.WriteConcern = WriteConcern.Unacknowledged;
mongoCollectionSettings.ReadConcern = ReadConcern.Linearizable;
@@ -178,7 +178,7 @@ public void WithMongoCollectionSettings_DefaultCollectionSettingsConfigured_Defa
var mongoCollectionSettings = new MongoCollectionSettings();

// Act
mongoCollectionBuilder.WithMongoCollectionSettings(mongoCollectionSettings => { });
mongoCollectionBuilder.WithCollectionSettings(mongoCollectionSettings => { });
IMongoCollection <Order> result = mongoCollectionBuilder.Build();

// Assert
@@ -199,7 +199,7 @@ public void WithMongoCollectionConfiguration_ChangeCollectionConfiguration_Mongo
var mongoCollectionBuilder = new MongoCollectionBuilder<Order>(_mongoDatabase);

// Act
mongoCollectionBuilder.WithMongoCollectionConfiguration(mongoCollection =>
mongoCollectionBuilder.WithCollectionConfiguration(mongoCollection =>
{
mongoCollection.Indexes.CreateOne(new CreateIndexModel<Order>(
Builders<Order>.IndexKeys.Ascending(order => order.Name),
4 changes: 2 additions & 2 deletions src/Context/IMongoCollectionBuilder.cs
Original file line number Diff line number Diff line change
@@ -12,10 +12,10 @@ public interface IMongoCollectionBuilder<TDocument>
IMongoCollectionBuilder<TDocument> AddBsonClassMap<TMapDocument>(
Action<BsonClassMap<TMapDocument>> bsonClassMapAction) where TMapDocument : class;

IMongoCollectionBuilder<TDocument> WithMongoCollectionSettings(
IMongoCollectionBuilder<TDocument> WithCollectionSettings(
Action<MongoCollectionSettings> collectionSettings);

IMongoCollectionBuilder<TDocument> WithMongoCollectionConfiguration(
IMongoCollectionBuilder<TDocument> WithCollectionConfiguration(
Action<IMongoCollection<TDocument>> collectionConfiguration);
}
}
2 changes: 1 addition & 1 deletion src/Context/IMongoCollectionConfiguration.cs
Original file line number Diff line number Diff line change
@@ -2,6 +2,6 @@ namespace MongoDB.Extensions.Context
{
public interface IMongoCollectionConfiguration<TDocument> where TDocument : class
{
void Configure(IMongoCollectionBuilder<TDocument> mongoCollectionBuilder);
void OnConfiguring(IMongoCollectionBuilder<TDocument> mongoCollectionBuilder);
}
}
4 changes: 2 additions & 2 deletions src/Context/Internal/MongoCollectionBuilder.cs
Original file line number Diff line number Diff line change
@@ -50,15 +50,15 @@ public IMongoCollectionBuilder<TDocument> AddBsonClassMap<TMapDocument>(
return this;
}

public IMongoCollectionBuilder<TDocument> WithMongoCollectionSettings(
public IMongoCollectionBuilder<TDocument> WithCollectionSettings(
Action<MongoCollectionSettings> collectionSettings)
{
_collectionSettingsActions.Add(collectionSettings);

return this;
}

public IMongoCollectionBuilder<TDocument> WithMongoCollectionConfiguration(
public IMongoCollectionBuilder<TDocument> WithCollectionConfiguration(
Action<IMongoCollection<TDocument>> collectionConfiguration)
{
_collectionConfigurations.Add(collectionConfiguration);
2 changes: 1 addition & 1 deletion src/Context/Internal/MongoDatabaseBuilder.cs
Original file line number Diff line number Diff line change
@@ -56,7 +56,7 @@ public IMongoDatabaseBuilder ConfigureCollection<TDocument>(

var collectionBuilder = new MongoCollectionBuilder<TDocument>(mongoDb);

configuration.Configure(collectionBuilder);
configuration.OnConfiguring(collectionBuilder);
collectionBuilder.Build();

mongoCollectionBuilders.Add(typeof(TDocument), collectionBuilder);

0 comments on commit cd94cb7

Please sign in to comment.