Advertisement
Guest User

Simple repository sample

a guest
May 28th, 2012
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5.  
  6. namespace tacRepository
  7. {
  8.     public interface IDbSet<T>: IQueryable<T>
  9.     {
  10.         void Add(T data);
  11.         void Remove(T data);
  12.     }
  13.  
  14.     public interface IDbContext: IDisposable
  15.     {
  16.         IDbSet<T> GetDbSet<T>();
  17.         void Attach<T>(T data);
  18.         void SubmitChanges();
  19.     }
  20.  
  21.     public class Person: IIdentifiableObject<Guid>
  22.     {
  23.         public Guid Id { get; set; }
  24.         public string Name { get; set; }
  25.     }
  26.  
  27.     public class Apartment: IIdentifiableObject<Guid>
  28.     {
  29.         public Guid Id { get; set; }
  30.     }
  31.  
  32.     public interface IIdentifiableObject<out T>
  33.         where T: struct
  34.     {
  35.         T Id { get; }
  36.     }
  37.  
  38.     public class Repository<TData, TIdentifier>: IDisposable
  39.         where TIdentifier : struct
  40.         where TData : class, IIdentifiableObject<TIdentifier>
  41.     {
  42.         private readonly IDbContext _dbContext;
  43.  
  44.         public Repository(IDbContext dbContext)
  45.         {
  46.             _dbContext = dbContext;
  47.         }
  48.  
  49.         public TData Load(TIdentifier id)
  50.         {
  51.             var result = _dbContext.GetDbSet<TData>().SingleOrDefault(d => d.Id == id);
  52.             if (result == null)
  53.                 throw new ArgumentOutOfRangeException("id", id, "Object not found");
  54.             return result;
  55.         }
  56.  
  57.         public IEnumerable<TData> LoadAll()
  58.         {
  59.             return _dbContext.GetDbSet<TData>();
  60.         }
  61.  
  62.         public IEnumerable<TData> Load(Expression<Func<TData, bool>> criteria)
  63.         {
  64.             return _dbContext.GetDbSet<TData>().Where(criteria);
  65.         }
  66.  
  67.         public void Insert(TData data)
  68.         {
  69.             _dbContext.GetDbSet<TData>().Add(data);
  70.             _dbContext.SubmitChanges();
  71.         }
  72.  
  73.         public void Update(TData data)
  74.         {
  75.             _dbContext.Attach(data);
  76.             _dbContext.SubmitChanges();
  77.         }
  78.  
  79.         public void Delete(TData data)
  80.         {
  81.             _dbContext.GetDbSet<TData>().Remove(data);
  82.             _dbContext.SubmitChanges();
  83.         }
  84.  
  85.         public void Dispose()
  86.         {
  87.             _dbContext.Dispose();
  88.         }
  89.     }
  90.  
  91.     public class UsageSample
  92.     {
  93.         private readonly Repository<Person, Guid> _repository;
  94.  
  95.         public UsageSample(Repository<Person, Guid> repository)
  96.         {
  97.             _repository = repository;
  98.         }
  99.  
  100.         public void PrintPersons()
  101.         {
  102.             foreach (var person in _repository.LoadAll())
  103.             {
  104.                 Console.WriteLine(person.Name);
  105.             }
  106.         }
  107.  
  108.         public void AddPerson(string name)
  109.         {
  110.             var person = new Person
  111.                 {
  112.                     Id = Guid.NewGuid(),
  113.                     Name = name
  114.                 };
  115.             _repository.Insert(person);
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement