public void UnitOfWorkCommits(Company input) { // Arrange var testSettings = new TestProjectSettings(); DbContext testContext = null; StockUnitOfWork tested = null; try { testContext = new StockContext(testSettings); testContext.Database.EnsureCreated(); tested = new StockUnitOfWork(testContext); // Act tested.StockRepository.Add(input); tested.Complete(); var expected = 1; var actual = tested.StockRepository.Count(); // Assert Assert.Equal(expected, actual); } finally { testContext?.Database.EnsureDeleted(); tested?.Dispose(); testContext?.Dispose(); } }
public void UnitOfWorkDoNotShowAsExisitngWithoutCommit(Company input) { // Arrange var testSettings = new TestProjectSettings(); DbContext testContext = null; StockUnitOfWork tested = null; try { testContext = new StockContext(testSettings); testContext.Database.EnsureCreated(); tested = new StockUnitOfWork(testContext); // Act tested.StockRepository.Add(input); var expected = 0; var actual = tested.StockRepository.GetAll().ToList().Count; // Assert Assert.Equal(expected, actual); } finally { testContext?.Database.EnsureDeleted(); tested?.Dispose(); testContext?.Dispose(); } }
public void FactoryProvidesNewInstanceEverytimeItsCalled() { // Arrange var testSettings = new TestProjectSettings(); StockContext testContext = null; StockUnitOfWorkFactory ufo = null; StockUnitOfWork uow = null; StockUnitOfWork uow2 = null; try { testContext = new StockContext(testSettings); ufo = new StockUnitOfWorkFactory(testContext); // Act uow = ufo.GetInstance(); uow2 = ufo.GetInstance(); // Assert Assert.NotEqual(uow, uow2); } finally { testContext?.Dispose(); ufo?.Dispose(); uow?.Dispose(); } }
public void Delete(string Id) { using (StockUnitOfWork db = new StockUnitOfWork()) { db.FavoriteStock.Delete(Id); db.SaveChanges(); } }
/// <summary> /// Get the last order number of favorite stock by stock category /// </summary> /// <param name="category"></param> /// <returns></returns> public int GetLastOrder(int CustomCategoryId) { int lastOrder = 0; using (StockUnitOfWork db = new StockUnitOfWork()) { var favoriteStocks = db.FavoriteStock.GetAll(); if (favoriteStocks.Count != 0) { lastOrder = favoriteStocks.Where(x => x.CustomCategory == CustomCategoryId). Max(x => x.Order); lastOrder++; } } return(lastOrder); }
public void Add(StockInfoItem item) { var stockEntity = new StockEntity { Id = item.Id, Name = item.Name, MarketType = item.MarketType, Category = item.Category }; using (StockUnitOfWork db = new StockUnitOfWork()) { db.Stock.Add(stockEntity); db.SaveChanges(); } }
public List <StockInfoItem> GetAll() { List <StockInfoItem> results = new List <StockInfoItem>(); using (StockUnitOfWork db = new StockUnitOfWork()) { List <StockEntity> stocks = db.Stock.GetAll(); foreach (var item in stocks) { results.Add(new StockInfoItem() { Id = item.Id, Name = item.Name, MarketType = item.MarketType, Category = item.Category }); } } return(results); }
static async Task Main() { var dbName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()); var proj = new ProjectSettings { ConnectionString = $"server=(localdb)\\MSSQLLocalDB;Initial Catalog={dbName};Integrated Security=True;" }; var input = GetStub(); DbContext context = null; StockUnitOfWork tested = null; try { var res = await DbManagementService.EnsureDbExists(proj); context = new StockContext(proj); tested = new StockUnitOfWork(context); tested.StockRepository.Add(input); tested.Complete(); var actual = tested.StockRepository.Count(); Console.WriteLine($"Added {actual} record(s) to db"); Console.ReadKey(); await DbManagementService.EnsureDbDoesNotExist(proj); } finally { tested?.Dispose(); if (context != null) { await context.DisposeAsync(); } } }
public List <StockInfoItem> GetAll() { List <StockInfoItem> allResult = new List <StockInfoItem>(); using (StockUnitOfWork db = new StockUnitOfWork()) { List <FavoriteStockEntity> allEntity = db.FavoriteStock.GetAll(); foreach (var item in allEntity) { allResult.Add(new StockInfoItem { Id = item.Id, // TODO : it must rewrite the category source from category table Category = item.ParentStock.Category, MarketType = item.ParentStock.MarketType, Name = item.ParentStock.Name, Order = item.Order }); } } return(allResult); }
public void Add(StockInfoItem item) { using (StockUnitOfWork db = new StockUnitOfWork()) { // check if the stock is exist in favorite table if (db.FavoriteStock.GetById(item.Id) != null) { throw new FavoriteStockExistException(item.Id, item.Name); } StockEntity stockEntity = db.Stock.GetById(item.Id); var favoriteStockEntity = new FavoriteStockEntity { Id = item.Id, ParentStock = stockEntity, // TODO : it must rewrite the category source from category table //CustomCategory = item.Category, Order = item.Order }; db.FavoriteStock.Add(favoriteStockEntity); db.SaveChanges(); } }
public StockInfoItem Get(string key) { using (StockUnitOfWork db = new StockUnitOfWork()) { StockEntity stock = db.Stock.GetById(key); if (stock == null) { stock = db.Stock.GetByName(key); if (stock == null) { throw new Exception("本機資料庫查無此筆資料,請確認此股票代號是否正確,並更新上市櫃股票資料"); } } return(new StockInfoItem() { Id = stock.Id, Name = stock.Name, MarketType = stock.MarketType, Category = stock.Category }); } }
public void Add(List <StockInfoItem> stockInfoList) { using (StockUnitOfWork db = new StockUnitOfWork()) { var existStockInfoList = db.Stock.GetAll(); List <StockEntity> stockEntityList = new List <StockEntity>(); foreach (var item in stockInfoList) { // 不存在才更新 if (existStockInfoList.FirstOrDefault(x => x.Id == item.Id) == null) { stockEntityList.Add(new StockEntity { Id = item.Id, Name = item.Name, MarketType = item.MarketType, Category = item.Category }); } } db.Stock.Add(stockEntityList); db.SaveChanges(); } }
/// <summary> /// Inititalize the data For the first time /// </summary> public void InitializeData() { StockUnitOfWork.CreateDatabase(); }