New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Generic Repository v2

101 Views
Copy Code Show/Hide Line Numbers
   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Web;
   5:  using System.Data.Linq;
   6:   
   7:  namespace HelpDesk.Models {
   8:      public interface IGenericRepository {
   9:          IQueryable<T> Get<T>() where T : class;
  10:          void Insert<T>(T item) where T : class;
  11:          void Insert<T>(IEnumerable<T> items) where T : class;
  12:          void Delete<T>(T item) where T : class;
  13:          void Delete<T>(IEnumerable<T> items) where T : class;
  14:          void Save();
  15:      }
  16:   
  17:      public class GenericRepository : IGenericRepository {
  18:          DataContext db;
  19:   
  20:          public GenericRepository(DataContext dataContext) {
  21:              this.db = dataContext;
  22:          }
  23:   
  24:          public IQueryable<T> Get<T>() where T : class {
  25:              return db.GetTable<T>();
  26:          }
  27:   
  28:          public void Insert<T>(T entity) where T : class {
  29:              db.GetTable<T>().InsertOnSubmit(entity);
  30:          }
  31:   
  32:          public void Insert<T>(IEnumerable<T> entities) where T : class {
  33:              db.GetTable<T>().InsertAllOnSubmit(entities);
  34:          }
  35:   
  36:          public void Delete<T>(T entity) where T : class {
  37:              db.GetTable<T>().DeleteOnSubmit(entity);
  38:          }
  39:   
  40:          public void Delete<T>(IEnumerable<T> entities) where T : class {
  41:              db.GetTable<T>().DeleteAllOnSubmit(entities);
  42:          }
  43:   
  44:          public void Save() {
  45:              db.SubmitChanges();
  46:          }
  47:      }
  48:  }
by davecowart
  March 17, 2010 @ 1:49pm
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies


If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate