mirror of
https://github.com/streetwriters/notesnook-sync-server.git
synced 2026-02-12 19:22:45 +00:00
sync: make collection & db name usage more obvious
This commit is contained in:
@@ -1,36 +0,0 @@
|
||||
/*
|
||||
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
|
||||
|
||||
Copyright (C) 2023 Streetwriters (Private) Limited
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the Affero GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
Affero GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the Affero GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
using System;
|
||||
|
||||
namespace Streetwriters.Data.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
|
||||
public class BsonCollectionAttribute : Attribute
|
||||
{
|
||||
public string CollectionName { get; }
|
||||
public string DatabaseName { get; }
|
||||
|
||||
public BsonCollectionAttribute(string databaseName, string collectionName)
|
||||
{
|
||||
CollectionName = collectionName;
|
||||
DatabaseName = databaseName;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,6 @@ using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using Streetwriters.Data.Attributes;
|
||||
using Streetwriters.Data.Interfaces;
|
||||
|
||||
namespace Streetwriters.Data.Repositories
|
||||
@@ -34,19 +33,15 @@ namespace Streetwriters.Data.Repositories
|
||||
protected readonly IDbContext dbContext;
|
||||
protected IMongoCollection<TEntity> Collection { get; set; }
|
||||
|
||||
public Repository(IDbContext _dbContext)
|
||||
public Repository(IDbContext _dbContext, string databaseName, string collectionName)
|
||||
{
|
||||
dbContext = _dbContext;
|
||||
Collection = GetCollection();
|
||||
Collection = GetCollection(databaseName, collectionName);
|
||||
}
|
||||
|
||||
private protected IMongoCollection<TEntity> GetCollection()
|
||||
private protected IMongoCollection<TEntity> GetCollection(string databaseName, string collectionName)
|
||||
{
|
||||
var attribute = (BsonCollectionAttribute)typeof(TEntity).GetCustomAttributes(
|
||||
typeof(BsonCollectionAttribute),
|
||||
true).FirstOrDefault();
|
||||
if (string.IsNullOrEmpty(attribute.CollectionName) || string.IsNullOrEmpty(attribute.DatabaseName)) throw new Exception("Could not get a valid collection or database name.");
|
||||
return dbContext.GetCollection<TEntity>(attribute.DatabaseName, attribute.CollectionName);
|
||||
return dbContext.GetCollection<TEntity>(databaseName, collectionName);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user