Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections.Concurrent;
- namespace VideoStore.Infrastructure
- {
- public abstract class InMemoryRepository<T> : ICommands<T>, IQueries<T> where T : IEntity
- {
- protected static ConcurrentDictionary<int, T> items;
- public IQueryable<T> All()
- {
- return items.Values.AsQueryable();
- }
- public T ById(int id)
- {
- if (items.ContainsKey(id))
- return items[id];
- return default(T);
- }
- private object lockObj = new Object();
- public T Add(T entity)
- {
- lock (lockObj)
- {
- var newId = items.Keys.Max() + 1;
- entity.Id = newId;
- items.TryAdd(newId, entity);
- }
- return entity;
- }
- public T Update(T entity)
- {
- items[entity.Id] = entity;
- return entity;
- }
- public void Remove(int id)
- {
- T x;
- items.TryRemove(id, out x);
- }
- }
- }
Add Comment
Please, Sign In to add comment