Facade Design Pattern with C#.NET and SQL Server

Most of software companies are making high cost software for their clients around the world so software development should become profitable business to development companies. Standard development process, well define cording style always helpful to achieve their business goals. As my experience lot of software has some portion of common occurring problems. Developing same component for each client will be reducing profit of software companies. To overcome above issues now a day’s software companies used reusable component. What we called is software design pattern. In this tutorial I am going to teach you facade design pattern with c#.net. Facade design pattern provides unified interface to set of subsystems. This will simplified the interface to larger body of the code. So it is commonly used developing class libraries. Façade make software library easy to used, make library more readable and provide well define interface to poorly design code. facade design pattern Façade design pattern highly used when simple interface required to access complex subsystem, system is very complex and difficult to understand and need entry point to each level of layered software system. screen shot of the program Insert method
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Sonic_Core.Data.Core;
  6. using Sonic_Core.DomainObjects;
  7. using System.Data.SqlClient;
  8. using System.Data;
  9. using Sonic_Core.Exceptions.DataBase;
  10.  
  11. namespace Sonic_Core.Data.Commands.Locations
  12. {
  13. class InsertLocationDataAction : SoniceDataAction<int>
  14. {
  15.  
  16.  
  17. Location location_O = null;
  18.  
  19. public InsertLocationDataAction(Location location)
  20. {
  21. location_O = location;
  22. }
  23.  
  24.  
  25. public override int Body(System.Data.SqlClient.SqlConnection PConn)
  26. {
  27. //throw new NotImplementedException();
  28. try
  29. {
  30. int LVCount = 0;
  31. SqlCommand LVCommand = new SqlCommand();
  32. LVCommand.Connection = PConn;
  33. LVCommand.CommandType = CommandType.StoredProcedure;
  34. LVCommand.CommandText = "[dbo].[sp_tblLocationInsert]";
  35. LVCommand.Parameters.AddWithValue("@loc_code", location_O.loc_code);
  36. LVCommand.Parameters.AddWithValue("@loc_com_id", location_O.loc_company_id);
  37. LVCommand.Parameters.AddWithValue("@loc_name", location_O.loc_name);
  38. LVCommand.Parameters.AddWithValue("@loc_address", location_O.loc_address);
  39. LVCommand.Parameters.AddWithValue("@loc_tele_phone", location_O.loc_tele_phone);
  40. LVCommand.Parameters.AddWithValue("@loc_status", location_O.loc_status);
  41. LVCount = LVCommand.ExecuteNonQuery();
  42. return LVCount;
  43.  
  44.  
  45. }
  46. catch (SonicDatabaseException ex)
  47. {
  48. throw new SonicDatabaseException(ex);
  49. }
  50. }
  51.  
  52. }
  53. }
Update method
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Sonic_Core.Data.Core;
  6. using Sonic_Core.DomainObjects;
  7. using System.Data.SqlClient;
  8. using System.Data;
  9. using Sonic_Core.Exceptions.DataBase;
  10.  
  11. namespace Sonic_Core.Data.Commands.Locations
  12. {
  13. class UpdateLocationDataAction : SoniceDataAction<int>
  14. {
  15. Location location_O = null;
  16.  
  17. public UpdateLocationDataAction(Location location)
  18. {
  19. location_O = location;
  20. }
  21.  
  22. public override int Body(System.Data.SqlClient.SqlConnection PConn)
  23. {
  24. //throw new NotImplementedException();
  25. try
  26. {
  27. int LVCount = 0;
  28. SqlCommand LVCommand = new SqlCommand();
  29. LVCommand.Connection = PConn;
  30. LVCommand.CommandType = CommandType.StoredProcedure;
  31. LVCommand.CommandText = "[dbo].[sp_tblLocationUpdate]";
  32. LVCommand.Parameters.AddWithValue("@loc_id", location_O.loc_id);
  33. LVCommand.Parameters.AddWithValue("@loc_code", location_O.loc_code);
  34. LVCommand.Parameters.AddWithValue("@loc_com_id", location_O.loc_company_id);
  35. LVCommand.Parameters.AddWithValue("@loc_name", location_O.loc_name);
  36. LVCommand.Parameters.AddWithValue("@loc_address", location_O.loc_address);
  37. LVCommand.Parameters.AddWithValue("@loc_tele_phone", location_O.loc_tele_phone);
  38. LVCommand.Parameters.AddWithValue("@loc_status", location_O.loc_status);
  39. LVCount = LVCommand.ExecuteNonQuery();
  40. return LVCount;
  41.  
  42.  
  43. }
  44. catch (SonicDatabaseException ex)
  45. {
  46. throw new SonicDatabaseException(ex);
  47. }
  48. }
  49. }
  50. }
get all method
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Sonic_Core.Data.Core;
  6. using Sonic_Core.DomainObjects;
  7. using System.Data.SqlClient;
  8. using System.Data;
  9.  
  10. using Sonic_Core.Exceptions.DataBase;
  11.  
  12. namespace Sonic_Core.Data.Commands.Locations
  13. {
  14. class GetAllLocations : SoniceDataAction<DataTable>
  15. {
  16.  
  17. Location location_O = null;
  18.  
  19. public GetAllLocations()
  20. {
  21.  
  22. }
  23.  
  24. public GetAllLocations(Location location)
  25. {
  26. location_O = location;
  27. }
  28.  
  29.  
  30. public override DataTable Body(System.Data.SqlClient.SqlConnection PConn)
  31. {
  32. //throw new NotImplementedException();
  33.  
  34. DataTable tblLocation = new DataTable();
  35.  
  36. tblLocation.Columns.Add("ID");
  37. tblLocation.Columns.Add("Code");
  38. tblLocation.Columns.Add("Company ID");
  39. tblLocation.Columns.Add("Company");
  40. tblLocation.Columns.Add("Name");
  41. tblLocation.Columns.Add("Address");
  42. tblLocation.Columns.Add("Phone");
  43. tblLocation.Columns.Add("Status");
  44.  
  45.  
  46. try
  47. {
  48. //int LVReader = 0;
  49. SqlCommand LVCommand = new SqlCommand();
  50. LVCommand.Connection = PConn;
  51. LVCommand.CommandType = CommandType.StoredProcedure;
  52. LVCommand.CommandText = "[dbo].[sp_tblLocationSelect_By_Status]";
  53.  
  54. if (location_O.loc_id == 0)
  55. {
  56. LVCommand.Parameters.AddWithValue("@loc_id", location_O.loc_id);
  57. }
  58. else
  59. {
  60.  
  61. LVCommand.Parameters.AddWithValue("@loc_id", location_O.loc_id);
  62. }
  63. LVCommand.Parameters.AddWithValue("@status", location_O.loc_status);
  64. SqlDataReader LVReader = LVCommand.ExecuteReader();
  65.  
  66.  
  67. while (LVReader.Read())
  68. {
  69. //comp_o = new Sonic_Core.DomainObjects.Company();
  70.  
  71. //comp_o._com_id = LVReader.GetInt32(0);
  72. //comp_o.com_code = LVReader.GetString(1);
  73. //comp_o.com_name = LVReader.GetString(2);
  74. //comp_o.com_address = LVReader.GetString(3);
  75. //comp_o.com_PhoneNo = LVReader.GetString(4);
  76.  
  77. //CompList.Add(comp_o);
  78.  
  79. tblLocation.Rows.Add(tblLocation.NewRow());
  80.  
  81.  
  82. tblLocation.Rows[tblLocation.Rows.Count - 1][0] = LVReader.GetInt32(0);
  83. tblLocation.Rows[tblLocation.Rows.Count - 1][1] = LVReader.GetString(1);
  84. tblLocation.Rows[tblLocation.Rows.Count - 1][2] = LVReader.GetInt32(2);
  85. tblLocation.Rows[tblLocation.Rows.Count - 1][3] = LVReader.GetString(3);
  86. tblLocation.Rows[tblLocation.Rows.Count - 1][4] = LVReader.GetString(4);
  87. tblLocation.Rows[tblLocation.Rows.Count - 1][5] = LVReader.GetString(5);
  88. tblLocation.Rows[tblLocation.Rows.Count - 1][6] = LVReader.GetString(6);
  89. tblLocation.Rows[tblLocation.Rows.Count - 1][7] = LVReader.GetString(7);
  90.  
  91. }
  92.  
  93. return tblLocation;
  94. }
  95. catch (SonicDatabaseException ex)
  96. {
  97. throw new SonicDatabaseException(ex);
  98. }
  99. }
  100.  
  101.  
  102. }
  103. }
Facade
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Sonic_Core.DomainObjects;
  6. using Sonic_Core.Data.Commands;
  7. using System.Data;
  8. using Sonic_Core.Data.Commands.Locations;
  9.  
  10. namespace Sonic_Core.Data.Fascade
  11. {
  12. public class LocationDAO
  13. {
  14.  
  15. public static int LocationInsert(Location location)
  16. {
  17.  
  18. InsertLocationDataAction location_DA = new InsertLocationDataAction(location);
  19. return location_DA.execute();
  20. }
  21.  
  22. public static int LocationUpdate(Location location)
  23. {
  24. UpdateLocationDataAction Location_Upd = new UpdateLocationDataAction(location);
  25. return Location_Upd.execute();
  26. }
  27.  
  28. public static DataTable GetAllLocations(Location location)
  29. {
  30. GetAllLocations getLocations = new GetAllLocations(location);
  31. return getLocations.execute();
  32.  
  33. }
  34.  
  35.  
  36. public static int DeleteLocationByStatus(Location location)
  37. {
  38. DeleteLocationByStatusDataAction deletLocation = new DeleteLocationByStatusDataAction(location);
  39. return deletLocation.execute();
  40. }
  41.  
  42. }
  43. }

Add new comment