[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 707
  • Last Modified:

Change data source using MVC 3 Repository Pattern

I have been looking into the Repository pattern of coding data access for an MVC application. As I understand it, it allows me to change the database/data storage easily, but all the examples I've seen don't show how this is achieved.

I have my interface with the CRUD operations, I have the repository that implements the interface, and I have the data context set to a SQLce database.

This all works, but I want to be able to give the option to use a SQL Express database instead of the Compact Edition. How can I do that in my MVC app?

I would assume that I create another data context and use that in a new repository class the implements my interface, but how do I tell the MVC application to use this instead of the original via something like a web.config key?
0
strickdd
Asked:
strickdd
  • 3
  • 3
1 Solution
 
ToddBeaulieuCommented:
Yes, you're really close.

Keep in mind that there are different approaches and you're always free to implement solutions that seem to make sense to you, even if it's not exactly how others do it.

For instance, I've settled on making a single service that the application (controller) talks to. No interface needed, since there is only one.

Then I make an xml repository for early development. It implements a repository interface that the sql repository - when created later - will also implement.

Each repository is isolated, obtaining it's data however appropriate. I make each a separate project in the solution.  In my case, I can simply hardcode which repository is used because I work with one all the way till I'm ready for release. I've also used dependency injection to do this, which lets you code the service to gets an object that implments IRepository. The injector then decides at runtime which to inject. The logic that it uses is yours, so you can again, either hard code it or look it up in a config file, etc.
0
 
strickddAuthor Commented:
I was hoping to have a way to have a user install the webapplication and select which data source they wanted to use. Some people don't want to use a full SQL Express server and would prefer the SQLce version, so I want to give them the choice.

Is there an easy way to do this? I am willing to change the setup of my application if needed.
0
 
ToddBeaulieuCommented:
Oh, well if you're not looking to swap out the repository, but rather just change the datasource that it points to, all you need to do is put the connection string in the config file so the user can change it. Of course, you could also add a config "screen" to let them enter the string and have it store it back in the config file. Have the respository read the string and open the connection.
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
strickddAuthor Commented:
Would that work for swapping between SQL Express, SQLce, MySQL, and XML datasource options?
0
 
ToddBeaulieuCommented:
Well, if you used ANSI SQL (never using propietary statements or constructs) you could support any data source that is ansi compliant. Of course, there are different levels of ANSI, so you'd pick the most recent you could get away with that's common to the platforms you want to support.

Writing ANSI compliant SQL isn't hard, but it's not as easy as going to town with whatever your favorite vendor has rolled into the latest and greatest.

As for XML ... to be honest, I don't know if there is a way to use XML as if it were SQL. What I mean, is ... maybe there's a way to connect to an XML file and have .net transform it into ado.net data structures.

But ... if you're using stored procs and UDFs that wouldn't work anyway. When I use XML I use a separate repository because it access and persists the data differently than the SQL repository and database do. I'm not calling stored procs for updates, for instance.

You could write a repository for each supported platform (if needed ... if you can't get away with a common sql version), including XML and then let the user choose the TYPE and then have them also specify the connection string (for sql) or the filename (for xml). In your code, you'd have a "repository factory". A fancy name for a class that you call to get an IRepository instance and it would probably just check the selected type in a switch block, instanatiating the appropriate repository and returning it.

The service would not need to change. It would call into whatever repository it was given and let the repository do its magic as a black box.
0
 
strickddAuthor Commented:
Yes
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now