Windows 和 Web 应用程序中使用的 BusinessLogic 库。在业务逻辑中需要知道哪个应用程序正在调用它

发布于 2024-12-02 18:16:51 字数 303 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑忘罢 2024-12-09 18:16:51

你不应该在图书馆里做出这种决定。库应该通过将某种配置对象传递给构造函数或方法来获取它的设置。然后,您只需从 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.

等风也等你 2024-12-09 18:16:51

正如 Ben Robinson 指出的那样,这些设置位于 BusinessLogic 外部。业务逻辑不需要知道或关心谁调用它以及如何调用它。您可以通过依赖注入来完成此操作。您的代码可能如下所示:

//in MyApp.BusinessLogic
public class MyBusinessObject
{

   private IMyConfiguration _configuration;

   public MyBusinessObject(IMyConfiguration configuration)
   {
     _configuration = configuration;
   }

   //.. code that uses the configuration to do what's needed
}

//in MyApp.Entities (any project that is visible anywhere)
public interface IMyConfiguration 
{
   //whatever configurable properties are needed
}

//in MyApp.Windows
public class MyRegistryConfiguration : IMyConfiguration 
{
  //class that loads settings from the registry
}


// somewhere in the code
IMyConfiguration configuration = new MyRegistryConfiguration ();
MyBusinessObject business = new MyBusinessObject(configuration);
// use MyBusinessObject to do businessy things

我更喜欢这种方法,因为它更灵活,但您也可以使用 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:

//in MyApp.BusinessLogic
public class MyBusinessObject
{

   private IMyConfiguration _configuration;

   public MyBusinessObject(IMyConfiguration configuration)
   {
     _configuration = configuration;
   }

   //.. code that uses the configuration to do what's needed
}

//in MyApp.Entities (any project that is visible anywhere)
public interface IMyConfiguration 
{
   //whatever configurable properties are needed
}

//in MyApp.Windows
public class MyRegistryConfiguration : IMyConfiguration 
{
  //class that loads settings from the registry
}


// somewhere in the code
IMyConfiguration configuration = new MyRegistryConfiguration ();
MyBusinessObject business = new MyBusinessObject(configuration);
// use MyBusinessObject to do businessy things

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文