ServiceStack 应该是 MVC 应用程序中的服务层还是应该调用服务层? [英] Should ServiceStack be the service layer in an MVC application or should it call the service layer?

查看:36
本文介绍了ServiceStack 应该是 MVC 应用程序中的服务层还是应该调用服务层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 MVC 网站,并且还打算创建一个 Web API,以便在网站内使用,也可能供第三方使用.

I'm creating an MVC website and also intend to create a web API for use both within the website and potentially by third parties.

从 MVC 控制器中,我将调用一个服务层,该层将包含业务逻辑、对域模型进行操作、执行验证、进行基础设施外部服务调用等.服务层又将调用存储库以进行任何数据库交互.

From the MVC controllers I'll be calling into a service layer which will contain business logic, act on domain models, perform validation, make infrastructure external service calls etc. The service layer in turn will call into repositories for any database interactions.

现在,我喜欢 ServiceStack 的外观并打算将它用于 Web API - 它似乎比 ASP.NET MVC 4 Web API.我的问题是,我应该让 ServiceStack API 调用到我上面的服务层,类似于 MVC 控制器,还是应该让它成为服务层,为 Web 客户端和 MVC 控制器提供服务?

Now, I like the look of ServiceStack and intend on using it for the Web API - it seems more mature than the ASP.NET MVC 4 Web API. My question is, should I have the ServiceStack API call into my service layer above, similar to the MVC controllers, or should I make it the service layer, servicing both web clients and the MVC controllers?

推荐答案

我都不会.

理想情况下,MVC 和 ServiceStack 都应该使用并共享纯 C# 依赖项.MVC + ServiceStack 网站和谐相处的一个很好的例子是 SocialBootstrapApi 演示项目,该项目已部署在 AppHarbor 上:http://bootstrapapi.apphb.com

Ideally both MVC and ServiceStack should use and share pure C# dependencies. A good example of an MVC + ServiceStack website living harmoniously together is in the SocialBootstrapApi demo project, which has been deployed on AppHarbor at: http://bootstrapapi.apphb.com

我会在您的 ServiceStack AppHost 中注册您的所有依赖项,然后注册一个 MVC 控制器工厂,因此您的 MVC 控制器和 ServiceStack 服务都会自动连接这些依赖项.

I would register all your dependencies in your ServiceStack AppHost then register an MVC Controller factory so both your MVC Controllers and ServiceStack services get auto-wired with these dependencies.

在您的 AppHost 中:

In your AppHost:

void Configure(Funq.Container container) {
    container.Register<IGreeter>(c => new Greeter());
    //Set MVC to use the same Funq IOC as ServiceStack
    ControllerBuilder.Current.SetControllerFactory(
       new FunqControllerFactory(container));
}

使用IGreeter

public class HelloService : Service {
    public IGreeter Greeter { get; set; } //Autowired

    public HelloResponse Get(Hello request) {
        return new HelloResponse { 
           Result = Greeter.SayHelloTo(request.Name) };
    }
}

MVC 控制器使用同一个 IGreeter 的例子:

Example of MVC Controller using same IGreeter:

public HelloController : ServiceStackController {
    public IGreeter Greeter { get; set; } //Autowired

    public void Index(string name) {
       ViewBag.GreetResult = Greeter.SayHelloTo(name);
       return View();
    }        
}

一般的想法是MVC控制器和ServiceStack服务内部的逻辑应该关注HTTP层/集成点,即从QueryString或FORM POST'ed变量中收集用户输入并用它调用纯/可测试的C#逻辑然后准备响应,在 ServiceStack 中将填充响应 DTO,而对于 MVC 控制器,您将填充 ViewModel.

The general idea is for logic inside MVC Controllers and ServiceStack services should be concerned with the HTTP layer/integration point i.e. collecting the user input from the QueryString or FORM POST'ed variables and calling pure/testable C# logic with it then preparing the Response, in ServiceStack that would be populating the Response DTO whilst for an MVC Controller you would be populating the ViewModel.

虽然我会通过上面的 C#greet 服务实现 Controllers + ServiceStack 共享功能,但您也可以从 MVC Controller 调用 ServiceStack 服务,例如:

Although I would have Controllers + ServiceStack share functionality via a C# greet service above, you also can call a ServiceStack service from an MVC Controller like:

public HelloController : ServiceStackController {

  public void Index(string name) 
  {
    using (var helloService = AppHostBase.ResolveService<HelloService>())
    {
       ViewBag.GreetResult = helloService.Get(name).Result;
       return View();
    }
  }        
}

与 ServiceStackController 共享会话/缓存

尽管 MVC 控制器示例继承自 ServiceStackController,这不是必需的,但确实允许您在 MVC 和 ServiceStack 中共享相同的 Session/Caching/Au​​thentication+RequiredRole/RequiredPermission 属性.

Share Session/Caching with the ServiceStackController

Although the MVC Controller examples inherit from the ServiceStackController, it's not necessary but does allow you to share the same Session / Caching / Authentication + RequiredRole/RequiredPermission attributes in MVC and ServiceStack.

查看 MVC PowerPack 了解 ServiceStack 给 MVC 带来的其他好处.

See the MVC PowerPack for other benefits that ServiceStack brings to MVC.

这篇关于ServiceStack 应该是 MVC 应用程序中的服务层还是应该调用服务层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆