smark.data是基于C#开发的轻量级数据访问组件。
提供以下功能封:
1)提供了跨数据库类型操作能力
2)基于程线存储的数据访问上下文对象,轻易提供跨方法域的数据事务处理机制。
3)对象查询表达式
4)实体操作基础封装等快速开发功能
开发人员可以在以上基于功能扩展出更灵活高效的数据操作。
组件还提供一个基于VS2008的Generator,可以让开发人员在VS。NET里描述XML的同时同步生成实体操作代码进一步简化操作(开发人员也可以根据自己的需求编写适合自己的Generator)。
发布地址:http://www.codeplex.com/smarkdata

以下是Smark.data的单元测试代码

 

[TestMethod]
        public void Null()
        {
            IList
<Order> items = Order.List(null);
            WriteOrder(items);
        }
        [TestMethod]
        
public void Eq()
        {
            Expression exp 
= Order.employeeID.Eq(4);
            WriteExpression(exp);
            IList
<Order> items = Order.List( exp);
            WriteOrder(items);
        }
        [TestMethod]
        
public void Lt()
        {
            Expression exp 
= Order.employeeID.Lt(3);
            WriteExpression(exp);
            IList
<Order> items = Order.List(exp);
            WriteOrder(items);
        }
        [TestMethod]
        
public void LtEq()
        {
            Expression exp 
= Order.employeeID.LtEq(3);
            WriteExpression(exp);
            IList
<Order> items =Order.List(exp);
            WriteOrder(items);
        }
        [TestMethod]
        
public void Gt()
        {
            Expression exp 
= Order.employeeID.Gt(4);
            WriteExpression(exp);
            IList
<Order> items = Order.List(exp);
            WriteOrder(items);
        }
        [TestMethod]
        
public void GtEq()
        {
            Expression exp 
= Order.employeeID.GtEq(4);
            WriteExpression(exp);
            IList
<Order> items = Order.List(exp);
            WriteOrder(items);
        }
        [TestMethod]
        
public void NotRt()
        {
            Expression exp 
= Order.employeeID.NotEq(4);
            WriteExpression(exp);
            IList
<Order> items = Order.List(exp);
            WriteOrder(items);
        }
        [TestMethod]
        
public void In()
        {
            Expression exp 
= Order.employeeID.In(new int[] {1,3,5 });
            WriteExpression(exp);
            IList
<Order> items = Order.List(exp);
            WriteOrder(items);
        }
        [TestMethod]
        
public void InTable()
        {
            Expression exp 
= Order.employeeID.In(Employee.employeeID, Employee.country.Eq("usa"));
            WriteExpression(exp);
            IList
<Order> items = Order.List(exp);
            WriteOrder(items);
        }
        [TestMethod]
        
public void NotIn()
        {
            Expression exp 
= Order.employeeID.NotIn(new int[] { 135 });
            WriteExpression(exp);
            IList
<Order> items = Order.List(exp);
            WriteOrder(items);
        }
        [TestMethod]
        
public void NoTInTable()
        {
            Expression exp 
= Order.employeeID.NotIn(Employee.employeeID, Employee.country.Eq("usa"));
            WriteExpression(exp);
            IList
<Order> items = Order.List(exp);
            WriteOrder(items);
        }
        [TestMethod]
        
public void Between()
        {
            Expression exp 
= Order.orderDate.Between("1995-1-1""1995-12-31");
            WriteExpression(exp);
            IList
<Order> items = Order.List(exp);
            WriteOrder(items);
        }
        [TestMethod]
        
public void NotBetween()
        {
            Expression exp 
= Order.orderDate.NotBetween("1995-1-1""1995-12-31");
            WriteExpression(exp);
            IList
<Order> items = Order.List(exp);
            WriteOrder(items);
        }
        [TestMethod]
        
public void Insert()
        {
            Employee emp 
= new Employee();
            emp.FirstName 
= "Fan";
            emp.LastName 
= "henry";
            emp.Save();
            TestContext.WriteLine(
"EmployeeID:" + emp.EmployeeID);
        }
        [TestMethod]
        
public void Update()
        {
            Employee emp 
= Employee.Load(3);
            emp.Country 
= "china";
            emp.Save();
            emp 
= Employee.Load(3);
            TestContext.WriteLine(emp.Country);
        }
        [TestMethod]
        
public void Count()
        {

            Expression exp
=null;
            TestContext.WriteLine(
"Orders:" + Order.Count(exp));
            exp 
= Order.orderDate.Between("1995-1-1""1995-1-31");
            WriteExpression(exp);
            TestContext.WriteLine(
"Orders:" + Order.Count(exp));
        }
        [TestMethod]
        
public void Sum()
        {
            Expression exp 
= Detail.orderID.In(Order.orderID, Order.orderDate.Between("1995-1-1""1995-1-31"));
            WriteExpression(exp);
            TestContext.WriteLine(
                
"Order ExtendedPrice:" + Detail.SUM(string.Format("{0}*{1}",Detail.unitPrice,Detail.quantity), exp));
            WriteSplit();
            exp 
= Detail.orderID.In(Order.orderID, Order.employeeID.In(Employee.employeeID, Employee.country.Eq("usa")));
            WriteExpression(exp);
            TestContext.WriteLine(
                
"Order ExtendedPrice:" + Detail.SUM(string.Format("{0}*{1}", Detail.unitPrice, Detail.quantity), exp));
            exp 
= Detail.orderID.In(Order.orderID, Order.employeeID.Eq(5));
            WriteExpression(exp);
            TestContext.WriteLine(
                
"Order ExtendedPrice:" + Detail.SUM(string.Format("{0}*{1}", Detail.unitPrice, Detail.quantity), exp));
        }
        [TestMethod]
        
public void Max()
        {
            Expression exp 
= Detail.orderID.In(Order.orderID, Order.orderDate.Between("1995-1-1""1995-1-31"));
            WriteExpression(exp);
            TestContext.WriteLine(
                
"Order ExtendedPrice:" + Detail.MAX(string.Format("{0}*{1}", Detail.unitPrice, Detail.quantity), exp));
            WriteSplit();
            exp 
= Detail.orderID.In(Order.orderID, Order.employeeID.In(Employee.employeeID, Employee.country.Eq("usa")));
            WriteExpression(exp);
            TestContext.WriteLine(
                
"Order ExtendedPrice:" + Detail.MAX(string.Format("{0}*{1}", Detail.unitPrice, Detail.quantity), exp));
            exp 
= Detail.orderID.In(Order.orderID, Order.employeeID.Eq(5));
            WriteExpression(exp);
            TestContext.WriteLine(
                
"Order ExtendedPrice:" + Detail.MAX(string.Format("{0}*{1}", Detail.unitPrice, Detail.quantity), exp));
        }
        [TestMethod]
        
public void Min()
        {
            Expression exp 
= Detail.orderID.In(Order.orderID, Order.orderDate.Between("1995-1-1""1995-1-31"));
            WriteExpression(exp);
            TestContext.WriteLine(
                
"Order ExtendedPrice:" + Detail.MIN(string.Format("{0}*{1}", Detail.unitPrice, Detail.quantity), exp));
            WriteSplit();
            exp 
= Detail.orderID.In(Order.orderID, Order.employeeID.In(Employee.employeeID, Employee.country.Eq("usa")));
            WriteExpression(exp);
            TestContext.WriteLine(
                
"Order ExtendedPrice:" + Detail.MIN(string.Format("{0}*{1}", Detail.unitPrice, Detail.quantity), exp));
            exp 
= Detail.orderID.In(Order.orderID, Order.employeeID.Eq(5));
            WriteExpression(exp);
            TestContext.WriteLine(
                
"Order ExtendedPrice:" + Detail.MIN(string.Format("{0}*{1}", Detail.unitPrice, Detail.quantity), exp));
        }
        [TestMethod]
        
public void Avg()
        {
            Expression exp 
= Detail.orderID.In(Order.orderID, Order.orderDate.Between("1995-1-1""1995-1-31"));
            WriteExpression(exp);
            TestContext.WriteLine(
                
"Order ExtendedPrice:" + Detail.AVG(string.Format("{0}*{1}", Detail.unitPrice, Detail.quantity), exp));
            WriteSplit();
            exp 
= Detail.orderID.In(Order.orderID, Order.employeeID.In(Employee.employeeID, Employee.country.Eq("usa")));
            WriteExpression(exp);
            TestContext.WriteLine(
                
"Order ExtendedPrice:" + Detail.AVG(string.Format("{0}*{1}", Detail.unitPrice, Detail.quantity), exp));
            exp 
= Detail.orderID.In(Order.orderID, Order.employeeID.Eq(5));
            WriteExpression(exp);
            TestContext.WriteLine(
                
"Order ExtendedPrice:" + Detail.AVG(string.Format("{0}*{1}", Detail.unitPrice, Detail.quantity), exp));
        }
        [TestMethod]
        
public void op_Equality()
        {
            Expression exp 
= Order.employeeID == new int[] {1,3,5 };
            IList
<Order> items = Order.List(exp);
            WriteExpression(exp);
            WriteOrder(items);
            WriteSplit();
            exp 
= Order.employeeID == 3;
            items 
= Order.List(exp);
            WriteExpression(exp);
            WriteOrder(items);
            WriteSplit();
            exp 
= Order.employeeID == null;
            items 
= Order.List(exp);
            WriteExpression(exp);
            WriteOrder(items);


        }
        [TestMethod]
        
public void op_Inequality()
        {
            Expression exp 
= Order.employeeID != new int[] { 135 };
            IList
<Order> items = Order.List(exp);
            WriteExpression(exp);
            WriteOrder(items);
            WriteSplit();
            exp 
= Order.employeeID != 3;
            items 
= Order.List(exp);
            WriteExpression(exp);
            WriteOrder(items);
            WriteSplit();
            exp 
= Order.employeeID != null;
            items 
= Order.List(exp);
            WriteExpression(exp);
            WriteOrder(items);
        }
        [TestMethod]
        
public void op_GreaterThan()
        {
            Expression exp 
= Order.orderDate > "1996-1-1";
            IList
<Order> items = Order.List(exp);
            WriteExpression(exp);
            WriteOrder(items);
        }
        [TestMethod]
        
public void op_LessThan()
        {
            Expression exp 
= Order.orderDate < "1996-1-1";
            IList
<Order> items = Order.List(exp);
            WriteExpression(exp);
            WriteOrder(items);
        }
        [TestMethod]
        
public void op_GreaterThanOrEqual()
        {
            Expression exp 
= Order.orderDate >= "1996-1-1";
            IList
<Order> items = Order.List(exp);
            WriteExpression(exp);
            WriteOrder(items);
        }
        [TestMethod]
        
public void op_LessThanOrEqual()
        {
            Expression exp 
= Order.orderDate <= "1996-1-1";
            IList
<Order> items = Order.List(exp);
            WriteExpression(exp);
            WriteOrder(items);
        }
        [TestMethod]
        
public void Or()
        {
            Expression exp 
= Order.employeeID == 3 | Order.orderDate == "1996-1-1";
            IList
<Order> items = Order.List(exp);
            WriteExpression(exp);
            WriteOrder(items);
        }
        [TestMethod]
        
public void And()
        {
            Expression exp 
= Order.employeeID == 3 & Order.orderDate == "1996-1-1";
            IList
<Order> items = Order.List(exp);
            WriteExpression(exp);
            WriteOrder(items);
        }
        [TestMethod]
        
public void CountByEmployee()
        {
            Expression exp 
= Order.employeeID.At("o").In(Employee.employeeID, Employee.country == "usa");
            IList
<OrdersByEmployee> items = OrdersByEmployee.List(exp);
            WriteExpression(exp);
            TestContext.WriteLine(
"Count\tEmployeeName");
            
foreach (OrdersByEmployee item in items)
            {
                TestContext.WriteLine(
"{0}\t{1}", item.Orders, item.EmployeeName);
            }

            exp 
= Order.orderDate.Between("1995-5-1""1995-6-1");
            items 
= OrdersByEmployee.List(exp);
            WriteExpression(exp);
            TestContext.WriteLine(
"Count\tEmployeeName");
            
foreach (OrdersByEmployee item in items)
            {
                TestContext.WriteLine(
"{0}\t{1}", item.Orders, item.EmployeeName);
            }
        }
        [TestMethod]
        
public void CountByCustomer()
        {
            Expression exp 
= Order.customerID.At("o"== new string[] { "ANATR""ANTON""BLONP" };
            IList
<OrderByCustomer> items = OrderByCustomer.List(exp);
            WriteExpression(exp);
            TestContext.WriteLine(
"Count\tCompanyName");
            
foreach (OrderByCustomer item in items)
            {
                TestContext.WriteLine(
"{0}\t{1}", item.Orders, item.CompanyName);
            }

            exp 
= Order.orderDate.Between("1995-5-1""1995-6-1");
            items 
= OrderByCustomer.List(exp);
            WriteExpression(exp);
            TestContext.WriteLine(
"Count\tCompanyName");
            
foreach (OrderByCustomer item in items)
            {
                TestContext.WriteLine(
"{0}\t{1}", item.Orders, item.CompanyName);
            }
        }

相关文章: