Microsoft 9GD00001 Computer Accessories User Manual


 
268 Microsoft Visual Studio 2010: A Beginner’s Guide
(From cust In m_ctx.Customers
Select cust).ToList()
Return customers
End Function
Public Sub DeleteCustomer(ByVal custID As Integer)
Dim customer =
(From cust In m_ctx.Customers
Where cust.CustomerID = custID
Select cust).SingleOrDefault()
m_ctx.Customers.DeleteOnSubmit(customer)
m_ctx.SubmitChanges()
End Sub
End Class
You can have more methods in a repository for doing whatever is required with data
for the application, but the items in Listing 9-6 are typical. The LINQ to SQL operations
are consistent with the material covered in Chapter 7, so there’s no need to repeat the same
material here. The purpose of the repository is to give the Controller an object to work
with for getting data without filling up Controller methods with data access logic. Let’s
see how the Controller works with this repository next.
Creating a Customer Controller
Right-click the Controllers folder, select Add | Controller, or press CTRL-M, press CTRL-
C, and name the file CustomerController. Check the box for “Add action methods for
Create, Update, and Details scenarios” as shown in Figure 9-4.
Figure 9-4 Creating a new Controller