xotoi

Untitled

Dec 1st, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Sakalou.Module._9.OnlineShop;
  5. using Exceptions;
  6.  
  7. namespace DataAccess
  8. {
  9.     public class RepositoryDictionary : IRepository<Product>
  10.     {
  11.         private readonly Dictionary<int, Product> _data;
  12.  
  13.         public RepositoryDictionary(Dictionary<int, Product> data)
  14.         {
  15.             _data = data;
  16.         }
  17.  
  18.         public void Create(Product obj)
  19.         {
  20.             _data.Add(obj.Id, obj);
  21.         }
  22.  
  23.         public Product Read(int id)
  24.         {
  25.             return _data[id];
  26.         }
  27.  
  28.         public void Update(Product obj)
  29.         {
  30.             if (!_data.ContainsValue(obj))
  31.             {
  32.                 throw new NoItemException("There is no item with this id in data base");
  33.             }
  34.         }
  35.  
  36.         public void Delete(int id)
  37.         {
  38.             try
  39.             {
  40.                 _data.Remove(id);
  41.             }
  42.             catch (KeyNotFoundException e)
  43.             {
  44.                 throw new NoItemException("There is no item with this id in data base", e);
  45.             }
  46.            
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment