How To Return A Pair Of Closest Birthdates Using API, Multi Threading And MVC 5 Using C#.NET

This article demonstrates how to use multi-threading to return the name of  two customers who have the closest birthdate. Also, it includes how to create API to add/retrieve customer details from SQL database using tasks and how to use MVC to call an API to add a new customer. 

The proposed algorithm

  • For each customer, find the difference between the selected customer and the rest of the customers.
  • Do the same for all other customers. Note that you don’t have to look for the differences if it was already calculated. See the diagram below.

The complexity for calculating the diff between n customers will be equal to

n2-n-((n2-n)/2)

To find the closest birthday between 10 customers, you need to calculate 45 operations.

See below

 0123456789
0 0,10,20,30,40,50,60,70,80,9
1  1,21,31,41,51,61,71,81,9
2   2,32,42,52,62,72,82,9
3    3,43,53,63,73,83,9
4     4,54,64,74,84,9
5      5,65,75,85,9
6       6,76,86,9
7        7,87,9
8         8,9
9          

The main idea is to find the difference between n customers using multi-threading where each task will return the minimum difference between one customer and the rest of the customers. The tasks will be executed asynchronously on a separate processor. Each task will return an object that will include the customer index in the array, the index of the customer with minimum difference, and the difference value.

The last step is to find the objects that have the minimum difference from all the returned task objects. The resulted object will include the index of the first customer and the index of the second customer with closest birthdates.

The project is created using Visual Studio 2015 and MVC5

Create an SQL database with one table using the following script.

  1. CREATE DATABASE [CustomersDB]  
  2. GO  
  3.   
  4. SET ANSI_NULLS ON  
  5. GO  
  6.   
  7. SET QUOTED_IDENTIFIER ON  
  8. GO  
  9.   
  10. CREATE TABLE [dbo].[PersonalInformation](  
  11.     [ID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_PersonalInformation_ID]      
  12.                            DEFAULT (newid()),  
  13.     [FirstName] [nvarchar](50) NULL,  
  14.     [LastName] [nvarchar](50) NULL,  
  15.     [BirthDate] [datetime] NULL  
  16. ON [PRIMARY]  
  17.   
  18. GO  

C#

Create a new MVC5 project using Visual Studio 2015.

C#

Under Controller folder, add a new API called CustomerApiController, allow the user to add a new customer to the database, retrieve all customer IDs, and retrieve each customer's details.

C#

  • The API controller code is as below.
    1. public class CustomerApiController : ApiController  
    2.     {  
    3.       [HttpPost]  
    4.         public IHttpActionResult AddClient(Customer customer)  
    5.         {  
    6.             Repository.AddCustomers(customer);  
    7.             return Ok(customer);  
    8.         }  
    9.   
    10.   
    11.         [HttpGet]  
    12.         [ActionName("GetCustomer")]  
    13.         [ResponseType(typeof(Customer))]  
    14.         public IHttpActionResult GetCustomer(Guid id)  
    15.         {  
    16.             return  Ok(Repository.CustomerInfo(id));  
    17.         }  
    18.   
    19.           
    20.         [HttpGet]  
    21.         [ActionName("CustomersOId")]  
    22.         public IEnumerable<Guid> CustomersOId()  
    23.         {  
    24.             return Repository.CustomersOID();  
    25.         }  
    26.     } 

2-    Need to create the model part to enable connecting to the SQL. Under the Models folder, create the Repository.cs files.

C#

... which include retrieving and adding data to the customer database tables.

Note

You can use any ORM Model but this is not part of this article.

  • The Repository code is,
    1. public static class Repository  
    2.  {  
    3.           
    4.  /// <summary>  
    5.  /// Add customer to the customer table  
    6.  /// </summary>  
    7.  /// <param name="customer"></param>  
    8.  public static void AddCustomers(Customer customer)  
    9.  {  
    10.  string sql = "INSERT INTO PersonalInformation(FirstName,LastName,BirthDate)    
    11.                 VALUES(@FirstName,@LastName,@BirthDate)";  
    12. using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["CustomerConnection"].ToString()))  
    13.   {  
    14.         connection.Open();  
    15.                 using (SqlTransaction transaction = connection.BeginTransaction())  
    16.                 {  
    17.  using (SqlCommand command = new SqlCommand(sql, connection, transaction))  
    18.    {  
    19.           command.CommandType = CommandType.Text;  
    20.           command.Parameters.AddWithValue("@FirstName", customer.FirstName);  
    21.           command.Parameters.AddWithValue("@LastName", customer.LastName);  
    22.           command.Parameters.AddWithValue("@BirthDate", customer.BirthDate);  
    23.           command.ExecuteNonQuery();  
    24.           transaction.Commit();  
    25.     }  
    26.    }  
    27.   }  
    28.  }  
    29.   
    30. /// Get All Customers OID’s  
    31.   
    32. public static List<Guid> CustomersOID()  
    33. {  
    34.             string sql = "SELECT ID from PersonalInformation";  
    35.   
    36.             List<Guid> customerOIds = new List<Guid>();  
    37.             using (SqlConnection connection =  
    38.                 new SqlConnection(ConfigurationManager.ConnectionStrings["CustomerConnection"].ToString()))  
    39.             {  
    40.                 connection.Open();  
    41.                 using (SqlCommand sqlCommand = new SqlCommand(sql, connection))  
    42.                 {  
    43.                     using (SqlDataReader reader = sqlCommand.ExecuteReader())  
    44.                     {  
    45.                         while (reader.Read())  
    46.                         {  
    47.                             Guid guid = reader.GetGuid(0);  
    48.                             customerOIds.Add(guid);  
    49.                         }  
    50.   
    51.                     }  
    52.                 }  
    53.             }  
    54.             return customerOIds;  
    55.         }  
    56.   
    57. /// Get Customer detail once we pass the guid  
    58.   
    59.         public static Customer CustomerInfo(Guid guid)  
    60.         {  
    61.             string sql = "SELECT FirstName,LastName,BirthDate from PersonalInformation where ID =@ID";  
    62.             Customer C1 = new Customer();  
    63.             using (SqlConnection connection =new SqlConnection(ConfigurationManager.ConnectionStrings["CustomerConnection"].ToString()))  
    64.             {  
    65.                 connection.Open();  
    66.                 using (SqlCommand sqlCommand = new SqlCommand(sql, connection))  
    67.                 {  
    68.                     sqlCommand.Parameters.Add(new SqlParameter("@ID", SqlDbType.UniqueIdentifier)).Value = guid;  
    69.                     using (SqlDataReader reader = sqlCommand.ExecuteReader())  
    70.                     {  
    71.   
    72.                         while (reader.Read())  
    73.                         {  
    74.                             C1.FirstName = reader.GetString(0);  
    75.                             C1.LastName= reader.GetString(1);  
    76.                             C1.BirthDate = reader.GetDateTime(2);  
    77.                         }  
    78.   
    79.                     }  
    80.                 }  
    81.             }  
    82.             return C1;  
    83.         }  
    84.    }  
  • Create the Customer.cs file that defines the customer object ( you can create the file under Model folder).
    1. public class Customer  
    2. {  
    3.     public Guid ID { get; set; }  
    4.     public string FirstName { get; set; }  
    5.     public string LastName { get; set; }  
    6.     public DateTime BirthDate { get; set; }  
    7. }  

Build the algorithm of finding the closest birthdates using multi-threading. You can create a new folder to add the classes needed to find the closest birthdates or for simplicity, I have added the 2 required classes for the algorithm DiffClass and Resultstruct under Models folder.

C#

  • Create the cs. This is a static class used to define 2 static functions,

    • The first function, FindMinDiff, will accept 2 parameters - customer and index (n). You need to find the diff with the other customers in a list of all retrieved customers (CustomerDate). It will return ResultStruct object which includes the customer number, the index of the nearest customer birthday and the difference between the 2 birthdays.
    • The second function, ClosestBirthDate, will create a list of tasks and assign each customer to a separate task and wait for all the tasks to complete. Then, find the minimum difference between all the returned results and return the nearest birthdays of the 2 customers.

  • See the code below.
    1. public static class DiffClass  
    2.  {  
    3.  /// <summary>  
    4.  ///   
    5.  /// </summary>  
    6.  /// <param name="no"></param>  
    7.  /// <param name="customersDate"></param>  
    8.  /// <param name="arraysize"></param>  
    9.  /// <returns></returns>  
    10. public static ResultStruct FindMinDiff(int no, List<Customer> customersDate,int arraysize)  
    11. {  
    12.    double[] diffs = new double[arraysize];  
    13.    int index = -1;  
    14.    DateTime pivot = customersDate[no].BirthDate;  
    15.   
    16.    for (int i = 0; i < customersDate.Count; i++)  
    17.     if (no < i) diffs[i] = Math.Abs((pivot - customersDate[i].BirthDate).TotalDays);  
    18.   else diffs[i] = -1;  
    19.   
    20.   index = Enumerable.Range(0, diffs.Length)  
    21.                 .Aggregate((a, b) => (diffs[a] < diffs[b] && diffs[a] != -1 && diffs[b] != -1) ? a : b);  
    22.     return new ResultStruct() {PivotNumber = no, IndexResult = index, DiffValue = diffs[index]};  
    23.   }  
    24.   
    25.           
    26.   
    27.   
    28.   
    29.   
    30. /// <summary>  
    31. ///   
    32. /// </summary>  
    33. /// <param name="Customers"></param>  
    34. /// <returns></returns>  
    35. public static List<Customer> ClosestBirthDate(List<Customer> Customers)  
    36. {  
    37.  Task<ResultStruct>[] tasks = new Task<ResultStruct>[Customers.Count-1];  
    38.               
    39.       for (int ctr = 0; ctr < Customers.Count-1; ctr++)  
    40.           {  
    41.          int factor = ctr;  
    42.          tasks[ctr] = Task.Run(() => FindMinDiff(factor, Customers, Customers.Count));  
    43.           }  
    44.     Task.WaitAll(tasks);  
    45.     var min = tasks.Min(t => t.Result.DiffValue);  
    46.     var item = tasks.First(t => t.Result.DiffValue == min);  
    47.     List<Customer> rsesutedCustomers = new List<Customer>();  
    48.     rsesutedCustomers.Add(Customers[item.Result.PivotNumber]);  
    49.     rsesutedCustomers.Add(Customers[item.Result.IndexResult]); ;  
    50.   
    51.  return rsesutedCustomers;  
    52.  }  
    53. }  
  • cs is the class definition where the result is returned from FindMinDiff function
    1. // class that define the returning object   
    2.     public class ResultStruct  
    3.     {  
    4.         public int PivotNumber;  
    5.         public int IndexResult;  
    6.         public double DiffValue;  
    7.     } 

Create the CustomerControllers.cs under Controllers folder.

C#

This control has 3 main Controllers - Index, Create, and GetClosestBirthday; where the Index and Create controllers are used to help the user add new customer to the DB using the Index.cshtml View.

The GetClosestBirthday() controller is used to call the 2 customer APIs through 2 functions, GetCustomerIDs() and CallCustomer(Guid oid) where the first asyn task function is used to get all the id of the customers and the second async function is used to get all the customer details and finally call the DiffClass.ClosestBirthDate() static function to get the closet birthdates and display the result in a View.

The following is the customerController code.

  1. public class CustomerController : Controller  
  2.     {  
  3.         // GET: Customer  
  4.   
  5.         private HttpClient _httpClient;  
  6.         private string url= "http://localhost:56237/api/CustomerApi";  
  7.   
  8.         public CustomerController()  
  9.         {  
  10.             _httpClient = new HttpClient();  
  11.             _httpClient.BaseAddress = new Uri(url);  
  12.             _httpClient.DefaultRequestHeaders.Accept.Clear();  
  13.             _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
  14.         }  
  15.         public ActionResult Index()  
  16.         {  
  17.             return View();  
  18.         }  
  19.   
  20.         /// <summary>  
  21.         /// controller to add customer to the database using Api  
  22.         /// </summary>  
  23.         /// <param name="C1"></param>  
  24.         /// <returns></returns>  
  25.         [HttpPost]  
  26.         public async Task<ActionResult> Create(Customer C1)  
  27.         {  
  28.             url = "http://localhost:56237/api/CustomerApi";  
  29.             HttpResponseMessage responseMessage = await _httpClient.PostAsJsonAsync(url, C1);  
  30.              
  31.             if (responseMessage.IsSuccessStatusCode)  
  32.             {  
  33.   
  34.                 return RedirectToAction("Index");  
  35.             }  
  36.             return RedirectToAction("Index");  
  37.   
  38.         }  
  39.   
  40.         /// <summary>  
  41.         /// get the customer detail by passing the customer oid using API  
  42.         /// </summary>  
  43.         /// <param name="oid"></param>  
  44.         /// <returns></returns>  
  45.   
  46.         public async Task<Customer> CallCustomer(Guid oid)  
  47.         {  
  48.             string requestUri = string.Format("http://localhost:56237/api/CustomerApi/GetCustomer/{0}", oid);  
  49.   
  50.             HttpResponseMessage responseMessage = await _httpClient.GetAsync(requestUri).ConfigureAwait(true);   
  51.             if (responseMessage.IsSuccessStatusCode)  
  52.             {  
  53.                 var responseData =  responseMessage.Content.ReadAsStringAsync().Result;  
  54.                 return JsonConvert.DeserializeObject<Customer>(responseData);  
  55.             }  
  56.             return new Customer();  
  57.         }  
  58.   
  59.   
  60.         /// <summary>  
  61.         /// Get all customer oid's from the database using API  
  62.         /// </summary>  
  63.         /// <returns></returns>  
  64.         public async Task<List<object>> GetCustomerIDs()  
  65.         {  
  66.             url = "http://localhost:56237/api/CustomerApi/CustomersOId";  
  67.             HttpResponseMessage responseMessage = await _httpClient.GetAsync(url);  
  68.             List<Object> CustomersOId = new List<object>();  
  69.             if (responseMessage.IsSuccessStatusCode)  
  70.             {  
  71.                 var responseData = responseMessage.Content.ReadAsStringAsync().Result;  
  72.                 CustomersOId = JsonConvert.DeserializeObject<List<Object>>(responseData);  
  73.             }  
  74.             return CustomersOId;  
  75.         }  
  76.   
  77.   
  78.         /// <summary>  
  79.         /// Contoller to retreive the closest birthday of 2 customers  
  80.         /// </summary>  
  81.         /// <returns></returns>  
  82.   
  83.         public ActionResult GetClosestBirthday()  
  84.         {  
  85.             List<Customer> Customers = new List<Customer>();  
  86.             List<Task<Customer>> tasks = new List<Task<Customer>>();  
  87.   
  88.             var CustomersOId = Task.Run(async () => { return await GetCustomerIDs(); }).Result;  
  89.   
  90.             foreach (var item in CustomersOId)  
  91.             {  
  92.                 Guid oid;  
  93.                 Guid.TryParse(item.ToString(), out oid);  
  94.                 tasks.Add(item: Task<Customer>.Run(() => CallCustomer(oid)));  
  95.             }  
  96.   
  97.             Task.WaitAll(tasks.ToArray());  
  98.             tasks.ForEach(item => { Customers.Add(item.Result); });  
  99.   
  100.             var result = DiffClass.ClosestBirthDate(Customers);  
  101.             return View(result);  
  102.         }  
  103.     }  

Finally, you also need to create 2 views under the Views folder. Create a folder “Customers” and add 2 Views, index.cshtml and GetClosestBirthday. The first one adds a new customer information to the DB while the second one displays the closest birthdates of two customers.

C#

Following is the code of the 2 Views.

Index.cshtml

  1. @model CustomersApi.Models.Customer  
  2.   
  3. @{  
  4.     ViewBag.Title = "Index";  
  5. }  
  6.   
  7. <h2>Index</h2>  
  8.   
  9. @using (Html.BeginForm("Create","customer"))   
  10. {  
  11.     @Html.AntiForgeryToken()  
  12.       
  13.     <div class="form-horizontal">  
  14.         <h4>Customer</h4>  
  15.         <hr />  
  16.         @Html.ValidationSummary(true""new { @class = "text-danger" })  
  17.         <div class="form-group">  
  18.             @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })  
  19.             <div class="col-md-10">  
  20.                 @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new {@class = "form-control",autofocus = "" } })  
  21.                 @Html.ValidationMessageFor(model => model.FirstName, ""new { @class = "text-danger" })  
  22.             </div>  
  23.         </div>  
  24.   
  25.         <div class="form-group">  
  26.             @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })  
  27.             <div class="col-md-10">  
  28.                 @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })  
  29.                 @Html.ValidationMessageFor(model => model.LastName, ""new { @class = "text-danger" })  
  30.             </div>  
  31.         </div>  
  32.   
  33.         <div class="form-group">  
  34.             @Html.LabelFor(model => model.BirthDate, htmlAttributes: new { @class = "control-label col-md-2" })  
  35.             <div class="col-md-10">  
  36.                 @Html.EditorFor(model => model.BirthDate, new { htmlAttributes = new { @class = "form-control" } })  
  37.                 @Html.ValidationMessageFor(model => model.BirthDate, ""new { @class = "text-danger" })  
  38.             </div>  
  39.         </div>  
  40.   
  41.         <div class="form-group">  
  42.             <div class="col-md-offset-2 col-md-10">  
  43.                 <input type="submit" value="Create" class="btn btn-default" />  
  44.             </div>  
  45.         </div>  
  46.     </div>  
  47. }  
  48.   
  49. <div>  
  50.     @Html.ActionLink("Back to List""Index")  
  51. </div>  

GetClosestBirthday.cshtml 

  1. @model IEnumerable<CustomersApi.Models.Customer>  
  2.   
  3. @{  
  4.     ViewBag.Title = "GetSmallestBirthday";  
  5. }  
  6.   
  7. <h1> Customers with the nearest Birthday</h1>  
  8.   
  9. @using (Html.BeginForm("GetSmallestBirthday""customer"))  
  10. {<table class="table table-striped ">  
  11.      <thead>  
  12.      <tr>  
  13.          <th >  
  14.              @Html.DisplayNameFor(model => model.FirstName)  
  15.          </th>  
  16.          <th >  
  17.              @Html.DisplayNameFor(model => model.LastName)  
  18.          </th>  
  19.          <th >  
  20.              @Html.DisplayNameFor(model => model.BirthDate)  
  21.          </th>  
  22.          <th >  
  23.   
  24.          </th>  
  25.   
  26.      </tr>  
  27.      </thead>  
  28.      <tbody>  
  29.      @if (Model != null)  
  30.      {  
  31.          foreach (var item in Model)  
  32.           {  
  33.               <tr style="vertical-align: middle">  
  34.                   <td >  
  35.                       @Html.DisplayFor(modelItem => item.FirstName)  
  36.                   </td>  
  37.                   <td >  
  38.                       @Html.DisplayFor(modelItem => item.LastName)  
  39.                   </td>  
  40.                   <td >  
  41.                       @Html.DisplayFor(modelItem => item.BirthDate)  
  42.                   </td>  
  43.   
  44.               </tr>  
  45.           }  
  46.      }  
  47.      </tbody>  
  48.  </table>  
  49.  }  

Run the application.

To add a new customer, you should use the following URL http://localhost:56237/Customer.

To get the closest birthdates, use the following URL http://localhost:56237/Customer/GetClosestBirthday

The result is given below.

C#

C#

The application is built using Visual Studio 2015. Download the application, compile, and run it, and make sure to change the CustomerConnection to point to your SQL server.


Similar Articles