WPF:如何告诉我的 DataLayer 使用哪个 DataContext?
我有一个解决方案,其中有 3 个 WPF 项目(项目 UI-A、项目 UI-B 和名为 CommonLibrary(数据层)的项目)。我在项目 UI-A 中有一个用户注册表单,我将在其中捕获用户配置文件在 CommonLibrary 项目中,我有一个名为 UserCommon 的类,它有一个将数据保存到数据库表的方法,我正在使用 LINQ 2 SQL 与 CommonLibrary 中的数据库进行通信。在项目 UI-A 中,我将有一个下拉列表,其中包含诸如 Development
和 Production
之类的选项,我可以使用它们选择要在哪里完成此操作。现在
我如何告诉 CommonLibrary 中的方法使用哪个数据上下文(开发或生产)正在传递变量(提及其开发或生产)? 在所有方法中作为参数是唯一的方法吗?还有哪些其他选择?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议将设置放入 app.config 文件中,并通过 ConfigurationManager 访问它。您不想要的是在代码中设置的变量。
另一种选择是使用依赖项注入工具从外部文件构建其配置。然而,这通常不是人们想要配置依赖注入的方式。 (这在代码中更加简洁。)
您还可以构建一个生成 DataContext 对象的工厂方法,并预先配置为正确的配置。这将决策隔离为单一方法。
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.
听起来有点像策略模式问题: http://www.dofactory.com/模式/PatternStrategy.aspx#_self2
Sounds a little bit like a Strategy Pattern problem: http://www.dofactory.com/Patterns/PatternStrategy.aspx#_self2