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

查看:209
本文介绍了应该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 - 它似乎比<一个比较成熟的HREF =htt​​p://www.asp.net/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然后注册一个<一个href=\"https://github.com/ServiceStack/SocialBootstrapApi/blob/master/src/SocialBootstrapApi/AppHost.cs#L134\">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.

在您A​​PPHOST:

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));
}

使用ServiceStack服务的示例 IGreeter

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

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

使用相同的IGreeter MVC控制器的示例:

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层/集成点担心,即从查询字符串收集用户输入或FORM POST'ed变量和调用纯/测试的C#的逻辑与它那么preparing响应,在ServiceStack,将被填充响应DTO而对于一个MVC控制器,你会被填充视图模型。

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#迎接服务控制器+ ServiceStack共享功能上面,你也可以打电话从MVC控制器中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控制器的例子来自继承<一个href=\"https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.FluentValidation.Mvc3/Mvc/ServiceStackController.cs#L51\">ServiceStackController,这不是必要的,但确实让你共享同一会话/缓存/认证+ RequiredRole / RequiredPermission属性在MVC和ServiceStack。

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天全站免登陆