Advertisement
Venciity

TwitterData

May 17th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1. namespace Twitter.Data
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Data.Entity;
  6.     using Twitter.Data.Repositories;
  7.     using Twitter.Models;
  8.  
  9.     public class TwitterData : ITwitterData
  10.     {
  11.         private DbContext context;
  12.         private IDictionary<Type, object> repositories;
  13.  
  14.         public TwitterData(DbContext context)
  15.         {
  16.             this.context = context;
  17.             this.repositories = new Dictionary<Type, object>();
  18.         }
  19.  
  20.         public ITwitterRepository<User> Users
  21.         {
  22.             get { return this.GetRepository<User>(); }
  23.         }
  24.  
  25.         public ITwitterRepository<Profile> Profiles
  26.         {
  27.             get { return this.GetRepository<Profile>(); }
  28.         }
  29.  
  30.         public ITwitterRepository<Tweet> Tweets
  31.         {
  32.             get { return this.GetRepository<Tweet>(); }
  33.         }
  34.  
  35.         public ITwitterRepository<Message> Messages
  36.         {
  37.             get { return this.GetRepository<Message>(); }
  38.         }
  39.  
  40.         public ITwitterRepository<Notification> Notifications
  41.         {
  42.             get { return this.GetRepository<Notification>(); }
  43.         }
  44.  
  45.         public int SaveChanges()
  46.         {
  47.             return this.context.SaveChanges();
  48.         }
  49.  
  50.         private ITwitterRepository<T> GetRepository<T>() where T : class
  51.         {
  52.             var typeOfRepository = typeof(T);
  53.             if (!this.repositories.ContainsKey(typeOfRepository))
  54.             {
  55.                 var newRepository = Activator.CreateInstance(typeof(TwitterRepository<T>), context);
  56.                 this.repositories.Add(typeOfRepository, newRepository);
  57.             }
  58.  
  59.             return (ITwitterRepository<T>)repositories[typeOfRepository];
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement