Within the principle of separation of concerns, there are 3 important layers that's needed to be followed while programming. They are:
- Domain layer
- Presentation layer
- Persistence layer
Domain layer could basically refer to as the layer that manages the basic logic of the application, while Presentation layer is the one that presents the entire function of an application to the end user, e.g the use of the server control. Obviously, you have seen these two types of layers during our previous lessons, while I haven't covered in details the persistence layer aspect of this SOC principle. Here you would see how persistence layer could be used in other to achieve your goals. A good example of how to use this layer is using under the creation of a database. Therefore a persistence layer could be used to hold long-lasting information that we could at any time later return to and access those values stored.
Step 1: Create a new project
Create a new project and name it LocalDBSample; then right-click on this project within the solution explorer and Add a new Item:
Add > new Item
After these steps go to VisualC# and select Data while among the visible templates select SQL Server Database.
VisualC# > Data > SQL Server Database.
Name the .mdf , as skylet.mdf or whatever suits your database and then push the Add button; whereas the visual studio would need your permission to add App_Data folder within your project, affirm that you really want this folder to be added to your project.
Step2: Right click on the "skylet.mdf" to manage the database through the sever explorer window
Now you need to right click on the named file and select "Open" this would eventually open up a server Explorer window under which you would see that a data connection has been established.
Learn also: Understanding entity model and Edmx file in C#
Note if the Server Explore window is not visible, you can get access to it through (Ctrl+Alt+S) or from the Vs menu View > Server Explorer.
|Step2: Server Explorer Diagram|
Step3: Create a database table
A Table in Database could be seen as the rough equivalent of an Excel spreadsheet that has columns and rows of information with headers which connote the information stored. Right there at the Server Explorer Window select "Tables" right click on the "Table" and select "Add New Table" this would load up our database Table Design View. Now we have access to change the table name, Table Id and also the Data Type; in this case I would make use of "Customer" as the Table name , "CustomerId" for the Name and "uniqueidentifier" as the Table Data Type.
Note that using "uniqueidentifier" as a data type will allow the use of GUID (global unique identifier) for each customer within the database.
Step 4: Knowing the right data types to use while populating our Customer's details
Obviously, when we are accepting information from customers it would be innocuous to our application to be able to accept both the English alphabet and non-English alphabet, an instance of a customer using this application from abroad. Therefore using a "varchar" as the Data Type to store information within our database table's would allow this accessibility because it stores Unicode characters. The value within the "varchar" parentheses means the maximum allowed characters; So specify as much as possible characters you want to accept for each row.
Step 5: "newId( )" Is a buit in method that populates table entries
There is a Default space entry for the CustomerId. Right under this space we could run a built in method to SQL server called newId(), this would ensure the supply of a GUID whenever a new customer is created and we do not supply a GUID for it manualy.
Step 6: Use update button to update the database
The next thing now is to click on the "Update" button to update the created Table and next, Click on the "Update Database" whereby you should have this right on your screen:
To see the changes in your database you may have to right-click on "Data Connections" and then "Refresh".
Step 7 : Populate table data values manually
In the Server Explorer window, right-click on the "Customers" and select "Show Table Data": After which we would need to supply values for each table element, manually. To commit the supplied values to the table then we need to press Enter. Obviously, our CustomerId column has no value as we could see but the point is that our data hasn't actually been populated until the "refresh" button is hit, which would make the default newid() built in method to return a valid GUID to the CustomerId.
|Table displaying data from the database|