ServiceStack - CSV column header (not per DataContract - DataMember Name=<value>) - servicestack

Created a Model class with DataContract and DataMember Name for each property in the class. The XML, JSON, JSV contents comes out with the Name as specified in the DataContract attribute. But CSV is not, it display the actual name of the property in the class.
Example header in XML, JSON, JSV:
FileNumber, AccountYear, AgreementDate, CollectionDate
Example header in CSV:
FILE_NO,ACCOUNT_YR,AGREEMENT_DATE,COLLECTION_DATE
I do not have any custom formatters / handlers for CSV, its all what comes with ServiceStack.
Is this how it is OR is any settings is required?
Thanks

The CSV serializer currently doesn't look at the Name property of the [DataMember] attribute.
Although if you add an issue on the ServiceStack.Text project, we can look at adding support for this in future.

Related

How to add "ref" as string name in C#

I want to name my string value 'ref' without any capital letters. Since ref is a value i can't do that.
Is there a way around this?
As you've discovered, ref is a keyword and can't be used directly in this manner. The usual advice is "don't use keywords as names". For things like deserializing JSON which has a property called "ref" you can often get around it with mapping your otherwise-named property to that JSON property with configuration or a property attribute, depending on the JSON serialization library you're using.
But if you really do need to use a keyword as a name like this, or if it's just easier than mucking around with things like serialization libraries, the language does provide a way to do that. Simply prefix the name with an #:
public string #ref { get; set; }
This approach is most commonly seen in things like Razor web pages where we need to specify client-side HTML attributes in server-side C# objects, and a common HTML attribute name is class.

How to include an XML schema (.xsd) in a JSON schema?

I want to define a JSON API response using JSON Schema.
Embedded in part of the API response is a complete, well-formed, schema valid XML string. The XSD of this XML string is a given.
Two part question:
How do I include the XSD in the JSON Schema such that the JSON Schema will also require the XML string to be schema valid in order for the whole API response to be valid?
If this is not possible, does anyone have another suggestion how to include the XSD at least in the specification? I'm working in RAML 0.8.
How do I include the XSD in the JSON Schema such that the JSON Schema
will also require the XML string to be schema valid in order for the
whole API response to be valid?
You cannot. The only thing you can do is validate the JSON and then at a later point extract the XML and validate it separately.
If this is not possible, does anyone have another suggestion how to
include the XSD at least in the specification? I'm working in RAML
0.8.
I've only used Swagger, not RAML. Swagger is also based on JsonSchema.
The only thing you can do here is to include a detailed specification that the contained XML should be compliant against such-and-such an XSD. You can do this by using the "description" functionality in swagger (or equivalent if it exists in RAML). This allows you to create a description (which supports markdown) and attach it to any element in the definition

Map to custom column names with ServiceStack OrmLite (Without Attributes)

Per title - Is it possible to map
class Test {
String SomeName {get; set;}
}
to SQL Table
tbl_test (name)
I am not interested to use attributes as I don't want to fill my POCOs with garbage.
Thanks.
Since all ServiceStack libraries use the metadata API's in ServiceStack.Text, all attributes can also be added decoupled from the model itself using the fluent API below:
typeof(Test)
.AddAttributes(new AliasAttribute("tbl_test"));
To add attributes on a property you can use the GetProperty() extension method, e.g:
typeof(Test)
.GetProperty("SomeName")
.AddAttributes(new AliasAttribute("p_some_name"));
These attributes need to be run once on Startup before they're accessed by any ServiceStack library.

Read DataMember Attribute from edmx in T4 Template

I have an application which has a server side part and a client side part. The server side part is implemented with WebApi2 and EF6 and uses Json AND Protobuf as serialisers.That's why I use [DataMember] as attribute on the properties of my models. Here I need to put the order into the Attribute [DataMember(Order = number)] because of the Protobuf serialiser.
From my serverside models (codefirst) I generate an edmx file which holds the information of the models. On the clientside I generate these models with a t4 template which uses the edmx file. Before I used Protobuf I just put the DataMember attribute on top of every property in the t4 template which worked fine. But now I have to use the same Order as on the server so that it still works with Protobuf. This means that I have to read the DataMember Attribute in the edmx and find the Order value. But I just can't read the attribute.
I tried to read the
edmProperty.TypeUsage.Facets
and some other properties on this object. I also searched on google and had a look at some sampleprojects on t4 templates. But no luck.
So how can I read the order of the attribute?
MyServerModel
{
[DataMember(Order=1)] //this number of the order is what I'm looking for
MyProperty{get;set;}
}
Now use the magic of T4 and generate following with the help of the edmx file.
MyClientModel
{
[DataMember(Order=1)] //I want to generate this Attribute with the t4 template from
//the edmx file
MyProperty{get;set;}
}
The DataMember Attribute is not part of the edmx file because every member is a DataMember attribute. And as a result of this the order information is lost.
We have solved this problem in the following way: First we generate the edmx file normaly. Afterwards we parse it manualy and look for the EntityType entries. Then we load the Type with reflection from the Assembly the code first models reside in. Parse the CustomAttributes to find the DateMemberAttribute where we can read the Order. After we have this information we can write this into the edmx file by either putting it in the documentation tag (not so elegant) or add it the Property/NavigationProperty tag as custom annotation.
After this our edmx Property tags look similar to this:
<Property Name="PropertyName" Type="Guid" xmlns:d2p8:DataMemberOrder="5" xmlns:d2p8="http://www.yourcompany.com/customAnnotation" />
In the t4 template script you have to parse edmx file again for this information and you can write the DataMember annotation with order.

Is it possible to retain custom attributes in a class instance after deserialization?

I'm trying to build a custom HTML helper for MVC.NET that would allow me to render object entities (Model Objects) as HTML forms. So I decided to do it using custom attributes such as html input type, readonly flag, css classes, etc. Similar in a way to LINQ Mapping attributes that set database related bindings for Table and Column. So I did write a custom attribute class, applied it to the same entities that I store in the database, but when I retrieve an entity class from a database to display in a View, all of my custom attributes are gone. Is there a way to retain my custom attributes, AFTER they come back from a database?

Resources