コード例 #1
0
        public void DapperDbMultiInstanceTest()
        {
            SqlCeConnectionStringBuilder builder = new SqlCeConnectionStringBuilder()
            {
                DataSource = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "DapperTests.sdf")
            };

            for (int i = 0; i < 100; i++)
            {
                DapperAccessLayer.Create <IDapperTestsDb>(() => new SqlCeConnection(builder.ToString()));
            }
        }
コード例 #2
0
        public void SimpleDapperLayerTest()
        {
            DbConnectionMock con = new DbConnectionMock();

            IDAL dal = DapperAccessLayer.Create <IDAL>(con);

            Assert.IsNotNull(dal);
            Assert.AreSame(dal.GetType(), DapperAccessLayer.Implement(typeof(IDAL)));
            DapperAccessLayer.Create <IDAL>(con);

            con.Table = new DataTable();
            con.Table.Columns.Add("A");
            con.Table.LoadDataRow(new[] { "a1" }, true);

            DTO[] rows = dal.GetDTOs(new DTO()
            {
                A = "a", B = "b"
            }).ToArray();
            Assert.AreEqual(1, rows.Length);
            Assert.AreEqual("a1", rows[0].A);
            Assert.AreEqual(2, con.LastCommand.Parameters.Count);
            Assert.AreEqual("a", ((IDbDataParameter)con.LastCommand.Parameters[0]).Value);
            Assert.AreEqual("b", ((IDbDataParameter)con.LastCommand.Parameters[1]).Value);

            rows = dal.GetDTOs("42", 42).ToArray();
            Assert.AreEqual(1, rows.Length);
            Assert.AreEqual("a1", rows[0].A);
            Assert.AreEqual(2, con.LastCommand.Parameters.Count);
            Assert.AreEqual("42", ((IDbDataParameter)con.LastCommand.Parameters[0]).Value);
            Assert.AreEqual(42, ((IDbDataParameter)con.LastCommand.Parameters[1]).Value);

            rows = dal.GetDTOsDynamic(new { A = "a", B = "b" }).ToArray();
            Assert.AreEqual(1, rows.Length);
            Assert.AreEqual("a1", rows[0].A);
            Assert.AreEqual(2, con.LastCommand.Parameters.Count);
            Assert.AreEqual("a", ((IDbDataParameter)con.LastCommand.Parameters[0]).Value);
            Assert.AreEqual("b", ((IDbDataParameter)con.LastCommand.Parameters[1]).Value);
        }
コード例 #3
0
        public void DapperDbConnectionTest()
        {
            SqlCeConnectionStringBuilder builder = new SqlCeConnectionStringBuilder()
            {
                DataSource = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "DapperTests.sdf")
            };

            using (var con = new SqlCeConnection(builder.ToString()))
            {
                IDapperTestsDb dal = DapperAccessLayer.Create <IDapperTestsDb>(con);

                dal.ClearAccounts();

                // No rows
                Account[] accounts = dal.GetAllAccounts().ToArray();
                Assert.AreEqual(0, accounts.Length);

                // No single row
                Account account = dal.GetAccount(-1).SingleOrDefault();
                Assert.IsNull(account);

                // Insert row and select it back
                int affectedRows = dal.InsertAccount("DKK", "My DKK account");
                Assert.AreEqual(1, affectedRows);
                Assert.AreNotEqual(0, dal.GetAccountsByCurrency("DKK").Count());
                Assert.AreEqual(0, dal.GetAccountsByCurrency("XXX").Count());
                Assert.AreEqual("My DKK account", dal.GetAccountsByCurrency("DKK").Single().Description);

                // Insert one more row
                dal.InsertAccount(new Account()
                {
                    AccountCurrency = "USD", Description = "USD"
                });
                Assert.AreEqual("USD", dal.GetAccountsByCurrency("USD").Single().Description);

                // Insert many accounts
                dal.ClearAccounts();
                accounts = Enumerable.Range(0, 100).Select(i => new Account()
                {
                    Description = i.ToString(), AccountCurrency = "GBP"
                }).ToArray();
                Assert.AreEqual(accounts.Length, dal.InsertAccounts(accounts));
                Assert.AreEqual(accounts.Length, dal.CountAccounts().Single());

                // Clean up
                Assert.AreNotEqual(0, dal.ClearAccounts());
            }

            // Test transaction
            using (var con = new SqlCeConnection(builder.ToString()))
            {
                IDapperTestsDb dal = DapperAccessLayer.Create <IDapperTestsDb>(con);
                using (var tran = new TransactionScope())
                {
                    con.Open();

                    // Insert accounts
                    dal.InsertAccount(new Account()
                    {
                        AccountCurrency = "USD", Description = "USD"
                    });
                    Assert.AreEqual(1, dal.CountAccounts().Single());

                    // Don't commit
                }
                Assert.AreEqual(0, dal.CountAccounts().Single());
            }
        }