Windows 和 Web 应用程序中使用的 BusinessLogic 库。在业务逻辑中需要知道哪个应用程序正在调用它
我有一个 Windows 应用程序和 Web 应用程序。
我有一个公共业务逻辑层,我想用它来调用一些设置。
在 Web 应用程序中,我想从 Web 配置调用设置 如果它是 Windows 应用程序,我想从注册表中调用它们。
MyApp.Windows
MyApp.WebApp
这些都调用业务逻辑应用
MyApp.BusinessLogic
有什么方法可以识别调用程序集吗?
I have a Windows App and Web App.
I have a common business logic layer that I want to use to call some settings from.
In the Web app I want to call settings from the web config
If its a Windows app I want to call them from the registry.
MyApp.Windows
MyApp.WebApp
These both call the business logic app
MyApp.BusinessLogic
Is there any way of identifying the calling assembly ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不应该在图书馆里做出这种决定。库应该通过将某种配置对象传递给构造函数或方法来获取它的设置。然后,您只需从 Windows 应用程序中的注册表和 Web 应用程序中的配置文件填充该配置对象。这样图书馆就不需要知道它是从哪里调用的。
You should not make this kind of decision in the library. The library should take it's setting via passing in some kind of config object, to a constructor or a method. THen you just populate that config object from the registry in the windows app and the config file in the web app. That way the library does not need to know where it is getting called from.
正如 Ben Robinson 指出的那样,这些设置位于 BusinessLogic 外部。业务逻辑不需要知道或关心谁调用它以及如何调用它。您可以通过依赖注入来完成此操作。您的代码可能如下所示:
我更喜欢这种方法,因为它更灵活,但您也可以使用 app.config Windows 应用程序的路由,这将使您能够从 Windows 和 Web 应用程序读取配置文件
The settings are external to the BusinessLogic, as Ben Robinson pointed. The Business Logic does not need to know, or care, who called it and how. You can accomplish this via Dependecy Injection. Your code might look something like this:
I prefer this approach, as it is more flexible, but you could also use the app.config route for Windows applications, that will enable you to read configuration files from both windows and web applications