Step 12 : Enhancing and Testing the Customer Object

In this step you will enhance the Customer Controller object so it can retrieve Customer entities, and create a unit test for the class.

  1. In the Solution Explorer under the Customer node, click the Customer.Partial.vb class to open it for code editing.

  2. Add the following method below the HookConstructor() method:

    ''' <summary>
    ''' Gets customers that match the partial customerID string
    ''' </summary>
    ''' <param name="customerID">Partial or full customerID string</param>
    ''' <returns>List of Customer entities that match the search criteria</returns>
    Public Function GetCustomersLikeCustomerID(ByVal customerID As StringAs mmBindingList(Of CustomerEntity)
    	Dim query As IQueryable(Of CustomerEntity) =
    From customer In Me.ObjectContext.CustomerEntities
    Where (customer.CustomerID.StartsWith(customerID))
    Select customer
     
    	Return Me.GetEntityList(query)
    End Function

    The LINQ statement in this method uses the String.StartsWith() method to find all customers whose CustomerID starts with specified characters. Behind the scenes, this LINQ query results in the following SQL statement that uses the LIKE operator (in this example, the letter "Q" is specified as the customerID parameter):

    SELECT *
    FROM dbo.Customers
    WHERE CustomerID LIKE [Q%]

  3. To test this method, add the following method to the OrderSystemTests class in the unit test project:

    <TestMethod()>
    Public Sub TestGetCustomersLikeCustomerID()
    	Dim c As Customer = New Customer()
    	Dim CustomerList As mmBindingList(Of CustomerEntity) = c.GetCustomersLikeCustomerID("Q")
    	Assert.IsTrue(CustomerList.Count > 0)
    End Sub

  4. Set a breakpoint in the first line of code in this method, then press F5 to rebuild your business object project. In the Test Explorer, right-click the TestGetCustomersLikeCustomerID method and select Debug Selected Tests from the shortcut menu. This should cause you to hit the breakpoint at the top of the method:

  5. Press F10 to instantiate the Customer business controller object, then press F10 again to return a strongly typed list of CustomerEntity objects. If you hover your mouse pointer over the CustomerList property then click the + sign on the popup you should see a list of entity objects that were returned:

  6. Press F5 to finish executing the method. This should add a green check mark next to the TestGetCustomersLikeCustomerID method in the Test Explorer.

See Also:
Conclusion : EF Code First Business Objects - VB


© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 02/11/26
Comment or report problem with topic