Last post May 03, 2013 05:44 AM by Illeris
May 02, 2013 10:52 AM|PrashanthReddy|LINK
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.
May 02, 2013 11:01 AM|AidyF|LINK
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.
May 02, 2013 11:06 AM|Careed|LINK
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.
May 02, 2013 05:16 PM|Illeris|LINK
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
May 03, 2013 02:44 AM|PrashanthReddy|LINK
Thanks to every one,
But we must have to different implementations, one is for in premise( MS SQL) and other is for Sql Azure.
May 03, 2013 05:44 AM|Illeris|LINK
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)