NLayers Examples


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.

  1. Download the solution from here.
  2. Extract the zip file and open the solution.
  3. Make the NLayersExample.Web project as the startup project.
  4. Locate the Database.sql file inside the web project and execute it against your SQL Server.
  5. Modify the web.config with the SQL Server name.
  6. Execute (F5) the example.

On successful execution you will see the following web page:

NLayer1.gif

The Solution Explorer looks like below:

NLayer2.gif

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:

  1. Inside the Entity project, open the EDMX file
  2. 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)

    NLayer3.gif
     
  3. Create a new interface named INewEntityDA inside the Data Access Interface project.

    NLayer4.gif

    Use the following template as shown below:

    NLayer5.gif

    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.
     
  4. Now create the Data Access Implementation class named NewEntityDA inside the Data Access Implementation project.

    NLayer6.gif

    Use the following template as shown below:

    NLayer7.gif
     
  5. Build the Data Access Project
     
  6. 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:

  1. Select the Business Logic project and use Add New Item named NewBL

    NLayer8.gif

    You can use the following template:

    NLayer9.gif
     
  2. Build the Business Logic project
     
  3. 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.
 

Up Next
    Ebook Download
    View all
    Learn
    View all