Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

c# - service stack angularjs with ravendb, proper approach

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...