service stack angularjs with ravendb, proper approach

半世苍凉 提交于 2019-12-25 03:34:24

问题


I've created service stack angularjs vs template application. Initally I have

•   X.AngularJS
•   X.AngularJS.ServiceInterface
•   X.AngularJS.ServiceModel
•   X.AngularJS.Tests

I'm using RavenDb as datastore. On X.AngularJS there is AppHost.cs configuration object where I'm defining DocumentStore and access to database.

public override void Configure(Container container)
{
   var store = new DocumentStore()
   {
       Url = "http://...",
       DefaultDatabase = "somedb"
   }.Initialize();
   ...
}

Now, if I create Service instance (MySimpleService.cs) inside this project (X.AngularJS) everything is ok, I have access to IDocumentSession which I will use to retrieve data.

However this approach is not recommended (at least from my point of view), why would then be a X.AngularJS.ServiceModel project. I also looked at http://razor.servicestack.net/ where service is embedded in single project together with AppHost.cs.

So, I would follow this 4 project template (created out of box from vs service stack angularjs template). Having that in mind can you suggest me how to use (in most basic example) X.AngularJS.ServiceModel and X.AngularJS with ravendb database.


回答1:


Here is the approach I would suggest for using the ServiceStack ASP.NET with AngularJS template that comes installed with the ServiceStackVS plugin for Visual Studio. I'll use the project names that are used by default when generating the code using the template.

  • AngularJSWebApp1: This is the client side AngularJS project. It does have some ASP.NET code (i.e. default.cshtml, Global.asax), but these are only used for managing the layout. The AngularJS application defined in default.cshtml is a complete client side application -- the only trip it makes back to the server is for data (via the /hello URI). All communication between this project and the ServiceStack webservice projects should be initiated by the AngularJS code.

  • AngularJSWebApp1.ServiceInterface: This project defines the service (i.e. what to do once a particular request is received by the webservice). In the case of the default project, the response is a simple string. However, this is the place you would want to make calls out to the database (RavenDB in your case).

  • AngularJSWebApp1.ServiceModel: This project defines the request and response objects and maps them to particular URI routes. For example, when a request comes in to the URI /hello/user1765862, ServiceStack will instantiate a new Hello class and set the Name property equal to "user1765862". This Hello object instance will then be passed to the AngularJSWebApp1.ServiceInterface.MyServices.Any() method.

  • AngularJSWebApp1.Tests: The project demonstrates how to unit test your service.

Notice the dependency chain. AngularJSWebApp1.ServiceModel is a dependency of AngularJSWebApp1.ServiceInterface. The later needs an instance of a class defined in the former in order to perform operations against the request object. Both AngularJSWebApp1.ServiceModel and AngularJSWebApp1.ServiceInterface are dependencies of AngularJSWebApp1. Since this is the only ASP.NET project, it is being used to host both the AngularJS application and the ServiceStack webservice. Notice the <httpHandlers> section in the Web.config file.



来源:https://stackoverflow.com/questions/26888838/service-stack-angularjs-with-ravendb-proper-approach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!