Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

MongoDB GenericDao to manage MongoCollections with Linq support

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 MongoDbGenericDao
Octocat-spinner-32 .gitattributes
Octocat-spinner-32 .gitignore
Octocat-spinner-32 README.markdown
README.markdown

MongoDB GenericDao

C# implementation to GenericDao pattern using MongoDB Driver and Linq support

What methods are supported?

  • T GetByID(ID id);
  • IEnumerable GetAll();
  • T GetByCondition(System.Linq.Expressions.Expression> condition);
  • IEnumerable GetAll(System.Linq.Expressions.Expression> condition);
  • IEnumerable GetAll(System.Linq.Expressions.Expression> condition, int maxresult, bool orderByDescending);
  • T Save(T pobject);
  • void Delete(T pobject);
  • long Count();
  • long Count(System.Linq.Expressions.Expression> condition);
  • IEnumerable Paginate(System.Linq.Expressions.Expression> condition, int pagesize, int page, Func pOrderByClause = null, bool pOrderByDescending = false);
  • IEnumerable Paginate(int pagesize, int page, Func pOrderByClause = null, bool pOrderByDescending = false);
  • IEnumerable Search(string field, string search, int page, int pagesize, out long foundedRecords); #Install nuget package
PM> Install-Package MongoDB-GenericDao

How to use?

Each class must have an inheritance from MongoDBGenericDao.MongoDBEntity class

Create a class

public class Message : MongoDBEntity
{
    public DateTime DateCreated { get; set; }
    public string From { get; set; }
    public List<string> To { get; set; }
    public string Content { get; set; }
}

Create a business class

public interface IBMessage : IDao<Message, string>
{
    Message AddNewMessage(string from, List<string> to, string messagetext);
}

public class BMessage : MongoDBGenericDao<Message>, IBMessage
{
    public BMessage(string mongodbconnectionstring) : base(mongodbconnectionstring) { }

    public Message AddNewMessage(string from, List<string> to, string messagetext)
    {
        if(to == null || to.Count == 0)
            throw new ApplicationException("'to' cannot be empty");

        var message = new Message
            {
                DateCreated = DateTime.Now.ToUniversalTime(), //ToUniversalTime() provides datetime serialization
                From = from,
                To = to
            };

        return this.Save(message); //saves in mongodb and return updated reference
    }
}

Usage

IBMessage bmessage = new BMessage("[your connectionstring here. ex: server=appserver-db;database=mymongodbdatabasename]");

var message = bmessage.AddNewMessage(   "mongodb@mongodb.com",
                                        new List<string>().Add("nononono@nonono.com"),
                                        "Message content nonononononononono");
var messageid = message.Id;


var top50MessagesThatContainsHelloWordMessage = bmessage.GetAll(x=>x.Content.Contains("hello world"), 50, true);

//remove the first message from top50MessagesThatContainsHelloWordMessage
bmessage.Delete(top50MessagesThatContainsHelloWordMessage.First());


//paginate first page based in date created order by descending
var last10 = bmessage.Paginate(1,10,x=>x.DateCreated,true);

//search
var user_by_domain_email = bmessage.Search("Email", "@email.com", 1, 10, out totalrecords);

Something went wrong with that request. Please try again.