public UserAccount() { _uow = new UnitOfWork<UsersAccountDbContext>(); _user = new UserProfileRepository(_uow); _roles = new webpages_RolesRepository(_uow); _inRoles = new webpages_UsersInRolesRepository(_uow); }
public Northwind() { _uow = new UnitOfWork<NorthwindDbContext>(); _category = new CategoryRepository(_uow); _orders = new OrdersRepository(_uow); _customers = new CustomerRepository(_uow); _products = new ProductsRepository(_uow); }
private Token CreateToken(User user) { Token token = new Token(); token.Key = Guid.NewGuid().ToString(); token.UserId = user.Id; token.Issued = DateTime.Now; using (var uow = new UnitOfWork()) { uow.TokenRepository.Create(token); } return token; }
public void ShouldNotCommitToDatabaseOnDataChange() { //Arrange IDbContext mockContext = MockRepository.GenerateMock<IDbContext>(); IUnitOfWork unitOfWork = new UnitOfWork(mockContext); mockContext.Stub(x => x.Find<Blog>()).Return(new List<Blog>() { new Blog() { Id = 1, Title = "Test" } }.AsQueryable()); IBlogRepository repository = new BlogRepository(unitOfWork); var items = repository.Set<Blog>(); //Act items.First().Title = "Not Going to be Written"; //Assert mockContext.AssertWasNotCalled(x => x.SaveChanges()); }
public AuthenticationVM AuthByLogin(string email, string password) { AuthenticationVM vm = null; using (var uow = new UnitOfWork()) { var user = uow.UserRepository.GetByEmail(email) .SingleOrDefault(u=>u.Password == password); if (user != null) { Token token = CreateToken(user); vm = new AuthenticationVM(); vm.Key = token.Key; vm.UserId = user.Id; } } return vm; }
public void DupmCellMeanVectors() { var wr = GetFileWriterForDumpingCellMeanVectors(); using (var work = new UnitOfWork()) { var dfoDataCellRepository = RepositoryContainer.GetRepository<Cell>(work); var sqlQuery = string.Format("Select * from Cells"); var dfoDataCells = dfoDataCellRepository.ExecuteCommand<Cell>(sqlQuery); var msg = string.Format("Total {0} cell mean vector is being dumping ...", dfoDataCells.Count); Console.WriteLine(msg); foreach (var dfoDataCell in dfoDataCells) { wr.WriteLine("{0},{1},{2},{3}", dfoDataCell.X, dfoDataCell.Y, dfoDataCell.U, dfoDataCell.V); } } wr.Close(); Console.WriteLine("Done!!"); }
public void ShouldNotCommitOnError() { //Arrange IDbContext mockContext = MockRepository.GenerateMock<IDbContext>(); IUnitOfWork unitOfWork = new UnitOfWork(mockContext); mockContext.Stub(x => x.Find<Blog>()).Return(new List<Blog>() { new Blog() { Id = 1, Title = "Test" } }.AsQueryable()); mockContext.Stub(x => x.SaveChanges()).Throw(new ApplicationException()); IBlogRepository repository = new BlogRepository(unitOfWork); var items = repository.Set<Blog>(); items.First().Title = "Not Going to be Written"; //Act try { repository.SaveChanges(); } catch (Exception) { } //Assert mockContext.AssertWasCalled(x => x.Rollback()); }
public void TestMethod1() { using (IUnitOfWork unitOfWork = new UnitOfWork("TestData")) { try { unitOfWork.Open(); var command = new GetTestDataCommand() .AddParameters("@EntityName", "Test"); Entities entities = command.Execute(unitOfWork).FirstOrDefault(); Assert.IsNotNull(entities); Assert.AreEqual("Test", entities.EntityName); Assert.AreEqual("Test entity for use with the data access code", entities.Description); } finally { unitOfWork.Close(); } } }
private AuthenticationVM AuthByKey(string key) { AuthenticationVM vm = null; using (var uow = new UnitOfWork()) { Token token = uow.TokenRepository.GetByKey(key).SingleOrDefault(); if (token != null) { if ((DateTime.Now - token.Issued) > Settings.GetAuthExpiration()) { throw new ExpiredAuthenticationException(); } else { vm = new AuthenticationVM(); vm.Key = token.Key; vm.UserId = token.UserId; } } } return vm; }
public Response<List<CellVector>> GetAllCellVector() { //Thread.Sleep(2000); var response = new Response<List<CellVector>>(); try { using (var work = new UnitOfWork()) { var cellRepository = RepositoryContainer.GetRepository<Cell>(work); const string sqlQuery = "select X,Y,U,V from Cells"; var cellVectors = cellRepository.ExecuteCommand<CellVector>(sqlQuery); response.Data = cellVectors; return response; } } catch (Exception) { response.Success = false; response.ErrorMessage = "failed"; return response; } }
public void DeletePreviousData() { try { using (var work = new UnitOfWork()) { Console.WriteLine("Deleting Previous Data"); var dfoDataRepository = RepositoryContainer.GetRepository<DataAccess.DFOdata>(work); dfoDataRepository.ExecuteCommandDirectly("delete from DFOdata"); //foreach (var dfOdata in dfoDataRepository.All()) //{ // dfoDataRepository.Remove(dfOdata); //} //work.SaveChanges(); Console.WriteLine("Done"); } } catch (Exception ex) { throw new Exception("Data deletion fails !"); } }
public void ShouldReadToDatabaseOnRead() { //Arrange IDbContext mockContext = MockRepository.GenerateMock<IDbContext>(); IUnitOfWork unitOfWork = new UnitOfWork(mockContext); IBlogRepository repository = new BlogRepository(unitOfWork); //Act var items = repository.Set<Blog>(); //Assert mockContext.AssertWasCalled(x => x.Find<Blog>()); }
private void GetMeanVectorAndSelectedPoints(StreamWriter wr1, StreamWriter wr2, StreamWriter wr3, UnitOfWork work) { var dfoDataRepository = RepositoryContainer.GetRepository<DataAccess.DFOdata>(work); //BySpiralFasion(wr1, wr2, dfoDataRepository); ByGridFasion(wr1, wr2, wr3, dfoDataRepository); }
public void InsertNewDFOData() { var folderPath = GetDataFolderPath(); var xyFilePath = folderPath + "\\paresedXY.txt"; var velocityPath = folderPath + "\\parsedVelocity.txt"; var xyFileLines = File.ReadAllLines(xyFilePath); var velocityFileLines = File.ReadAllLines(velocityPath); try { using (var work = new UnitOfWork()) { var dfoDataRepository = RepositoryContainer.GetRepository<DataAccess.DFOdata>(work); for (int i = 0; i < xyFileLines.Count(); i++) { var xyFileLine = xyFileLines.ElementAt(i).Split(','); var velocityFileLine = velocityFileLines.ElementAt(i).Split(','); if (xyFileLine.Count() == 3 && velocityFileLine.Count() == 3) { var x = float.Parse(xyFileLine[1], CultureInfo.InvariantCulture.NumberFormat); var y = float.Parse(xyFileLine[2], CultureInfo.InvariantCulture.NumberFormat); var u = float.Parse(velocityFileLine[1], CultureInfo.InvariantCulture.NumberFormat); var v = float.Parse(velocityFileLine[2], CultureInfo.InvariantCulture.NumberFormat); var dfOdata = new DataAccess.DFOdata() { X = x, Y = y, U = u, V = v }; dfoDataRepository.Insert(dfOdata); Console.WriteLine(xyFileLine[0]); } else { throw new Exception("txt files is not in perfect format !"); } } Console.WriteLine("Saving new data"); work.SaveChanges(); Console.WriteLine("Done"); } } catch (Exception ex) { } }
public ActionResult EditUser(UsersEditUserVM model, string[] assignedRoles) { LibraryManagementSystemContext context = new LibraryManagementSystemContext(); ModelState.Remove("DateOut"); UsersRepository usersRepositoryContext = new UsersRepository(context); if (model.Email != null && usersRepositoryContext.GetAll().Any(u => u.Email == model.Email) && model.ID != usersRepositoryContext.GetAll(filter: u => u.Email == model.Email).FirstOrDefault().ID) { ModelState.AddModelError("Email", "* email already exists"); } if (model.ID <= 0 && string.IsNullOrEmpty(model.Password)) { this.ModelState.AddModelError("Password", "* password required"); } if (!this.ModelState.IsValid) { RolesRepository rolesRepository = new RolesRepository(context); var allRoles = rolesRepository.GetAll(); List<AssignedRolesVM> assignedRolesViewModel = new List<AssignedRolesVM>(); foreach (var role in allRoles) { assignedRolesViewModel.Add(new AssignedRolesVM { ID = role.ID, Name = role.Name, IsAssigned = false }); } ViewBag.Roles = assignedRolesViewModel; return View(model); } TryUpdateModel(model); User user = null; using (UnitOfWork unitOfWork = new UnitOfWork(context)) { try { UsersRepository usersRepositoryUnitOfWork = new UsersRepository(unitOfWork); RolesRepository rolesRepository = new RolesRepository(unitOfWork); if (model.ID > 0) { user = usersRepositoryUnitOfWork.GetAll(filter: u => u.ID == model.ID, includeProperties: "Roles").FirstOrDefault(); user.PersonalNumber = model.PersonalNumber; } else { user = new User(); user.Roles = new List<Role>(); user.PersonalNumber = usersRepositoryUnitOfWork.GetAll().LastOrDefault().PersonalNumber + 1; } user.Password = (model.Password != null) && (model.Password.Trim() != String.Empty) ? model.Password.Trim() : user.Password; user.FirstName = model.FirstName; user.Email = model.Email; user.Address = model.Address; user.LastName = model.LastName; user.Birthday = model.Birthday; user.DateIn = model.DateIn; user.DateOut = model.DateOut != null ? model.DateOut : null; UpdateUserRoles(assignedRoles, user, rolesRepository); usersRepositoryUnitOfWork.Save(user); PopulateAssignedRoles(user, rolesRepository); unitOfWork.Commit(); } catch (Exception ex) { unitOfWork.RollBack(); throw ex; } } return RedirectToAction("Index", "Users"); }
private void GenerateGrid() { HalfOfEachCellArea = 1 * Math.Pow(10, 4); try { var wr1 = GetFileWriterForSelectedPoints(); var wr2 = GetFileWriterForCellMeanVector(); var wr3 = GetFileWriterForNaNPoints(); using (var work = new UnitOfWork()) { GetMeanVectorAndSelectedPoints(wr1, wr2, wr3, work); } wr1.Close(); wr2.Close(); } catch (Exception exception) { throw new Exception("Error while generating grid !"); } Console.ReadLine(); }
public ActionResult EditRole(RolesEditRoleVM model, string[] assignedAuthenticatingActions) { LibraryManagementSystemContext context = new LibraryManagementSystemContext(); Role role = null; if (!this.ModelState.IsValid) { AuthenticatingActionsRepository authenticatingActionsRepository = new AuthenticatingActionsRepository(context); var authenticatingActions = authenticatingActionsRepository.GetAll(); List<AuthenticatingActionsVM> authenticatingActionsViewModel = new List<AuthenticatingActionsVM>(); foreach (var action in authenticatingActions) { authenticatingActionsViewModel.Add(new AuthenticatingActionsVM { ID = action.ID, Name = action.Name, IsAssigned = false }); } ViewBag.AuthenticatingActions = authenticatingActionsViewModel; return View(model); } using (UnitOfWork unitOfWork = new UnitOfWork(context)) { try { var authenticatingActionsRepository = new AuthenticatingActionsRepository(unitOfWork); var rolesRepository = new RolesRepository(unitOfWork); if (model.ID > 0) { role = rolesRepository.GetAll(filter: r => r.ID == model.ID, includeProperties: "AuthenticatingActions").FirstOrDefault(); } else { role = new Role(); role.AuthenticatingActions = new List<AuthenticatingAction>(); } role.Name = model.Name; UpdateAuthenticatingActions(assignedAuthenticatingActions, role, authenticatingActionsRepository); rolesRepository.Save(role); PopulateAssignedAuthenticatingActions(role, authenticatingActionsRepository); unitOfWork.Commit(); } catch (Exception ex) { unitOfWork.RollBack(); throw ex; } } return RedirectToAction("Index", "Roles"); }
public Response<Dimension> GetCurrentDataDimension() { var response = new Response<Dimension>(); try { using (var work = new UnitOfWork()) { var cellRepository = RepositoryContainer.GetRepository<Cell>(work); const string sqlQuery = "select max(X) as MaxX, min(X) as MinX, max(Y) as MaxY, min(Y) as MinY from Cells"; var dimension = cellRepository.ExecuteCommand<Dimension>(sqlQuery); response.Data = dimension.ElementAt(0); return response; } } catch (Exception) { response.Success = false; response.ErrorMessage = "failed"; return response; } }