Skip to content

Instantly share code, notes, and snippets.

@mahizsas
Forked from antdimot/Repository.cs
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save mahizsas/2488016ae88230c42d09 to your computer and use it in GitHub Desktop.

Select an option

Save mahizsas/2488016ae88230c42d09 to your computer and use it in GitHub Desktop.
Sample repository with MongoDb
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;
namespace PRJ.Data
{
public class Repository<T> where T : IEntity<ObjectId>
{
DataContext _context;
MongoCollection<T> _collection;
public MongoCollection<T> Collection { get { return _collection; } }
public Repository( DataContext context )
{
_context = context;
_collection = _context.GetDatabase().GetCollection<T>( typeof( T ).Name.ToLower() );
}
private IQueryable<T> CreateSet()
{
return _collection.AsQueryable<T>();
}
public virtual T Insert( T instance )
{
instance.Id = ObjectId.GenerateNewId();
try
{
_collection.Insert<T>( instance );
return instance;
}
catch( Exception ex )
{
//todo: handle exception
throw ex;
}
}
public virtual void Update( T instance )
{
var query = Query<T>.EQ( o => o.Id, instance.Id );
var update = Update<T>.Replace( instance );
_collection.Update( query, update );
}
public virtual void Delete( ObjectId id, bool logical = true )
{
try
{
if( logical )
{
_collection.Update(
Query<T>.EQ<ObjectId>( p => p.Id, id ),
Update<T>.Set<bool>( p => p.Deleted, true ) );
}
else
{
_collection.Remove( Query<T>.EQ<ObjectId>( p => p.Id, id ) );
}
}
catch( Exception ex )
{
//todo: handle exception
throw ex;
}
}
public virtual T GetById( ObjectId id )
{
return this.Single( o => o.Id == id );
}
public virtual T Single( Expression<Func<T, bool>> predicate = null,
params Expression<Func<T, object>>[] includes )
{
var set = CreateSet();
var query = ( predicate == null ? set : set.Where( predicate ) );
return query.SingleOrDefault();
}
public virtual IList<T> List( Expression<Func<T, bool>> condition = null, Func<T, string> order = null )
{
var set = CreateSet();
if( condition != null )
{
set = set.Where( condition );
}
if( order != null )
{
return set.OrderBy( order ).ToList();
}
return set.ToList();
}
public virtual int Count( Expression<Func<T, bool>> predicate = null )
{
var set = CreateSet();
return ( predicate == null ? set.Count() : set.Count( predicate ) );
}
public virtual bool Exists( Expression<Func<T, bool>> predicate )
{
var set = CreateSet();
return set.Any( predicate );
}
}
public class DataContext
{
string _mongoServerUrl;
string _mongoDbName;
MongoClient _client;
public DataContext( string dburl, string dbname )
{
_mongoServerUrl = dburl;
_mongoDbName = dbname;
_client = new MongoClient( _mongoServerUrl );
}
public MongoDatabase GetDatabase() { return _client.GetServer().GetDatabase( _mongoDbName ); }
public void DropDatabase( string dbName )
{
var server = _client.GetServer();
server.DropDatabase( dbName );
}
public void DropCollection<T>() where T : IEntity<ObjectId>
{
var database = GetDatabase();
var collectionName = typeof( T ).Name.ToLower();
if( database.CollectionExists( collectionName ) )
{
database.DropCollection( collectionName );
}
}
}
public interface IEntity<T>
{
T Id { get; set; }
bool Deleted { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment