lundi 28 novembre 2016

ASP.NET OData, route typeless/untyped entities to same controller

I am using the ASP.NET OData API, version 6.0 I am creating the EDM model using typeless objects. Reason is that I have a semantic data model in my database, crud operations on any object type is done using the same piece of code. So I essentially just need to make one controller which parses the OData query string, and apply it to my native way of loading objects. I have all this working, almost. Whats missing to defined the routing dynamic in some way....

So how can I map entity x,y and z to use the same controller??

My code for building the model looks similar to below. This will route to controllers named IdentitiesController, ResourcesController, SystemsController and OrgunitsController. But they should all route to the same controller "NativeObjectsController"....

The below is just a set of the possible entity types... and the enduser can create his own types, so I cannot just make specialized controllers...

public static void Register(HttpConfiguration config)
{
  IList<IODataRoutingConvention> routingConventions = ODataRoutingConventions.CreateDefault();
  IODataPathHandler pathHandler = new DefaultODataPathHandler();

  config.MapODataServiceRoute(
    routeName: "ODataRoute",
    routePrefix: "odata/dataobjects",
    model: GetEntityModel()
   // ,pathHandler: pathHandler,
    //routingConventions: routingConventions
  );

  var routes = config.Routes;
  config.AddODataQueryFilter();
  config.EnsureInitialized();
}

public static EdmModel GetEntityModel()
{
  if (model != null)
    return model;

  model = new EdmModel();
  EdmEntityContainer container = new EdmEntityContainer(ns_ois, "OISContainer");
  model.AddElement(container);

  AddDataObjectTypeToModel(model, container, "IDENTITY", "Identities");
  AddDataObjectTypeToModel(model, container, "RESOURCE", "Resources");
  AddDataObjectTypeToModel(model, container, "SYSTEM", "Systems");
  AddDataObjectTypeToModel(model, container, "ORGUNIT", "OrgUnits");

  return model;
}

private static void AddDataObjectTypeToModel(EdmModel model, EdmEntityContainer container, string dataObjectSystemName, string entitySetName)
{
  DataObjectTypeController dotController = Factory.Default.CreateController<DataObjectTypeController>();
  int dotId = dotController.GetId(dataObjectSystemName);
  DataObjectType dot = dotController.GetDataObjectType(dotId);

  var entityObject = new EdmEntityType(ns_ois, dataObjectSystemName);
  model.AddElement(entityObject);
  entityObject.AddKeys(entityObject.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, false));
  entityObject.AddStructuralProperty("UId", EdmPrimitiveTypeKind.Guid, false);
  entityObject.AddStructuralProperty("DisplayName", EdmPrimitiveTypeKind.String, false);

  foreach (var property in dot.GetProperties())
  {
  }

  var entitySet = container.AddEntitySet(entitySetName, entityObject);
}




Aucun commentaire:

Enregistrer un commentaire