locked
Repository Pattern RRS feed

  • Question

  • User-1555788080 posted

    Hi All,

    I have to design repository pattern for myweb application.

    I have two different database servers one is SQL server and other is SQL Azure. Database and database schema is same in both servers. So I have cereated an interface and gave two implementations( Repositories ), one for SQL server and other for SQL Azure. After implementing I observed that methods written in both implemetations are same just connection string is different.

    Thus resulted in a lot of duplicated code. How to design repository pattern considering above scenario to avoid duplicate code. 

    Thursday, May 2, 2013 10:52 AM

Answers

  • User-488622176 posted

    In this case, I'd separate the deployement logic from the application logic. You can use a variable "HostingEnvironment" in your web.config, that could have 2 values : "OnPremise" or "Azure". The factory reads this variable and creates the right repository.

    If you really need 2 different implementations (in this case, I consider the information the dbase scheme alikeness not relevant)

    • If you need only one of them at the same time
      • Create 2 repository classes, each containing the logic for 1 implementation
      • Apply the factory logic I mentioned in the lines above (with the variable)
    • If you need both at the same time (meaning : you store everything in 2 dbases)
      • Create 2 repository classes, each containing the logic for 1 implementation
      • Create 1 wrapper repository, with an inner list of dedicated repositories
      • The wrapper will invoke the corresponding function from each containing repository

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 3, 2013 5:44 AM

All replies

  • User-760709272 posted

    You could make just a generic class rather than a SQL or SQL Azure specific class, then pass the required connection string in on the constructor.

    Or you could make your existing class an abstract class, create a SQL class that inherits it, and also a SQL Azure class that does the same, and that way you have implementation-specific class names.

    Thursday, May 2, 2013 11:01 AM
  • User1759999623 posted

    The purpose of the repositiory pattern is to abstract your data model, which, I'm guessing, you would have two: one for the SQL Server and one of SQL (Azure) Database.  Since the models (class definitions) are the same, you could use the same repository pattern for both data models.  Your repository pattern design should have no knowledge of the ultimate data source.

    Thursday, May 2, 2013 11:06 AM
  • User-488622176 posted

    You only need 1 repository. However, it is possible to create a repository factory that passes a connection string to your repository. The factory then decides wither to use the SQL or Azure database connection string, based upon your logic. This enables you to do both, while keeping the connection logic separated from the repository

    Thursday, May 2, 2013 5:16 PM
  • User-1555788080 posted

    Thanks to every one,

    But we must have to different implementations, one is for in premise( MS SQL) and other is for Sql Azure.

    Friday, May 3, 2013 2:44 AM
  • User-488622176 posted

    In this case, I'd separate the deployement logic from the application logic. You can use a variable "HostingEnvironment" in your web.config, that could have 2 values : "OnPremise" or "Azure". The factory reads this variable and creates the right repository.

    If you really need 2 different implementations (in this case, I consider the information the dbase scheme alikeness not relevant)

    • If you need only one of them at the same time
      • Create 2 repository classes, each containing the logic for 1 implementation
      • Apply the factory logic I mentioned in the lines above (with the variable)
    • If you need both at the same time (meaning : you store everything in 2 dbases)
      • Create 2 repository classes, each containing the logic for 1 implementation
      • Create 1 wrapper repository, with an inner list of dedicated repositories
      • The wrapper will invoke the corresponding function from each containing repository

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 3, 2013 5:44 AM