MDrivenServer - 如何将视图模型公开为 REST 服务

MDrivenServer - how to expose viewmodel as a REST service

我使用独立的 MDrivenServer。 问题 - 是否可以在不安装 MDrivenTurnkey 的情况下在此配置中将视图模型公开为 REST 服务,例如使用 url 比如 ...Rest/Get?command=vmname&id=rootobjref ? 谢谢!

不,MDrivenServer 仅公开了持久性映射器 api。为了模拟视图模型驱动的休息获取服务,您可以在 MVC 应用程序中这样做:

Derive your controller from ModelDrivenControllerBase<ESType>

/// <summary>
/// command should match a viewmodel, id should be external id for root or $null$
/// will try and match extra params to viewmodel variables
/// will execute actions on root level
/// will return complete vm on success as json except if string attribute RawJSon is found - then this is returned instead
/// </summary>
public virtual ActionResult Get(string command, string id)
{
  Eco.ObjectRepresentation.IModifiableVariableList vars = FindAdditionalRequestParamsAndTreatAsVars();
  if (string.IsNullOrEmpty(id))
    id = ObjectId.nulltoken;
  SaveVariablesToSessionState(command, id, vars);


    VMClass onlinevm = CreateVMClassFromName(command, id);
    if (onlinevm != null)
    {
      if (!CheckRestAllowed(onlinevm))       <-- turnkey checks the rest allowed flag 
        return Content("Must set RestAllowed on VM " + command);

      foreach (var col in onlinevm.ViewModelClass.Columns)
      {
        if (col.IsAction)
        {
          col.ViewModel.ExecuteAction(col.ViewModelClass.RuntimeName, col.RuntimeName);
        }
      }
      return GetJsonFromVm(onlinevm);   <-- this can be implemented with the Tajson concept: https://wiki.mdriven.net/index.php/Tajson
    }
    else
      return Content("Access denied");
}



/// <summary>
/// targetViewRootObject may be both string and IEcoObject. 
/// If the newrootAsObject happens to be a guid string - and the IClass has a property guid - then we will try and PS-resolve the guid and use it as root 
/// </summary>
protected VMClass CreateVMClassFromName(string targetViewName, object targetViewRootObject)
{
  EnsureEcoSpace();

  if (targetViewRootObject is string)
    targetViewRootObject = SafeObjectForId((string)targetViewRootObject);

  VMClass createdVMClass = ViewModelHelper.CreateFromViewModel(targetViewName, EcoSpace, targetViewRootObject as IEcoObject, false);
  IEcoObject root = targetViewRootObject as IEcoObject;
  if (root == null && createdVMClass.ViewModelClass.IClass is IClass && targetViewRootObject is string)
  {
    // Handle special case of a GUID as target
    // Used to act on direct links for an object
    if (targetViewRootObject is string)
    {
      root = EcoSpace.ExternalIds.ObjectForUnkownId(targetViewRootObject as string, createdVMClass.ViewModelClass.IClass as IClass);
      if (root != null)
        createdVMClass.Content = root.AsIObject();
    }

  }
  bool vm_visibleDueToAccessGroups = createdVMClass.ViewModelClass.ViewModel.VisibleDueToAccessGroups(EcoSpace, root != null ? root.AsIObject() : null);
  if (!vm_visibleDueToAccessGroups)
  {
    return null;
  }
  LoadVariablesFromSessionStateIfAvailable(targetViewName, SafeIDForObject(targetViewRootObject), createdVMClass.Variables);
  return createdVMClass;
}