Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Twitter.Data
- {
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using Twitter.Data.Repositories;
- using Twitter.Models;
- public class TwitterData : ITwitterData
- {
- private DbContext context;
- private IDictionary<Type, object> repositories;
- public TwitterData(DbContext context)
- {
- this.context = context;
- this.repositories = new Dictionary<Type, object>();
- }
- public ITwitterRepository<User> Users
- {
- get { return this.GetRepository<User>(); }
- }
- public ITwitterRepository<Profile> Profiles
- {
- get { return this.GetRepository<Profile>(); }
- }
- public ITwitterRepository<Tweet> Tweets
- {
- get { return this.GetRepository<Tweet>(); }
- }
- public ITwitterRepository<Message> Messages
- {
- get { return this.GetRepository<Message>(); }
- }
- public ITwitterRepository<Notification> Notifications
- {
- get { return this.GetRepository<Notification>(); }
- }
- public int SaveChanges()
- {
- return this.context.SaveChanges();
- }
- private ITwitterRepository<T> GetRepository<T>() where T : class
- {
- var typeOfRepository = typeof(T);
- if (!this.repositories.ContainsKey(typeOfRepository))
- {
- var newRepository = Activator.CreateInstance(typeof(TwitterRepository<T>), context);
- this.repositories.Add(typeOfRepository, newRepository);
- }
- return (ITwitterRepository<T>)repositories[typeOfRepository];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement