Introduction

This article explains how to create a real time MVC Web Application using n-tier architecture. The complete article goes through a sample customer information solution.

Modules

  1. Create a SampleCustomerInformation database using SQL Server.

  2. Create an empty MVC web application (Presentation Layer) using Visual Studio 2013.

    a. Creating controllers, views and respective HTML and JavaScript required.

  3. Adding another project to the solution for processing the data from SQL Server. Create a C# library project (Data Access Layer).

    a. Add an entity data model to connect to the database.
    b. Create classes to process all the requests from Business Service Layer
    c. Write necessary methods to process the requests in business service layer.

  4. Add another project to the solution for writing business logic. Create a WCF Service Library:

    a. Add necessary service parameters to the project required to write business logic
    b. Write necessary operation contracts to make available for Presentation Layer
    c. Use all the available methods from Data Access Layer to process data in Service Layer.

  5. Add necessary references to the projects in the solution.

  6. Add a Service Reference to Presentation Layer.

    a. Use all the available methods from service layer and process the requests.

Technologies Used

  1. ASP.NET MVC 5
  2. SQL Server 2014
  3. C# v5
  4. jQuery 2.14
  5. HTML5
  6. CSS3

IDE: Visual Studio 2013

Using the code

Module 1:

Creating a SampleCustomerInformation database using SQL Server,
  1. /****** Object: Database [SampleCustomerInformation]******/
  2. CREATE DATABASE[SampleCustomerInformation]
  3. Create table CustomerInformation
  4. (
  5. SNO int not null identity(0, 1),
  6. CustomerIDint not null primary key, [First Name] varchar(50) not null, [Last Name] varchar(50) not null,
  7. Email varchar(60) null,
  8. Phone varchar(13) not null, [User Name] varchar(60) not null, [Password] varchar(12) not null)
  9. Create table CustomerLoginActivity
  10. (
  11. SNO int not null identity(0, 1), [User Name] int not null, [Password] varchar(60) not null, [Login Time] Date not null
  12. )
Module 2:

Create an empty MVC web application (Presentation Layer) using Visual Studio 2013, creating controllers, views and respective HTML and JavaScript required. Here, I used ajax jQuery to send data from View to Controller.

Some of many properties which can be used on ajax request,

  1. Method: Specify the type of operation (ex-GET, POST).
  2. Url: specify the path of the method.
  3. Data: specify the data which needs to transferred from View to Controller for processing.
  4. Success: Can specify set of statements needs to be done when success.
  5. Failure: Can specify set of statements needs to be done when failure.

More information about jQuery Ajax can be found here: jQuery Ajax

HTML

  1. <html>
  2. <head>
  3. <title>Welcome</title>
  4. <script src="~/Scripts/jquery-2.1.4.min.js">
  5. </script>
  6. <script src="~/Scripts/HomeScript.js">
  7. </script>
  8. </head>
  9. <body>
  10. <form style="float:left">
  11. <h4>Sign Up</h4>
  12. <table>
  13. <tr>
  14. <td>First Name</td>
  15. <td>
  16. <input type="text" name="firstName" id="firstName" placeholder="First Name" value="" required />
  17. </td>
  18. </tr>
  19. <tr>
  20. <td>Last Name</td>
  21. <td>
  22. <input type="text" name="lastName" id="lastName" placeholder="Last Name" value="" required />
  23. </td>
  24. </tr>
  25. <tr>
  26. <td>Email</td>
  27. <td>
  28. <input type="email" name="email" id="email" placeholder="Email" value="" required />
  29. </td>
  30. </tr>
  31. <tr>
  32. <td>Phone Number</td>
  33. <td>
  34. <input type="text" name="phoneNumber" placeholder="Phone Number" id="phoneNumber" value="" />
  35. </td>
  36. </tr>
  37. <tr>
  38. <td>Password</td>
  39. <td>
  40. <input type="password" name="password" placeholder="Password" id="password" value="" />
  41. </td>
  42. </tr>
  43. <tr>
  44. <td>Confirm Password</td>
  45. <td>
  46. <input type="password" name="confirmPassword" placeholder="Confirm Password" id="confirmPassword" value="" />
  47. </td>
  48. </tr>
  49. <tr>
  50. <td></td>
  51. <td>
  52. <input type="submit" name="btnRegister" id="btnRegister" value="Register" />
  53. </td>
  54. </tr>
  55. </table>
  56. </form>
  57. <div style="float:left">
  58. </div>
  59. <form style="float:left">
  60. <h4>Login</h4>
  61. <table>
  62. <tr>
  63. <td>User Name</td>
  64. <td>
  65. <input type="text" name="userName" placeholder="Email" id="userName" value="" />
  66. </td>
  67. </tr>
  68. <tr>
  69. <td>Password</td>
  70. <td>
  71. <input type="password" name="loginPassword" placeholder="Password" id="loginPassword" value="" />
  72. </td>
  73. </tr>
  74. <tr>
  75. <td></td>
  76. <td>
  77. <input type="submit" name="btnLogin" id="btnLogin" value="Login" />
  78. </td>
  79. </tr>
  80. </table>
  81. </form>
  82. </body>
  83. </html>
JavaScript
  1. $(document).ready(function()
  2. {
  3. $("#btnRegister").click(function()
  4. {
  5. RegisterCustomer();
  6. });
  7. $("#btnLogin").click(function()
  8. {
  9. LoginCustomer();
  10. });
  11. });
  12. functionRegisterCustomer()
  13. {
  14. var firstName = $("#firstName").val();
  15. var lastName = $("#lastName").val();
  16. var email = $("#email").val();
  17. var phoneNumber = $("#phoneNumber").val();
  18. var password = $("#password").val();
  19. varconfirmPassword = $("#confirmPassword");
  20. //Attributes in "data" of ajax request should match the arguments defined in the method in controller
  21. $.ajax({
  22. method: "POST",
  23. url: "../Home/RegisterCustomer",
  24. data:
  25. {
  26. firstName: firstName,
  27. lastName: lastName,
  28. email: email,
  29. phoneNumber: phoneNumber,
  30. password: password
  31. }
  32. }).done(function(msg)
  33. {
  34. if (msg == "success")
  35. alert("Login Successful");
  36. else
  37. alert("Login Failed: " + msg);
  38. });
  39. }
  40. functionLoginCustomer()
  41. {
  42. var userName = $("#userName").val();
  43. var loginPassword = $("#loginPassword").val();
  44. $.ajax({
  45. method: "GET",
  46. url: "../Home/LoginCustomer",
  47. data: {
  48. userName: userName,
  49. loginPassword: loginPassword
  50. }
  51. })
  52. .done(function(msg)
  53. {
  54. alert("Login Activity Saved");
  55. });
  56. }
Module 3:

Adding another project to the solution for processing the data from SQL Server. Create a C# library project (Data Access Layer),

Module 4:

Add another project to the solution for writing business logic. Create a WCF Service Library.

A Service Contract defines what all the methods or operations available to the client from service endpoint. A service contract is an interface and it tells the client what service can do.

An Operation Contract is defined within the service contract and it has return type and parameters. Those operation contracts are implemented in the class which extends the service interface. Client can use this method to do operations.

A Data Contract describes the information which needs to be exchanged and it can be used in any operation contract as a parameter or return type. Based on the parameters and return types the data will be serialized/de-serialized to or from (Binary ó XML).

A Data Member attribute needs to be applied all the members of the data contract to indicate it as a data member, which means it should be serialized.

More information about Service Contracts can be found here: Designing Service Contracts.
More information about Data Contracts can be found here: Using Data Contracts.

  1. [ServiceContract]
  2. public interface IService1
  3. {
  4. [OperationContract]
  5. RegisterCustomerRequest RegisterCustomer(RegisterCustomerRequest request);
  6. [OperationContract]
  7. LoginCustomerRequest LoginCustomer(LoginCustomerRequest request);
  8. // TODO: Add your service operations here
  9. }
  10. public class Service1: IService1
  11. {#region IService1 Members
  12. # region Register Customer
  13. public RegisterCustomerRequest RegisterCustomer(RegisterCustomerRequest request)
  14. {
  15. try {
  16. var registerCustomerDal = new RegisterCustomerDataAccessLayer();
  17. var customer = registerCustomerDal.RegisterCustomer(request.FirstName, request.LastName, request.Email, request.PhoneNumber, request.Password);
  18. if (customer)
  19. request.RegisterCustomerResponse = "Success";
  20. else
  21. request.RegisterCustomerResponse = "Failure";
  22. return request;
  23. } catch (Exception ex)
  24. {
  25. throw new Exception(ex.Message);
  26. }
  27. throw new NotImplementedException();
  28. }#
  29. endregion
  30. # region Login Customer
  31. public LoginCustomerRequest LoginCustomer(LoginCustomerRequest request)
  32. {
  33. try {
  34. var customerLoginDal = new CustomerLoginDataAccessLayer();
  35. var validateLogin = customerLoginDal.LoginCustomer(request.UserName, request.Password);
  36. if (validateLogin)
  37. request.LoginCustomerResponse = "Login Successful";
  38. else
  39. request.LoginCustomerResponse = "Login Failed";
  40. return request;
  41. } catch (Exception ex)
  42. {
  43. throw new Exception(ex.Message);
  44. }
  45. throw new NotImplementedException();
  46. }#endregion# endregion
  47. }
Module 6: Add a Service Reference to Presentation Layer,

To add a service reference,

Controller: Use all the available methods from service layer and process the requests,

  1. // GET: Home View
  2. public ActionResult Home()
  3. {
  4. return View();
  5. }
  6. //This method is used to register new customer
  7. //This method is passed as a url to the Ajax function RegisterCustomer()
  8. public string RegisterCustomer(string firstName, string lastName, string email, string phoneNumber, string password)
  9. {
  10. try {
  11. SampleCustomerInformationServiceReference.RegisterCustomerRequest request = new SampleCustomerInformationServiceReference.RegisterCustomerRequest();
  12. request.FirstName = firstName;
  13. request.LastName = lastName;
  14. request.Email = email;
  15. request.PhoneNumber = phoneNumber;
  16. request.Password = password;
  17. var response = client.RegisterCustomer(request);
  18. return response.RegisterCustomerResponse;
  19. } catch (Exception ex)
  20. {
  21. throw new Exception(ex.Message);
  22. }
  23. return "";
  24. }
  25. //This method is used to login a customer
  26. //This method is passed as a url to the Ajax function LoginCustomer()
  27. public string LoginCustomer(string userName, string loginPassword)
  28. {
  29. try {
  30. SampleCustomerInformationServiceReference.LoginCustomerRequest request = new SampleCustomerInformationServiceReference.LoginCustomerRequest();
  31. request.UserName = userName;
  32. request.Password = loginPassword;
  33. var response = client.LoginCustomer(request);
  34. return response.LoginCustomerResponse;
  35. } catch (Exception ex) {
  36. throw new Exception(ex.Message);
  37. }
  38. return "";
  39. }