• We just launched and are currently in beta. Join us as we build and grow the community.

Facade Design Pattern with C#.NET and SQL Server

fgsdgf

Cybersecurity Architect
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
78
Likes
159
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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.gif


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.

fascade01.png


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. namespace

    Sonic_Core.

    Data

    .

    Commands

    .

    Locations
  11. {
  12. class

    InsertLocationDataAction :

    SoniceDataAction<

    int

    >
  13. {


  14. Location location_O =

    null

    ;

  15. public

    InsertLocationDataAction(

    Location location)
  16. {
  17. location_O =

    location;
  18. }


  19. public

    override

    int

    Body(

    System.Data

    .

    SqlClient

    .

    SqlConnection

    PConn)
  20. {
  21. //throw new NotImplementedException();
  22. try
  23. {
  24. int

    LVCount =

    0

    ;
  25. SqlCommand LVCommand =

    new

    SqlCommand(

    )

    ;
  26. LVCommand.

    Connection

    =

    PConn;
  27. LVCommand.

    CommandType

    =

    CommandType.

    StoredProcedure

    ;
  28. LVCommand.

    CommandText

    =

    "[dbo].[sp_tblLocationInsert]"

    ;
  29. LVCommand.

    Parameters

    .

    AddWithValue

    (

    "@loc_code"

    , location_O.

    loc_code

    )

    ;
  30. LVCommand.

    Parameters

    .

    AddWithValue

    (

    "@loc_com_id"

    , location_O.

    loc_company_id

    )

    ;
  31. LVCommand.

    Parameters

    .

    AddWithValue

    (

    "@loc_name"

    , location_O.

    loc_name

    )

    ;
  32. LVCommand.

    Parameters

    .

    AddWithValue

    (

    "@loc_address"

    , location_O.

    loc_address

    )

    ;
  33. LVCommand.

    Parameters

    .

    AddWithValue

    (

    "@loc_tele_phone"

    , location_O.

    loc_tele_phone

    )

    ;
  34. LVCommand.

    Parameters

    .

    AddWithValue

    (

    "@loc_status"

    , location_O.

    loc_status

    )

    ;
  35. LVCount =

    LVCommand.

    ExecuteNonQuery

    (

    )

    ;
  36. return

    LVCount;


  37. }
  38. catch

    (

    SonicDatabaseException ex)
  39. {
  40. throw

    new

    SonicDatabaseException(

    ex)

    ;
  41. }
  42. }

  43. }
  44. }

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. namespace

    Sonic_Core.

    Data

    .

    Commands

    .

    Locations
  11. {
  12. class

    UpdateLocationDataAction :

    SoniceDataAction<

    int

    >
  13. {
  14. Location location_O =

    null

    ;

  15. public

    UpdateLocationDataAction(

    Location location)
  16. {
  17. location_O =

    location;
  18. }

  19. public

    override

    int

    Body(

    System.Data

    .

    SqlClient

    .

    SqlConnection

    PConn)
  20. {
  21. //throw new NotImplementedException();
  22. try
  23. {
  24. int

    LVCount =

    0

    ;
  25. SqlCommand LVCommand =

    new

    SqlCommand(

    )

    ;
  26. LVCommand.

    Connection

    =

    PConn;
  27. LVCommand.

    CommandType

    =

    CommandType.

    StoredProcedure

    ;
  28. LVCommand.

    CommandText

    =

    "[dbo].[sp_tblLocationUpdate]"

    ;
  29. LVCommand.

    Parameters

    .

    AddWithValue

    (

    "@loc_id"

    , location_O.

    loc_id

    )

    ;
  30. LVCommand.

    Parameters

    .

    AddWithValue

    (

    "@loc_code"

    , location_O.

    loc_code

    )

    ;
  31. LVCommand.

    Parameters

    .

    AddWithValue

    (

    "@loc_com_id"

    , location_O.

    loc_company_id

    )

    ;
  32. LVCommand.

    Parameters

    .

    AddWithValue

    (

    "@loc_name"

    , location_O.

    loc_name

    )

    ;
  33. LVCommand.

    Parameters

    .

    AddWithValue

    (

    "@loc_address"

    , location_O.

    loc_address

    )

    ;
  34. LVCommand.

    Parameters

    .

    AddWithValue

    (

    "@loc_tele_phone"

    , location_O.

    loc_tele_phone

    )

    ;
  35. LVCommand.

    Parameters

    .

    AddWithValue

    (

    "@loc_status"

    , location_O.

    loc_status

    )

    ;
  36. LVCount =

    LVCommand.

    ExecuteNonQuery

    (

    )

    ;
  37. return

    LVCount;


  38. }
  39. catch

    (

    SonicDatabaseException ex)
  40. {
  41. throw

    new

    SonicDatabaseException(

    ex)

    ;
  42. }
  43. }
  44. }
  45. }

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. using

    Sonic_Core.Exceptions.DataBase

    ;

  10. namespace

    Sonic_Core.

    Data

    .

    Commands

    .

    Locations
  11. {
  12. class

    GetAllLocations :

    SoniceDataAction<

    DataTable>
  13. {

  14. Location location_O =

    null

    ;

  15. public

    GetAllLocations(

    )
  16. {

  17. }

  18. public

    GetAllLocations(

    Location location)
  19. {
  20. location_O =

    location;
  21. }


  22. public

    override

    DataTable Body(

    System.Data

    .

    SqlClient

    .

    SqlConnection

    PConn)
  23. {
  24. //throw new NotImplementedException();

  25. DataTable tblLocation =

    new

    DataTable(

    )

    ;

  26. tblLocation.

    Columns

    .

    Add

    (

    "ID"

    )

    ;
  27. tblLocation.

    Columns

    .

    Add

    (

    "Code"

    )

    ;
  28. tblLocation.

    Columns

    .

    Add

    (

    "Company ID"

    )

    ;
  29. tblLocation.

    Columns

    .

    Add

    (

    "Company"

    )

    ;
  30. tblLocation.

    Columns

    .

    Add

    (

    "Name"

    )

    ;
  31. tblLocation.

    Columns

    .

    Add

    (

    "Address"

    )

    ;
  32. tblLocation.

    Columns

    .

    Add

    (

    "Phone"

    )

    ;
  33. tblLocation.

    Columns

    .

    Add

    (

    "Status"

    )

    ;


  34. try
  35. {
  36. //int LVReader = 0;
  37. SqlCommand LVCommand =

    new

    SqlCommand(

    )

    ;
  38. LVCommand.

    Connection

    =

    PConn;
  39. LVCommand.

    CommandType

    =

    CommandType.

    StoredProcedure

    ;
  40. LVCommand.

    CommandText

    =

    "[dbo].[sp_tblLocationSelect_By_Status]"

    ;

  41. if

    (

    location_O.

    loc_id

    ==

    0

    )
  42. {
  43. LVCommand.

    Parameters

    .

    AddWithValue

    (

    "@loc_id"

    , location_O.

    loc_id

    )

    ;
  44. }
  45. else
  46. {

  47. LVCommand.

    Parameters

    .

    AddWithValue

    (

    "@loc_id"

    , location_O.

    loc_id

    )

    ;
  48. }
  49. LVCommand.

    Parameters

    .

    AddWithValue

    (

    "@status"

    , location_O.

    loc_status

    )

    ;
  50. SqlDataReader LVReader =

    LVCommand.

    ExecuteReader

    (

    )

    ;


  51. while

    (

    LVReader.

    Read

    (

    )

    )
  52. {
  53. //comp_o = new Sonic_Core.DomainObjects.Company();

  54. //comp_o._com_id = LVReader.GetInt32(0);
  55. //comp_o.com_code = LVReader.GetString(1);
  56. //comp_o.com_name = LVReader.GetString(2);
  57. //comp_o.com_address = LVReader.GetString(3);
  58. //comp_o.com_PhoneNo = LVReader.GetString(4);

  59. //CompList.Add(comp_o);

  60. tblLocation.

    Rows

    .

    Add

    (

    tblLocation.

    NewRow

    (

    )

    )

    ;


  61. tblLocation.

    Rows

    [

    tblLocation.

    Rows

    .

    Count

    -

    1

    ]

    [

    0

    ]

    =

    LVReader.

    GetInt32

    (

    0

    )

    ;
  62. tblLocation.

    Rows

    [

    tblLocation.

    Rows

    .

    Count

    -

    1

    ]

    [

    1

    ]

    =

    LVReader.

    GetString

    (

    1

    )

    ;
  63. tblLocation.

    Rows

    [

    tblLocation.

    Rows

    .

    Count

    -

    1

    ]

    [

    2

    ]

    =

    LVReader.

    GetInt32

    (

    2

    )

    ;
  64. tblLocation.

    Rows

    [

    tblLocation.

    Rows

    .

    Count

    -

    1

    ]

    [

    3

    ]

    =

    LVReader.

    GetString

    (

    3

    )

    ;
  65. tblLocation.

    Rows

    [

    tblLocation.

    Rows

    .

    Count

    -

    1

    ]

    [

    4

    ]

    =

    LVReader.

    GetString

    (

    4

    )

    ;
  66. tblLocation.

    Rows

    [

    tblLocation.

    Rows

    .

    Count

    -

    1

    ]

    [

    5

    ]

    =

    LVReader.

    GetString

    (

    5

    )

    ;
  67. tblLocation.

    Rows

    [

    tblLocation.

    Rows

    .

    Count

    -

    1

    ]

    [

    6

    ]

    =

    LVReader.

    GetString

    (

    6

    )

    ;
  68. tblLocation.

    Rows

    [

    tblLocation.

    Rows

    .

    Count

    -

    1

    ]

    [

    7

    ]

    =

    LVReader.

    GetString

    (

    7

    )

    ;

  69. }

  70. return

    tblLocation;
  71. }
  72. catch

    (

    SonicDatabaseException ex)
  73. {
  74. throw

    new

    SonicDatabaseException(

    ex)

    ;
  75. }
  76. }


  77. }
  78. }

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. namespace

    Sonic_Core.

    Data

    .

    Fascade
  10. {
  11. public

    class

    LocationDAO
  12. {

  13. public

    static

    int

    LocationInsert(

    Location location)
  14. {

  15. InsertLocationDataAction location_DA =

    new

    InsertLocationDataAction(

    location)

    ;
  16. return

    location_DA.

    execute

    (

    )

    ;
  17. }

  18. public

    static

    int

    LocationUpdate(

    Location location)
  19. {
  20. UpdateLocationDataAction Location_Upd =

    new

    UpdateLocationDataAction(

    location)

    ;
  21. return

    Location_Upd.

    execute

    (

    )

    ;
  22. }

  23. public

    static

    DataTable GetAllLocations(

    Location location)
  24. {
  25. GetAllLocations getLocations =

    new

    GetAllLocations(

    location)

    ;
  26. return

    getLocations.

    execute

    (

    )

    ;

  27. }


  28. public

    static

    int

    DeleteLocationByStatus(

    Location location)
  29. {
  30. DeleteLocationByStatusDataAction deletLocation =

    new

    DeleteLocationByStatusDataAction(

    location)

    ;
  31. return

    deletLocation.

    execute

    (

    )

    ;
  32. }

  33. }
  34. }


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top