Headless CMS and Content Managment Hub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

121 lines
3.9 KiB

// ==========================================================================
// MongoRepositoryBase.cs
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex Group
// All rights reserved.
// ==========================================================================
using System;
using System.Globalization;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using Squidex.Infrastructure.Tasks;
#pragma warning disable RECS0108 // Warns about static fields in generic types
namespace Squidex.Infrastructure.MongoDb
{
public abstract class MongoRepositoryBase<TEntity> : IInitializable
{
private const string CollectionFormat = "{0}Set";
protected static readonly UpdateOptions Upsert = new UpdateOptions { IsUpsert = true };
protected static readonly SortDefinitionBuilder<TEntity> Sort = Builders<TEntity>.Sort;
protected static readonly UpdateDefinitionBuilder<TEntity> Update = Builders<TEntity>.Update;
protected static readonly FieldDefinitionBuilder<TEntity> Fields = FieldDefinitionBuilder<TEntity>.Instance;
protected static readonly FilterDefinitionBuilder<TEntity> Filter = Builders<TEntity>.Filter;
protected static readonly IndexKeysDefinitionBuilder<TEntity> Index = Builders<TEntity>.IndexKeys;
protected static readonly ProjectionDefinitionBuilder<TEntity> Projection = Builders<TEntity>.Projection;
private readonly IMongoDatabase mongoDatabase;
private Lazy<IMongoCollection<TEntity>> mongoCollection;
protected IMongoCollection<TEntity> Collection
{
get { return mongoCollection.Value; }
}
protected IMongoDatabase Database
{
get { return mongoDatabase; }
}
static MongoRepositoryBase()
{
RefTokenSerializer.Register();
InstantSerializer.Register();
}
protected MongoRepositoryBase(IMongoDatabase database)
{
Guard.NotNull(database, nameof(database));
mongoDatabase = database;
mongoCollection = CreateCollection();
}
protected virtual MongoCollectionSettings CollectionSettings()
{
return new MongoCollectionSettings();
}
protected virtual string CollectionName()
{
return string.Format(CultureInfo.InvariantCulture, CollectionFormat, typeof(TEntity).Name);
}
private Lazy<IMongoCollection<TEntity>> CreateCollection()
{
return new Lazy<IMongoCollection<TEntity>>(() =>
{
var databaseCollection = mongoDatabase.GetCollection<TEntity>(
CollectionName(),
CollectionSettings() ?? new MongoCollectionSettings());
SetupCollectionAsync(databaseCollection).Wait();
return databaseCollection;
});
}
protected virtual Task SetupCollectionAsync(IMongoCollection<TEntity> collection)
{
return TaskHelper.Done;
}
public virtual Task ClearAsync()
{
return Collection.DeleteManyAsync(new BsonDocument());
}
public async Task<bool> DropCollectionIfExistsAsync()
{
try
{
await mongoDatabase.DropCollectionAsync(CollectionName());
mongoCollection = CreateCollection();
return true;
}
catch
{
return false;
}
}
public void Initialize()
{
try
{
Database.ListCollections();
}
catch (Exception ex)
{
throw new ConfigurationException($"MongoDb connection failed to connect to database {Database.DatabaseNamespace.DatabaseName}", ex);
}
}
}
}