Click here to Skip to main content
15,893,722 members
Articles / Programming Languages / C#

MongoDB GenericDAO with LINQ support

Rate me:
Please Sign up or sign in to vote.
4.86/5 (8 votes)
9 Nov 2012CPOL1 min read 32.7K   34  
A C# implementation for GenericDAO pattern for MongoDB with LINQ support.
using System;
using System.Collections.Generic;

namespace MongoDbGenericDao.Interfaces
{
    public interface IDao<T, ID> where T : MongoDBEntity
    {
        /// <summary>
        /// Gets the Entity by ID.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        T GetByID(ID id);
        /// <summary>
        /// Gets all Entities.
        /// </summary>
        /// <returns></returns>
        IEnumerable<T> GetAll();
        /// <summary>
        /// Gets the first Entity by condition.
        /// </summary>
        /// <param name="func">The condition.</param>
        /// <returns></returns>
        T GetByCondition(System.Linq.Expressions.Expression<Func<T, bool>> condition);
        /// <summary>
        /// Gets all Entities by condition.
        /// </summary>
        /// <param name="condition">The condition.</param>
        /// <returns></returns>
        IEnumerable<T> GetAll(System.Linq.Expressions.Expression<Func<T, bool>> condition);
        /// <summary>
        /// GetAll by query
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="top">TOP Clause, if is [0] will be ignored</param>
        /// <param name="orderByDescending">If is [true] order by desc - If is [false] order by asc</param>
        /// <returns></returns>
        IEnumerable<T> GetAll(System.Linq.Expressions.Expression<Func<T, bool>> condition, int maxresult, bool orderByDescending);
        /// <summary>
        /// Saves the Entity.
        /// </summary>
        /// <param name="pobject">The pobject.</param>
        /// <returns></returns>
        T Save(T pobject);
        /// <summary>
        /// Deletes the Entity.
        /// </summary>
        /// <param name="pobject">The pobject.</param>
        void Delete(T pobject);
        /// <summary>
        /// Counts the specified condition.
        /// </summary>
        /// <param name="condition">The condition.</param>
        /// <returns></returns>
        long Count(System.Linq.Expressions.Expression<Func<T, bool>> condition);
        /// <summary>
        /// Paginates the specified func.
        /// </summary>
        /// <param name="func">The func.</param>
        /// <param name="pagesize">The pagesize.</param>
        /// <param name="page">The page.</param>
        /// <param name="pOrderByDescending">if set to <c>true</c> [p order by descending].</param>
        /// <returns></returns>
        IEnumerable<T> Paginate(System.Linq.Expressions.Expression<Func<T, bool>> func, int pagesize, int page, bool pOrderByDescending);
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect America-NET Ltda.
Brazil Brazil
Lazaro is a brazilian software architect, live in São Paulo, focusing on web development, NoSql and agile practices for startups.

In his personal life he is a part time runner, speaker, amateur astronomer, gamer and an absorbing knowledge ambulant.

Comments and Discussions