1

Although I am able to access the SchemaVersion using code below, I cannot access FormatDocID nested element.

Any ideas how can I easily get FormatDocID using ServiceStack and AutoQueryFeature (or similar)?

I put only relevant parts of code here

public override void Configure(Container container)
{
    JsConfig.DateHandler = DateHandler.ISO8601;
    SetupValidators(container);
    SetupIOC(container);
    SetupPlugins(container, log);
    ContentTypes.Register("application/xml"
    , CLXmlSerializer.Serialize, ServiceStack.Text.XmlSerializer.DeserializeFromStream);
    SetupMetaDataRedirectionPath();
    SetupGlobalResponseFilters();
}

Setup plugins

private void SetupPlugins(Container container)
{
    Plugins.Add(new ValidationFeature());
    Plugins.Add(new SwaggerFeature());
    Plugins.Add(new AutoQueryFeature
    {
        MaxLimit = 1000,
        EnableUntypedQueries = false,
        IncludeTotal = true
    });

    Plugins.Add(new AutoQueryDataFeature {MaxLimit = 100}
        .AddDataSource(ctx => ctx.MemorySource(new List<WordDocument>
        {
            new WordDocument()
            {
                SchemaVersion = "",
                Format = new Word.DocumentFormat()
                {
                    FormatDocID = 254
                }
            }
        }))
        );

    typeof(RequestLogs).AddAttributes(new RestrictAttribute {VisibilityTo = RequestAttributes.None});
    typeof(AssignRoles).AddAttributes(new RestrictAttribute {VisibilityTo = RequestAttributes.None});
    typeof(UnAssignRoles).AddAttributes(new RestrictAttribute {VisibilityTo = RequestAttributes.None});
    typeof(Authenticate).AddAttributes(new RestrictAttribute {VisibilityTo = RequestAttributes.None});
}

Serializable classes

public abstract class Document
    {
        public DocumentFormat Format;

        public class DocumentFormat
        {
            [XmlAttribute] public int Version;

            public int FormatDocID;
            public string DocShortName;
        }
    }


public class WordDocument : Document
{
    [XmlAttribute] public string SchemaVersion { get; set; } = "1.0";
}

Thanks in advance for the answers.

essential
  • 648
  • 1
  • 7
  • 19

1 Answers1

1

It's not clear what you're trying to achieve or why, AutoQuery creates Auto Queryable APIs where the Response is the API Response serialized in the specified Response Content Type.

If you want to intercept the Typed Response DTO before it's returned you can create a Custom AutoQuery Implementation and introspect the response that way, e.g:

public class MyQueryServices : Service
{
    public IAutoQueryData AutoQuery { get; set; }

    //Override with custom implementation
    public object Any(MyQuery query)
    {
        var q = AutoQuery.CreateQuery(query, base.Request);
        var response = AutoQuery.Execute(query, q);
        return response;
    }
}

But the AutoQuery Memory Data Source you're using lets you provide your own collection of Typed POCOs as the Data source so you already have access to them when you create it, but the source POCOs should be a flat Type with public properties (in contrast to your class with public fields and nested types) - it's not possible to query nested object graph values.

This is an example of a POCO that doesn't use nested classes, or public fields:

public abstract class Document
{
    public int Version { get; set; }
    public int FormatDocID { get; set; }
    public string DocShortName { get; set; }
}

So the solution if you want to use AutoQuery would be to change your Data Source to use Flat POCOs with public properties otherwise you'd need to create the impl of your Service yourself.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • Thank you mythz. I'm new in Servicestack. Could you please provide example with my code? – essential Feb 26 '19 at 06:13
  • @essential not sure what you want an example of but I've converted your `Document` into a flat POCO with public properties. – mythz Feb 26 '19 at 07:09
  • Thanks, but need to use the nested structure. If you could provide that one, I'd be really grateful. – essential Feb 26 '19 at 07:22
  • @essential then as per my answer you can't use AutoQuery – mythz Feb 26 '19 at 07:26
  • 1
    @essential It's the same thing, it's all AutoQuery - you can't use AutoQuery and you'll need to implement whatever service + schema you want yourself. – mythz Feb 26 '19 at 07:37