Last active
July 7, 2022 23:59
-
-
Save antdimot/5037532 to your computer and use it in GitHub Desktop.
Revisions
-
antdimot renamed this gist
Aug 3, 2017 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
antdimot revised this gist
Jun 13, 2017 . 1 changed file with 61 additions and 57 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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() ); } private IQueryable<T> CreateSet() { 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; } } 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; } } public T GetById( ObjectId 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(); } 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 ); } 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 ) ); } public bool Exists( Expression<Func<T, bool>> 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 ); } 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> @@ -157,7 +161,7 @@ public void DropCollection<T>() where T : IEntity<ObjectId> public interface IEntity<T> { T Id { get; set; } bool Deleted { get; set; } } } -
antdimot revised this gist
Jun 13, 2017 . 1 changed file with 22 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -30,11 +30,11 @@ private IQueryable<T> CreateSet() return _collection.AsQueryable<T>(); } 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 void Update( T instance ) { 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 void Delete( ObjectId id, bool logical = true ) { try { @@ -74,20 +82,19 @@ public virtual void Delete( ObjectId id, bool logical = true ) } } public T GetById( ObjectId 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(); } 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 int Count( Expression<Func<T, bool>> predicate = null ) { 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 ); -
antdimot revised this gist
Jan 11, 2015 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 = 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 ); @@ -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 = 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 ); } } -
antdimot revised this gist
Feb 26, 2013 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 collectionName = typeof( T ).Name.ToLower(); if( database.CollectionExists( collectionName ) ) { database.DropCollection( collectionName ); } } } -
antdimot revised this gist
Feb 26, 2013 . No changes.There are no files selected for viewing
-
antdimot renamed this gist
Feb 26, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
antdimot renamed this gist
Feb 26, 2013 . 1 changed file with 2 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -123,13 +123,11 @@ public class DataContext 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 ) -
antdimot created this gist
Feb 26, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; } } }