Created
May 2, 2019 17:23
-
-
Save zinoviy23/f405dce1654f22ae5d0ec77e9a5f975f to your computer and use it in GitHub Desktop.
Task for simulating SQL db with linq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Sockets; | |
namespace DbTask | |
{ | |
interface IEntity | |
{ | |
long Id { get; } | |
} | |
interface IEntityFactory<out T> | |
{ | |
T Instance { get; } | |
} | |
class DataBaseException : Exception | |
{ | |
public DataBaseException() | |
{ | |
} | |
public DataBaseException(string message) : base(message) | |
{ | |
} | |
} | |
class DataBase | |
{ | |
private readonly IDictionary<Type, object> _tables = new Dictionary<Type, object>(); | |
public string Name { get; } | |
public DataBase(string name) | |
{ | |
Name = name; | |
} | |
public void CreateTable<T>() where T : IEntity | |
{ | |
Type tableType = typeof(T); | |
if (_tables.ContainsKey(tableType)) | |
throw new DataBaseException($"Table already exists {tableType.Name}!"); | |
_tables[tableType] = new List<T>(); | |
} | |
public void InsertInto<T>(IEntityFactory<T> values) where T : IEntity | |
{ | |
Type tableType = typeof(T); | |
if (!_tables.ContainsKey(tableType)) | |
throw new DataBaseException($"Unknown table {tableType.Name}!"); | |
((List<T>) _tables[tableType]).Add(values.Instance); | |
} | |
public IEnumerable<T> Table<T>() where T : IEntity | |
{ | |
Type tableType = typeof(T); | |
if (!_tables.ContainsKey(tableType)) | |
throw new DataBaseException($"Unknown table {tableType.Name}!"); | |
return (IEnumerable<T>) _tables[tableType]; | |
} | |
} | |
class Shop : IEntity | |
{ | |
public long Id { get; } | |
public string Name { get; } | |
public Shop(long id, string name) | |
{ | |
Id = id; | |
Name = name; | |
} | |
} | |
class ShopFactory : IEntityFactory<Shop> | |
{ | |
private static long _id = 0; | |
private string _name; | |
public ShopFactory(string name) | |
{ | |
_name = name; | |
} | |
public Shop Instance => new Shop(_id++, _name); | |
} | |
class Good : IEntity | |
{ | |
public long Id { get; } | |
public string Name { get; } | |
public long ShopId { get; } | |
public Good(long id, string name, long shopId) | |
{ | |
Id = id; | |
Name = name; | |
ShopId = shopId; | |
} | |
} | |
class GoodFactory : IEntityFactory<Good> | |
{ | |
private static long _id = 0; | |
private string _name; | |
private long _shopId; | |
public GoodFactory(string name, long shopId) | |
{ | |
_name = name; | |
_shopId = shopId; | |
} | |
public Good Instance => new Good(_id++, _name, _shopId); | |
} | |
class Sale : IEntity | |
{ | |
public long Id { get; } | |
public Sale(long id) | |
{ | |
Id = id; | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
DataBase db = new DataBase("ShopDb"); | |
db.CreateTable<Good>(); | |
db.CreateTable<Shop>(); | |
// <Shop> for similarity with SQL | |
db.InsertInto<Shop>(new ShopFactory("Auchan")); | |
db.InsertInto<Shop>(new ShopFactory("Magnit")); | |
db.InsertInto<Good>(new GoodFactory("Pepsi", 0)); | |
db.InsertInto(new GoodFactory("3 korochki", 0)); | |
db.InsertInto(new GoodFactory("Ohota", 1)); | |
db.InsertInto(new GoodFactory("Lays", 1)); | |
var auchanId = (from shop in db.Table<Shop>() where shop.Name == "Auchan" select shop.Id).First(); | |
var allAuchanGoods = from good in db.Table<Good>() where good.ShopId == auchanId select good.Name; | |
foreach (var goodName in allAuchanGoods) | |
{ | |
Console.WriteLine(goodName); | |
} | |
try | |
{ | |
var sales = from sale in db.Table<Sale>() select sale; | |
Console.WriteLine(sales.FirstOrDefault()); | |
} | |
catch (DataBaseException ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment