Example #1
0
        private void SetUpDbSettings()
        {
            if (!Program.Container.Kernel.HasComponent(typeof(DatabaseSettings)))
            {
                var dbSettings = new DatabaseSettings
                {
                    Url      = tbDbUrl.Text,
                    UserName = tbDdUserName.Text,
                    Password = tbDbPassword.Text
                };
                Program.Container.Register(Component.For <DatabaseSettings>().Instance(dbSettings));
            }
            else
            {
                var currentXmlApiSettings = Program.Container.Resolve <DatabaseSettings>();
                currentXmlApiSettings.Url      = tbDbUrl.Text;
                currentXmlApiSettings.UserName = tbDdUserName.Text;
                currentXmlApiSettings.Password = tbDbPassword.Text;
            }

            IDbProvider dbProvider = rbSqlProvider.Checked ? (IDbProvider) new SqlProvider() : new OracleProvider();

            if (!Program.Container.Kernel.HasComponent(typeof(IDataContext)))
            {
                var dataContext = new DapperDataContext(Program.Container.Resolve <DatabaseSettings>(), dbProvider);
                Program.Container.Register(Component.For <IDataContext>().Instance(dataContext));
            }
            else
            {
                var currentXmlApiSettings = Program.Container.Resolve <IDataContext>();
                currentXmlApiSettings.DbProvider = dbProvider;
            }
        }
        public void GivenDataContextWithouts_WhenFullCRUDFlowIsRun_ThenSucceed()
        {
            Execute(Structure.UsersCreateSql);

            var dataContext = new DapperDataContext(DbConnection);

            var user = new User
            {
                Id       = 0,
                UserName = "******",
                FullName = null
            };

            //1. Add
            var result = dataContext.Add(user);

            result.Should().NotBe(null);
            result.Id.Should().BeGreaterThan(0);
            result.UserName.Should().Be("*****@*****.**");
            result.FullName.Should().BeNull();

            //2. GetById

            var recordFromDb = dataContext.GetById <User>(user.Id);

            recordFromDb.Should().BeEquivalentTo(result);

            //3. Update
            var userToUpdate = new User
            {
                Id       = user.Id,
                UserName = "******",
                FullName = "Tom Smith"
            };

            result = dataContext.Update(userToUpdate);

            result.Should().NotBe(null);
            result.Id.Should().Be(user.Id);
            result.UserName.Should().Be("*****@*****.**");
            result.FullName.Should().Be("Tom Smith");

            recordFromDb = dataContext.GetById <User>(userToUpdate.Id);

            recordFromDb.Should().BeEquivalentTo(result);

            //4. Remove
            result = dataContext.Remove(userToUpdate);

            result.Should().NotBe(null);
            result.Id.Should().Be(user.Id);
            result.UserName.Should().Be("*****@*****.**");
            result.FullName.Should().Be("Tom Smith");

            recordFromDb = dataContext.GetById <User>(userToUpdate.Id);

            recordFromDb.Should().Be(null);

            //5. Add Range

            var results = dataContext.AddRange(
                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 = dataContext.GetQueryable <User>().ToList();

            queryResults.Should().Contain(x => results.Select(u => u.Id).Contains(x.Id));

            queryResults = dataContext.GetQueryable <User>().Where(x => x.UserName == results[1].UserName).ToList();

            queryResults.Should().HaveCountGreaterOrEqualTo(1);
            queryResults.First(x => x.Id == results[1].Id).Should().BeEquivalentTo(results[1]);
        }