generated from SwissLife-OSS/template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fixed context sample. * Added service collection extensions. * Created simple blog sample first draft. * Added sample projects again.
- Loading branch information
Showing
24 changed files
with
333 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
samples/Context/DataAccess/Configuration/BlogCollectionConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} | ||
} |
26 changes: 0 additions & 26 deletions
26
samples/Context/DataAccess/Configuration/ProductCollectionConfiguration.cs
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
samples/Context/DataAccess/Configuration/TagCollectionConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
samples/Context/DataAccess/Configuration/UserCollectionConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.