WPF:How to Tell my DataLayer which DataContext to use?
I have a solution where i have 3 WPF projects under it (Project UI-A, Project UI-B and Project named CommonLibrary(Data Layer).I have a user registration form in the Project UI-A where i will capture the User profile information. And in the CommonLibrary project i have a class called UserCommon
which will have a method to save the data to the db table. I am using LINQ 2 SQL to talk to the db in CommonLibrary
project. In the Project UI-A i will have a dropdonw list which has options like Development
and Production
Using which i can choose where this operation to be done.
Now how can i tell the method in CommonLibrary to use which datacontext (Development or Production)? Is passing a variable开发者_如何学编程 (to mention its development or production) in all the methods as a parameter is the only way? What are the other alternatives?
I'd suggest putting the setting in your app.config file, and access it through the ConfigurationManager. What you don't want is a variable set in code.
Another option is to use a dependency injection tool that builds its configuration from an external file. However, that's usually not the way people want to configure dependency injection. (That's much cleaner in code.)
You can also build a factory method that generates DataContext objects, pre-configured for the correct configuration. That isolates the decision to a single method.
Sounds a little bit like a Strategy Pattern problem: http://www.dofactory.com/Patterns/PatternStrategy.aspx#_self2
精彩评论