The previous 3 articles in this series showed you how easy
it is to use the Entity Framework 4 to create a database, add, update, delete,
and select records from tables, and also how to execute stored procedures. The
actual amount of coding required to create a web page that can manage data in a
table is quite small. However, the examples didn't really show you any type of
pattern. They simply showed you the syntax for making the web page work. I'm
sure by now you've read an article or a book about separating your logic into
the user interface, the business logic layer, and the data access layer and are
scratching your head thinking that the Entity Framework kind of encapsulates
the business layer and data access layer and all you need is a GridView control
on your form and you're in business. For some applications this can and will
work fine. Every pattern isn't for every application or every developer.
However, just from experience I have always found it better to at least
separate out the three layers logically.
This article will create the same web page that was created
in the second and third articles except it will introduce a pattern for developing
the application as a three layered application. Notice that I didn't say three
tiered application. A tier really means a logical and physical separation of
the layers. A layer implies just a logical separation. For many applications
that's all you need. I'm not one to advocate over-engineering a system. I
always follow the simpler, better approach as long as the developers working
with you understand the pattern.
This article will create a web application as the user
interface layer and two class libraries for the business logic layer (BLL) and
the data access layer (DAL). The code to communicate with the database will be
encapsulated in the DAL project. The entities in the Data Context will act as
the data transfer objects (DTOs) that pass data between the DAL and BLL. The
concept of a DTO is not new. Previously you would need to write your own custom
classes but the entity framework can save you all that custom coding.
The goal of this article is to create a web page that allows
a user to maintain the records in a table called UserAccounts. The final web
page looks like the following image.
The drop down list at the top of the page allows you to
navigate from user to user and displays the properties on the page. The user
can simply click the Save button to add or update records or click the Delete
button to remove a record.