3

Всем добрый вечер. Я новичок в MongoDb, работаю с c# driver, (версия выше 2.1), работаю с добавлением бд в проект.

Пусть есть обобщенный IRepository типа:

public interface IRepository<T> where T : IEntity
{
    T GetById(Guid Id);

    IQueryable<T> GetBy(Expression<Func<T, bool>> predicate);

    IQueryable<T> GetMatchesBy<TKey>(TKey keyFilter, Expression<Func<T, TKey>> keySelector);

    IQueryable<T> GetAll();

    PaginatedList<T> Paginate<TKey>(
        int pageIndex, int pageSize,
        Expression<Func<T, TKey>> keySelector);

    PaginatedList<T> Paginate<TKey>(
        int pageIndex, int pageSize,
        Expression<Func<T, TKey>> keySelector,
        Expression<Func<T, bool>> predicate);

    //Others

}

И реализация для Mongo например :

public class MongoRepository<T> : IRepository<T> where T : IEntity
{
    private readonly IMongoClient client;

    private readonly IMongoDatabase database;

    private readonly IMongoCollection<T> collection;

    public  MongoRepository(string ConnectionString, string DatabaseName)
    {
        client = new MongoClient(ConnectionString);
        database = client.GetDatabase(DatabaseName);
        collection = database.GetCollection<T>(typeof(T).Name);
    }


    #region IRepository Members



    public T GetById(Guid Id) 
    {
        var result = collection.AsQueryable()
                        .FirstOrDefault(x => x.Id.Equals(Id));
        return result;
    }

    public IQueryable<T> GetBy(Expression<Func<T, bool>> predicate) 
    {
        var result = collection.AsQueryable()
                        .Where(predicate).AsQueryable();
        return result;
    }
}

Пусть есть коллекция из документов типа

public class EntryTree : IEntity
{
    [BsonId]
    public Guid Id { get; set; }

    [BsonRequired]
    public string Name { get; set; }

    [BsonRequired]
    public List<Category> Categories { get; set; }

    [BsonRequired]
    public List<EntryTreeNode> TreeNodes { get; set; }

    //Others
}

И недавно в слое Сервисов появилась острая необходимость добавления методов работы со вложенными документами типа EntryTreeNode.

public interface IVocabularyService
{
    PaginatedList<EntryTreeNode> GetEntryTreeNodes(
        Guid entryTreeId, 
        int pageIndex, int pageSize);

    PaginatedList<EntryTreeNode> GetEntryTreeNodesBy(
        Guid entryTreeId, 
        int pageIndex, int pageSize, 
        Expression<Func<EntryTreeNode, bool>> predicate);

    PaginatedList<EntryTreeNode> SearchEntryTreeNodesBy(
        Guid entryTreeId, 
        string searchString, 
        int pageIndex, int pageSize);
}

Однако, в реализации VocabularyService я не могу типизировать MongoRepository<T> типом EntryTreeNode, он же представляет коллекцию вложенных документов.

Но в слое Сервисов хотелось бы использовать некоторые методы, реализованные в репозитории, для вложенных документов List<EntryTreeNode> TreeNodes.

Как лучшим образом решить эту проблему?

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Посмотрите другие вопросы с метками или задайте свой вопрос.