2
votes

I love Servicestack and have bought the commercial version. I need to provide Rest services based on fully dynamic data that is defined in metadata (in some data store). I therefore know the structure of the data and datatypes but not via a POCO, and in some situations the data (E.g. external web service) will be explored by a user who will then confirm data types, format strings etc. Users can expand and configure the metadata via a front end. I want to be able to use the benefits of servicestack but without providing POCO's because then I must recompile and redeploy if changes happen.

Is there no way that one can provide servicestack with the structure to be used dynamically from Metadata without doing it while coding using POCO's.

Even if someone can point me to someplace where I can override it to pass in the POCO structure.

1
Have you tried using loose-typed collections, e.g. List<string>, Dictionary<string,string>? - mythz
I am doing Dictionary<string,object> but need to hint to serializer/deserializer what type it is to serialize/deserialize consistently over different data sources based on my metadata. I don't want the _type property to bloat the json results though by having _type as part of the json. Using dictionaries as I need to use bracket notation as I get the field names from metadata. I can not use Person.Name...have to do result["Person.Name"]. So I have dynamic data but from metadata..not code - Andy
Any info on what you finally used for this case? I have the same requirement and I am thinking of using something like Dictionary<string, object> (hoping that it will work...). - user2173353

1 Answers

0
votes

Code-first Services Framework

ServiceStack is predominantly a code-first Services framework with many of its features centered around your code-first POCO's. Without a well-defined POCO to define your Service there's no source definition for ServiceStack's metadata services to refer to, or concrete type to deserialize into, etc.

Returning Custom JSON or loose-typed .NET Collections

Depending on what you're trying to achieve with dynamic untyped data, your Service implementation could return a custom JSON string serialized with the desired Service Response. To help with generating JSON you can return an untyped collection of List<T> and Dictionary<string,T> to match the shape of the JSON you want to return.

Although not having a typed model loses many of the benefit of using well-defined DTO's, where clients will no longer have a typed API to call or consume your Services with.

Automatically generating Types and dynamically registering Services

An advanced option would be to use code-generation to dynamically generate Types and Service implementations.

A similar approach is taken with the implementation of AutoQuery which uses Reflection.Emit to dynamically generate missing Service implementations for AutoQuery Request DTO's.

After code-generating a new Service implementation it can be registered with ServiceStack using the IAppHost.RegisterService() API:

var serviceType = GenerateMissingServices(misingRequestTypes);
appHost.RegisterService(serviceType);

This approach works well for AutoQuery as it uses the Request DTO as a blue-print to generate the Service implementation from, so clients still benefit from having a typed Request and Generic Response DTO to call AutoQuery Services without needing any code-gen.

You could also use this approach to dynamically generate the Service Models (DTOs) as well, but as there's no concrete type available, clients would need to use ServiceStack's Add ServiceStack Reference feature to generate the typed DTO's for their preferred language.