Skip to content

Instantly share code, notes, and snippets.

@antdimot
Last active July 7, 2022 23:59
Show Gist options
  • Select an option

  • Save antdimot/5037532 to your computer and use it in GitHub Desktop.

Select an option

Save antdimot/5037532 to your computer and use it in GitHub Desktop.

Revisions

  1. antdimot renamed this gist Aug 3, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. antdimot revised this gist Jun 13, 2017. 1 changed file with 61 additions and 57 deletions.
    118 changes: 61 additions & 57 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -21,28 +21,29 @@ public class Repository<T> where T : IEntity<ObjectId>

    public Repository( DataContext context )
    {
    _context = context;
    _collection = _context.GetDatabase().GetCollection<T>( typeof( T ).Name.ToLower() );
    _context = context;
    _collection = _context.GetDatabase().GetCollection<T>( typeof( T ).Name.ToLower() );
    }

    private IQueryable<T> CreateSet()
    {
    return _collection.AsQueryable<T>();
    return _collection.AsQueryable<T>();
    }

    public T Insert( T instance )
    {
    try
    {
    instance.Id = ObjectId.GenerateNewId();
    _collection.Insert<T>( instance );
    return instance;
    }
    catch( Exception ex )
    {
    //todo: handle exception
    throw ex;
    }
    try
    {
    instance.Id = ObjectId.GenerateNewId();
    _collection.Insert<T>( instance );

    return instance;
    }
    catch( Exception ex )
    {
    //todo: handle exception
    throw ex;
    }
    }

    public void Update( T instance )
    @@ -62,63 +63,66 @@ public void Update( T instance )

    public 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;
    }
    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 T GetById( ObjectId id )
    {
    return this.Single( o => o.Id == id );
    return this.Single( o => o.Id == id );
    }

    public T Single( Expression<Func<T, bool>> predicate = null )
    {
    var set = CreateSet();
    var query = ( predicate == null ? set : set.Where( predicate ) );
    return query.SingleOrDefault();
    var set = CreateSet();
    var query = ( predicate == null ? set : set.Where( predicate ) );

    return query.SingleOrDefault();
    }

    public IReadOnlyList<T> List( Expression<Func<T, bool>> condition = null, Func<T, string> order = null )
    {
    var set = CreateSet();
    if( condition != null )
    {
    set = set.Where( condition );
    }
    var set = CreateSet();
    if( condition != null )
    {
    set = set.Where( condition );
    }

    if( order != null )
    {
    return set.OrderBy( order ).ToList();
    }
    return set.ToList();
    if( order != null )
    {
    return set.OrderBy( order ).ToList();
    }

    return set.ToList();
    }

    public int Count( Expression<Func<T, bool>> predicate = null )
    {
    var set = CreateSet();
    return ( predicate == null ? set.Count() : set.Count( predicate ) );
    var set = CreateSet();

    return ( predicate == null ? set.Count() : set.Count( predicate ) );
    }

    public bool Exists( Expression<Func<T, bool>> predicate )
    {
    var set = CreateSet();
    return set.Any( predicate );
    var set = CreateSet();
    return set.Any( predicate );
    }
    }

    @@ -130,17 +134,17 @@ public class DataContext

    public DataContext( string dburl, string dbname )
    {
    _mongoServerUrl = dburl;
    _mongoDbName = dbname;
    _client = new MongoClient( _mongoServerUrl );
    _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 );
    var server = _client.GetServer();
    server.DropDatabase( dbName );
    }

    public void DropCollection<T>() where T : IEntity<ObjectId>
    @@ -157,7 +161,7 @@ public void DropCollection<T>() where T : IEntity<ObjectId>

    public interface IEntity<T>
    {
    T Id { get; set; }
    bool Deleted { get; set; }
    T Id { get; set; }
    bool Deleted { get; set; }
    }
    }
  3. antdimot revised this gist Jun 13, 2017. 1 changed file with 22 additions and 15 deletions.
    37 changes: 22 additions & 15 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -30,11 +30,11 @@ private IQueryable<T> CreateSet()
    return _collection.AsQueryable<T>();
    }

    public virtual T Insert( T instance )
    {
    instance.Id = ObjectId.GenerateNewId();
    public T Insert( T instance )
    {
    try
    {
    instance.Id = ObjectId.GenerateNewId();
    _collection.Insert<T>( instance );
    return instance;
    }
    @@ -45,14 +45,22 @@ public virtual T Insert( T instance )
    }
    }

    public virtual void Update( T instance )
    public void Update( T instance )
    {
    var query = Query<T>.EQ( o => o.Id, instance.Id );
    var update = Update<T>.Replace( instance );
    _collection.Update( query, update );
    try
    {
    var query = Query<T>.EQ( o => o.Id, instance.Id );
    var update = Update<T>.Replace( instance );
    _collection.Update( query, update );
    }
    catch( Exception ex )
    {
    //todo: handle exception
    throw ex;
    }
    }

    public virtual void Delete( ObjectId id, bool logical = true )
    public void Delete( ObjectId id, bool logical = true )
    {
    try
    {
    @@ -74,20 +82,19 @@ public virtual void Delete( ObjectId id, bool logical = true )
    }
    }

    public virtual T GetById( ObjectId id )
    public 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 )
    public T Single( Expression<Func<T, bool>> predicate = null )
    {
    var set = CreateSet();
    var query = ( predicate == null ? set : set.Where( predicate ) );
    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 )
    public IReadOnlyList<T> List( Expression<Func<T, bool>> condition = null, Func<T, string> order = null )
    {
    var set = CreateSet();
    if( condition != null )
    @@ -102,13 +109,13 @@ public virtual IList<T> List( Expression<Func<T, bool>> condition = null, Func<T
    return set.ToList();
    }

    public virtual int Count( Expression<Func<T, bool>> predicate = null )
    public 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 )
    public bool Exists( Expression<Func<T, bool>> predicate )
    {
    var set = CreateSet();
    return set.Any( predicate );
  4. antdimot revised this gist Jan 11, 2015. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -82,14 +82,14 @@ public virtual T GetById( ObjectId id )
    public virtual T Single( Expression<Func<T, bool>> predicate = null,
    params Expression<Func<T, object>>[] includes )
    {
    var set = CreateNotdeletedSet();
    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 = CreateNotdeletedSet();
    var set = CreateSet();
    if( condition != null )
    {
    set = set.Where( condition );
    @@ -104,13 +104,13 @@ public virtual IList<T> List( Expression<Func<T, bool>> condition = null, Func<T

    public virtual int Count( Expression<Func<T, bool>> predicate = null )
    {
    var set = CreateNotdeletedSet();
    var set = CreateSet();
    return ( predicate == null ? set.Count() : set.Count( predicate ) );
    }

    public virtual bool Exists( Expression<Func<T, bool>> predicate )
    {
    var set = CreateNotdeletedSet();
    var set = CreateSet();
    return set.Any( predicate );
    }
    }
  5. antdimot revised this gist Feb 26, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -139,11 +139,11 @@ public void DropDatabase( string dbName )
    public void DropCollection<T>() where T : IEntity<ObjectId>
    {
    var database = GetDatabase();
    var colleciontName = typeof( T ).Name.ToLower();
    var collectionName = typeof( T ).Name.ToLower();

    if( database.CollectionExists( colleciontName ) )
    if( database.CollectionExists( collectionName ) )
    {
    database.DropCollection( colleciontName );
    database.DropCollection( collectionName );
    }
    }
    }
  6. antdimot revised this gist Feb 26, 2013. No changes.
  7. antdimot renamed this gist Feb 26, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. antdimot renamed this gist Feb 26, 2013. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions gistfile1.cs → mongo_repository
    Original file line number Diff line number Diff line change
    @@ -123,13 +123,11 @@ public class DataContext

    public DataContext( string dburl, string dbname )
    {
    _mongoServerUrl = dburl;
    _mongoServerUrl = dburl;
    _mongoDbName = dbname;
    _client = new MongoClient( _mongoServerUrl );
    }
    }

    public static string ServerUrl { get { return _mongoServerUrl; } }
    public static string DbName { get { return _mongoDbName; } }
    public MongoDatabase GetDatabase() { return _client.GetServer().GetDatabase( _mongoDbName ); }

    public void DropDatabase( string dbName )
  9. antdimot created this gist Feb 26, 2013.
    158 changes: 158 additions & 0 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,158 @@
    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 = CreateNotdeletedSet();
    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 = CreateNotdeletedSet();
    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 = CreateNotdeletedSet();
    return ( predicate == null ? set.Count() : set.Count( predicate ) );
    }

    public virtual bool Exists( Expression<Func<T, bool>> predicate )
    {
    var set = CreateNotdeletedSet();
    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 static string ServerUrl { get { return _mongoServerUrl; } }
    public static string DbName { get { return _mongoDbName; } }
    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 colleciontName = typeof( T ).Name.ToLower();

    if( database.CollectionExists( colleciontName ) )
    {
    database.DropCollection( colleciontName );
    }
    }
    }

    public interface IEntity<T>
    {
    T Id { get; set; }
    bool Deleted { get; set; }
    }
    }