public async Task GivenRepository_WhenFullCRUDFlowIsRun_ThenSucceed() { Execute(Structure.UsersCreateSql); var repository = new DapperRepository <User>(DbConnection, new NulloAggregateEventsPublisher()); var user = new User { Id = 0, UserName = "******", FullName = null }; //1. Add var result = await repository.Add(user); result.Should().NotBe(null); result.Id.Should().BeGreaterThan(0); result.UserName.Should().Be("*****@*****.**"); result.FullName.Should().BeNull(); //2. GetById var recordFromDb = await repository.FindById(user.Id); recordFromDb.Should().BeEquivalentTo(result); //3. Update var userToUpdate = new User { Id = user.Id, UserName = "******", FullName = "Tom Smith" }; result = await repository.Update(userToUpdate); result.Should().NotBe(null); result.Id.Should().Be(user.Id); result.UserName.Should().Be("*****@*****.**"); result.FullName.Should().Be("Tom Smith"); recordFromDb = await repository.FindById(userToUpdate.Id); recordFromDb.Should().BeEquivalentTo(result); //4. Remove result = await repository.Delete(userToUpdate); result.Should().NotBe(null); result.Id.Should().Be(user.Id); result.UserName.Should().Be("*****@*****.**"); result.FullName.Should().Be("Tom Smith"); recordFromDb = await repository.FindById(userToUpdate.Id); recordFromDb.Should().Be(null); //5. Add Range var results = (await repository.AddAll(
static void Main(string[] args) { var db = new DapperRepository <User>(); string sql = "insert into user(UserID,UserGuid,GroupID,UserName,Password,DateOfBirth,Phone,Email,Address) values(@UserID,@UserGuid,@GroupID,@UserName,@Password,@DateOfBirth,@Phone,@Email,@Address)"; var u = new User() { UserID = "0001", UserGuid = Guid.NewGuid().ToString(), UserName = "******", Password = "******", DateOfBirth = new DateTime(1991, 11, 25), Phone = "12345678912", Email = "*****@*****.**", Address = "Xxxxxx" }; db.Add(sql, u, false); Console.WriteLine(JsonConvert.SerializeObject(u)); Console.WriteLine("Hello World!"); Console.ReadLine(); }
public virtual int Add(T item) { try { var mapper = new MapperConfiguration(cfg => cfg.CreateMap <T, K>()).CreateMapper(); var itemToAdd = mapper.Map <T, K>(item); int addResult = _curDapperRep.Add(itemToAdd); return(addResult); } catch (Exception ex) { Console.WriteLine(ex.Message); } return(0); }
public void GivenRepository_WhenFullCRUDFlowIsRun_ThenSucceed() { Execute(Structure.UsersCreateSql); var repository = new DapperRepository <User>(DbConnection); var user = new User { Id = 0, UserName = "******", FullName = null }; //1. Add var result = repository.Add(user); result.Should().NotBe(null); result.Id.Should().BeGreaterThan(0); result.UserName.Should().Be("*****@*****.**"); result.FullName.Should().BeNull(); //2. GetById var recordFromDb = repository.FindById(user.Id); recordFromDb.Should().BeEquivalentTo(result); //3. Update var userToUpdate = new User { Id = user.Id, UserName = "******", FullName = "Tom Smith" }; result = repository.Update(userToUpdate); result.Should().NotBe(null); result.Id.Should().Be(user.Id); result.UserName.Should().Be("*****@*****.**"); result.FullName.Should().Be("Tom Smith"); recordFromDb = repository.FindById(userToUpdate.Id); recordFromDb.Should().BeEquivalentTo(result); //4. Remove result = repository.Delete(userToUpdate); result.Should().NotBe(null); result.Id.Should().Be(user.Id); result.UserName.Should().Be("*****@*****.**"); result.FullName.Should().Be("Tom Smith"); recordFromDb = repository.FindById(userToUpdate.Id); recordFromDb.Should().Be(null); //5. Add Range var results = repository.AddAll( new User { UserName = "******" }, new User { UserName = "******" }, new User { UserName = "******" } )?.ToList(); results.Should().NotBeNull(); results.Should().HaveCount(3); results[0].Id.Should().BeGreaterThan(0); results[0].UserName.Should().Be("*****@*****.**"); results[0].FullName.Should().BeNull(); results[1].Id.Should().BeGreaterThan(0); results[1].UserName.Should().Be("*****@*****.**"); results[1].FullName.Should().BeNull(); results[2].Id.Should().BeGreaterThan(0); results[2].UserName.Should().Be("*****@*****.**"); results[2].FullName.Should().BeNull(); //6. Query var queryResults = repository.Query().ToList(); queryResults.Should().Contain(x => results.Select(u => u.Id).Contains(x.Id)); queryResults = repository.Query().ToList().Where(x => x.UserName == results[1].UserName).ToList(); queryResults.Should().HaveCountGreaterOrEqualTo(1); queryResults.First(x => x.Id == results[1].Id).Should().BeEquivalentTo(results[1]); }