ASP.NET MVC - 一个业务服务需要调用另一服务

发布于 2024-10-16 18:52:43 字数 437 浏览 3 评论 0原文

我有一个 RefData 服务(业务服务),它从数据库加载所有参考数据并缓存在内存中(自定义缓存服务器)。我的许多控制器在需要这些数据时都会调用此服务。

我还有一个定价服务(业务服务),它连接到外部 Web 服务以获取产品定价,一旦获得所有价格,它就必须从数据库获取一些内容数据并与定价响应合并,然后再将最终响应移交给控制器。现在大部分数据例程已经由 RefData 服务公开。我不想在定价服务中编写重复的方法来获取由参考数据服务公开和缓存的相同数据。另外,我无法将这些例程移至定价服务中,因为我的控制器也需要这些数据,并且它不属于定价服务...

我的情况是,我的定价服务需要调用 RefData 服务来获取一些数据,然后才能使用将响应移交给控制器。我找不到任何一项业务服务调用另一项服务的文章或示例。

我使用 ASP.NET MVC 2 和 Unity 作为我的 IOC。我很感激您对我应该采取什么方法的建议。

I have a RefData Service (Business Service) which loads all referance data from DB and caches in memory (Custom Cache Server). Many of my controllers call this service whenever they need this data.

I also have a Pricing Service (Business Service) which connects to external WebService to get Product pricing, once it gets all prices it has to get some content data from DB and merge with pricing response before it hands over final response to controllers. Now most of this data routines are already exposed by the RefData service. I don't want to write duplicate methods in my Pricing Service to get same data which is exposed and cached by Ref Data Service. Also I cannot move these routines into Pricing Service because my controllers also need this data and it does not belong in Pricing Service...

I am kind of in a situation where my Pricing service needs to call RefData service to get some data before it can hand over the response to Controllers. I couldn't find any write ups or samples where One Business Service calls another Service.

I am using ASP.NET MVC 2 and Unity as my IOC. I would appreciate your suggestions of what approach should I take.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

有木有妳兜一样 2024-10-23 18:52:43

在我的应用程序开始时,我使用 IOC 容器注册所有服务。当一个需要调用另一个时,我只需解析我需要的并调用它即可。

也许我遗漏了你的问题?

在 Global.asax.cs 中:

        var authorizationRepository = new AuthorizationRepository(session);
        IoC.Container.RegisterInstance<IAuthorizationRepository>(authorizationRepository);
        var permissionBuilderService = new PermissionsBuilderService(session, authorizationRepository);
        IoC.Container.RegisterInstance<IPermissionsBuilderService>(permissionBuilderService);

当我需要服务时:

        var repository = IoC.Container.Resolve<IAuthorizationRepository>();
        repository.DoSomething();

At the beginning of my app I register all my services with my IOC container. When one needs to call another, I simply resolve the one I need and call it.

Perhaps I am missing something about your qestion?

In Global.asax.cs:

        var authorizationRepository = new AuthorizationRepository(session);
        IoC.Container.RegisterInstance<IAuthorizationRepository>(authorizationRepository);
        var permissionBuilderService = new PermissionsBuilderService(session, authorizationRepository);
        IoC.Container.RegisterInstance<IPermissionsBuilderService>(permissionBuilderService);

When I need a service:

        var repository = IoC.Container.Resolve<IAuthorizationRepository>();
        repository.DoSomething();
羁拥 2024-10-23 18:52:43

一项服务需要另一项服务的情况很常见。一个例子是授权服务。如果您发现所有其他服务都需要某项服务,也许是时候创建一个可以继承的基础服务了?

It's pretty common for one service to need another service. One example would be an authorization service. If you find a service is required in all other services, maybe it's time to make a base service that you can inherit from?

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