GenricRepository Sample No. 3

  1. using Microsoft.EntityFrameworkCore;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. namespace GR.Data {  
  6.     public class Repository < T > : IRepository < T > where T: BaseEntity {  
  7.         private readonly ApplicationContext context;  
  8.         private DbSet < T > entities;  
  9.         string errorMessage = string.Empty;  
  10.         public Repository(ApplicationContext context) {  
  11.             this.context = context;  
  12.             entities = context.Set < T > ();  
  13.         }  
  14.         public IEnumerable < T > GetAll() {  
  15.             return entities.AsEnumerable();  
  16.         }  
  17.         public T Get(long id) {  
  18.             return entities.SingleOrDefault(s => s.Id == id);  
  19.         }  
  20.         public void Insert(T entity) {  
  21.             if (entity == null) {  
  22.                 throw new ArgumentNullException("entity");  
  23.             }  
  24.             entities.Add(entity);  
  25.             context.SaveChanges();  
  26.         }  
  27.         public void Update(T entity) {  
  28.             if (entity == null) {  
  29.                 throw new ArgumentNullException("entity");  
  30.             }  
  31.             context.SaveChanges();  
  32.         }  
  33.         public void Delete(T entity) {  
  34.             if (entity == null) {  
  35.                 throw new ArgumentNullException("entity");  
  36.             }  
  37.             entities.Remove(entity);  
  38.             context.SaveChanges();  
  39.         }  
  40.     }  
  41. }  

Comments

Popular posts from this blog

IGenricRepository

Summary of All Data Source Patterns