This article is meant to provide examples of using the NLayers project. You can download the latest NLayers from:
http://nlayers.codeplex.com/
Before proceeding with this article I would recommend reading the previous 2 articles on Installation and Architecture of NLayers.
Example Application
You can download the example application by using the following URL:
http://nlayers.codeplex.com/releases/82197/download/342246
Important Classes and Properties from the Web Context
You can see that the Web application project is referring to the Instance Manager. An Instance Manager project contains the static class named WebApp which can be used to access the Business Logic and Data Access properties.
Examples:
WebApp.BusinessLogic.SampleBL.GetCustomerCount();
WebApp.DataAccess.SampleCustomerDA.GetAll();
Important Classes and Properties from the Business Logic Context
The Business Logic classes are deriving from BaseBL. The BaseBL provides the following properties.
public class BaseBL
{
public IDataAccessAccessPoint DataAccess { get; set; }
public IBusinessLogicAccessPoint BusinessLogic { get; set; }
}
Important Classes and Properties from the Data Access Context
The Data Access classes are derived from BaseDA. The BaseDA provides the Entity Framework CRUD wrapper methods like:
- Insert
- Update
- Delete
- GetAll
- Where()
Running the Example
Use the following steps to execute the Example Application.
- Download the solution from here.
- Extract the zip file and open the solution.
- Make the NLayersExample.Web project as the startup project.
- Locate the Database.sql file inside the web project and execute it against your SQL Server.
- Modify the web.config with the SQL Server name.
- Execute (F5) the example.
On successful execution you will see the following web page:
The Solution Explorer looks like below:
Example 1: Get All SampleCustomer entities
var list = WebApp.DataAccess.SampleCustomerDA.GetAll();
Example 2: Insert a new SampleCustomer entity
SampleCustomer customer = new SampleCustomer();
customer.Name = "New Customer";
customer.Address = "New Address";
WebApp.DataAccess.SampleCustomerDA.Insert(customer);
Example 3: Update previous SampleCustomer entity
SampleCustomer customer = WebApp.DataAccess.SampleCustomerDA.Where(c => c.Name ==
"New Customer").FirstOrDefault();
customer.Address += "Updated";
if (customer != null)
WebApp.DataAccess.SampleCustomerDA.Update(customer);
Example 4: Delete the previous SampleCustomer entity
SampleCustomer customer = WebApp.DataAccess.SampleCustomerDA.Where(c => c.Name ==
"New Customer").FirstOrDefault();
if (customer != null)
WebApp.DataAccess.SampleCustomerDA.Delete(customer);
Example 5: Access a Business Logic class
WebApp.BusinessLogic.SampleBL
Scenarios
The following are the most common scenarios needed to be aware of while working with NLayers.
Scenario 1: Adding a new Entity and associated Data Access classes and Properties
Please use the following steps to do that:
- Inside the Entity project, open the EDMX file
- Update the Model from database and add the new Entity (Make sure the you uncheck the Pluralize option, otherwise the Data Access wrapper methods won't work as intended)
- Create a new interface named INewEntityDA inside the Data Access Interface project.
Use the following template as shown below:
The interface provides a decoupling between the Data Access Implementation and Web application projects. The Instance Manager project will be handling the instance creation.
After adding the interface file, try building the project alone.
- Now create the Data Access Implementation class named NewEntityDA inside the Data Access Implementation project.
Use the following template as shown below:
- Build the Data Access Project
- Now you can try accessing the new Data Access class through the property WebApp.NewEntityDA as shown below.
WebApp.DataAccess.NewEntityDA.GetAll();
// If the new property is not displayed, try refreshing the project references
Scenario 2: Adding a new Business Logic class
Please follow the steps to do that:
- Select the Business Logic project and use Add New Item named NewBL
You can use the following template:
- Build the Business Logic project
- Now try accessing the new Business Logic class property from the web application using the WebApp instance manager class.
WebApp.BusinessLogic.NewBL
// If the new property is not displayed, try refreshing the project references
Note
You can try using a breakpoint and debugging through all the layers to see how it works end to end.
Source
This document is meant to provide information on a NLayers project. You can access the latest source or documentation from:
http://nlayers.codeplex.com/
Summary
This article explains some examples of NLayers. You can try executing the example application attached along with the article.