在调试SimpleTaskSystem的AngularJs demo时,一开始我只看到对服务的应用。
app.controller(controllerId, [
'$scope', 'abp.services.tasksystem.task',
function($scope, taskService){}]);
在查找源代码中的所有js文件后还是没找到abp.services.tasksystem.task的定义,那么现在就剩下最后一种情况。这些服务是系统生成的,这样的话与动态WebApi的设计思路也是一致的。在layout.cshtml中有两处js引用
<script src="~/api/AbpServiceProxies/GetAll?type=angular"></script> <script src="~/AbpScripts/GetScripts" type="text/javascript"></script>
生成所有服务
~/api/AbpServiceProxies/GetAll?type=angular 对应的就是就是Abp对系统所有服务生成的JavaScript,现在对url进行反推我们可以在Abp.Web.Api中找到AbpServiceProxiesController,其中有一ScriptProxyManager 类型的字段_scriptProxyManager。ScriptProxyManager就是生成所有服务的一管理者。
在AbpServiceProxiesController中的GetAll方法有一参数type。这个参数表示根据什么js框架生成javascript,目前Abp提供了Angular与jQuery两种支持。
在ScriptProxyManager中会根据不同的type调用不同的IScriptProxyGenerator生成javascript代码。以Angular的实现AngularProxyGenerator为例。
public string Generate() { var script = new StringBuilder(); script.AppendLine("(function (abp, angular) {"); script.AppendLine(""); script.AppendLine(" if (!angular) {"); script.AppendLine(" return;"); script.AppendLine(" }"); script.AppendLine(" "); script.AppendLine(" var abpModule = angular.module('abp');"); script.AppendLine(" "); script.AppendLine(" abpModule.factory('abp.services." + _controllerInfo.ServiceName.Replace("/", ".") + "', ["); script.AppendLine(" '$http', function ($http) {"); script.AppendLine(" return new function () {"); foreach (var methodInfo in _controllerInfo.Actions.Values) { var actionWriter = CreateActionScriptWriter(_controllerInfo, methodInfo); script.AppendLine(" this." + methodInfo.ActionName.ToCamelCase() + " = function (" + GenerateJsMethodParameterList(methodInfo.Method) + ") {"); script.AppendLine(" return $http(angular.extend({"); script.AppendLine(" abp: true,"); script.AppendLine(" url: abp.appPath + '" + actionWriter.GetUrl() + "',"); actionWriter.WriteTo(script); script.AppendLine(" }, httpParams));"); script.AppendLine(" };"); script.AppendLine(" "); } script.AppendLine(" };"); script.AppendLine(" }"); script.AppendLine(" ]);"); script.AppendLine(); //generate all methods script.AppendLine(); script.AppendLine("})((abp || (abp = {})), (angular || undefined));"); return script.ToString(); }