当前位置:Gxlcms > 数据库问题 > LINQ to SQL活学活用(2):躲起来别让我看见

LINQ to SQL活学活用(2):躲起来别让我看见

时间:2021-07-01 10:21:17 帮助过:3人阅读

Customer CreateCustomer() { return new Customer(); }

2.按CustomerId获取Customer对象

public Customer GetCustomerById(int customerId)
{
    return (from p in DataContext.Customer
            where p.CustomerId == customerId
            select p).FirstOrDefault();
}

3.获取Customer对象列表

public IList<Customer> GetCustomerList()
{
    return (from p in DataContext.Customer
        select p).ToList<Customer>();
}

4.更新保存Customer对象

public void UpdateCustomer(Customer customer)
{
    if (customer == null)
    {
        throw new ArgumentNullException("Customer", "对象为空");
    }
    else
    {
        if (customer.CustomerId == 0)
        {
            DataContext.Customer.InsertOnSubmit(customer);
        }
        DataContext.SubmitChanges();
    }
}

这里为了演示,仅仅写出了4个方法,大家可以按照自己的需要添加一些操作。

单元测试层

可以测试上面我们修改的结果了,在单元测试层新建一CustomerFacadeFixture.cs类依然继承测试基类UnitTestBase。

Step1:实例化CustomerFacade

在这个测试类中,首先实例化CustomerFacade。

private CustomerFacade m_facade;
public CustomerFacade Facade
{
    get
    {
        if (m_facade == null)
        {
            m_facade = new CustomerFacade();
        }
        return m_facade;
    }
}

Step2:编写保存更新方法

其次,编写一个创建并保存Customer的方法,因为我们每次测试前,数据库为空的,在测试前,我们需要输入一些原始数据。

private Customer CreateAndSaveNewCustomer(string firstName, string lastName)
{
    Customer newCustomer = Facade.CreateCustomer();
    newCustomer.FirstName = firstName;
    newCustomer.LastName = lastName;
    Facade.UpdateCustomer(newCustomer);
    return newCustomer;
}

从这个方法,我们就直接使用Facade提供给客户端的Create()方法和Update()方法,我们完全不知道具体的实现细节。

Step3:测试UpdateCustomer()方法

调用上面的方法,向创建保存为YJingLee的Customer为测试初始数据,正好也是测试了保存数据方法。

[Test]
public void UpdateCustomerTest()
{
    Customer newCustomer = CreateAndSaveNewCustomer("YJing", "Lee");
    Assert.AreNotEqual(0, newCustomer.CustomerId);
    Assert.AreEqual("YJing", newCustomer.FirstName);
}

看看结果吧:

技术分享

分析一下:首先验证数据库是否存在,这里存在,删除原有的数据库重新创建一个新的数据库架构,向数据库中插入一条YJingLee的数据并查询这条数据。

Step4:测试GetCustomerById()方法

再来测试GetCustomerById()方法,首先在数据库中插入一条YJingLee的数据,看看这句reloaded = Facade.GetCustomerById(tempCustomer.CustomerId)调用外观Facade中的GetCustomerById()方法按CustomerId获取Customer对象,体现了对外隐藏具体的实现细节,最后断言数据是否符合预料的结果。

[Test]
public void GetCustomerByIdTest()
{
    Customer tempCustomer = CreateAndSaveNewCustomer("YJing", "Lee");
    Assert.AreNotEqual(0, tempCustomer.CustomerId);

    Customer reloaded = Facade.GetCustomerById(tempCustomer.CustomerId);
    Assert.IsNotNull(reloaded);
    Assert.AreEqual(tempCustomer.CustomerId, reloaded.CustomerId);
    Assert.AreSame(tempCustomer, reloaded);
}

这个测试就留给大家测试了!测试好了把结果告诉我哦。 

Step5:测试GetCustomerList()方法

首先初始化三条数据,然后调用外观Facade中的GetCustomerList()方法获取Customer列表,测试是否一致

[Test]
public void GetListTest()
{
    List<Customer> tempCustomers = new List<Customer>();

    tempCustomers.Add(CreateAndSaveNewCustomer("YJing", "Lee"));
    tempCustomers.Add(CreateAndSaveNewCustomer("li", "yongjing"));
    tempCustomers.Add(CreateAndSaveNewCustomer("cnblogs", "com"));

    var reloaded = Facade.GetCustomerList();
    Assert.IsNotNull(reloaded);
    Assert.AreEqual(tempCustomers.Count, reloaded.Count);
}

结语

这篇文章我们通过修改第一篇完全裸露的代码,运用一个外观Facade类对外提供较清晰的接口来隐藏具体的实现细节,客户使用只需和Facade对象接口交互,从这篇的改进也完美地体现了依赖倒转原则和迪米特法则的思想。

版权声明:本文为博主http://www.zuiniusn.com原创文章,未经博主允许不得转载。

LINQ to SQL活学活用(2):躲起来别让我看见

标签:

人气教程排行