-
Notifications
You must be signed in to change notification settings - Fork 6
Getting Started
sharp2Js
is designed to ease the transition of large/complex DTOs from C# to javascript.
With sharp2Js
, you tell the library which types you want javascript object representations of, and then sharp2Js uses reflection and the configuration options you specify to build a javascript string that can be added to your client code.
The easiest way to do that is with a T4
template in your server libraries.
sharp2Js
can be installed via the nuget UI (as sharp2Js), or via the nuget package manager console:
PM> Install-Package sharp2Js
Installing sharp2Js will install the required Dlls as well as a sample T4 template to get you started. By default, it's configured to use default values, but these are easily changed.
Let's take a look at configuring the options for sharp2Js.
var options = new Castle.Sharp2Js.JsGeneratorOptions() {
CamelCase = false,
OutputNamespace = "models",
IncludeMergeFunction = true,
ClassNameConstantsToRemove = new List<string>() { "Dto" },
RespectDataMemberAttribute = true,
RespectDefaultValueAttribute = true,
TreatEnumsAsStrings = false,
CustomFunctionProcessors =
new List<Action<StringBuilder, IEnumerable<Castle.Sharp2Js.PropertyBag>, Castle.Sharp2Js.JsGeneratorOptions>>()
{
(builder, bags, arg3) =>
{
builder.AppendLine("\tthis.helloWorld = function () {");
builder.AppendLine("\t\tconsole.log('hello');");
builder.AppendLine("\t}");
}
}
};
var str = Castle.Sharp2Js.JsGenerator.Generate(new [] { typeof(Castle.Sharp2Js.SampleData.AddressInformation) }, options);
These options should allow sharp2Js with most JSON serializer conventions.
-
Camel casing - if
true
, enables camel-casing of the property names generated by sharp2Js. This is useful for working with serializer libraries configured to camel-case JSON property names. -
OutputNamespace - a
string
that is prefixed to all generated objects to namespace them/remove them from the global scope. -
IncludeMergeFunction - if
true
, includes a function that will merge two instances of the javascript object. This is often useful for working with CRUD views where the objects are being updated, and is a convenience function only. -
ClassNameConstantsToRemove - a
List<string>
that contains reserved keywords to remove from object names during generation. As an example, its common to label C# types used as DTOs with "Dto" (e.g.AddressInformationDto
). That designation is redundant on the client-side, so you may wish to have it omitted from the name when the object is generated (e.g.AddressInformation
). -
RespectDataMemberAttribute - if
true
, property names marked with the[DataMember]
attribute that use theName
property will be respected, and the generated property will be named the value of theName
property. Additionally, any members marked with the[IgnoreDataMember]
attribute will not be generated. -
RespectDefaultValueAttribute - if
true
, the generator will set the default value of primitive types to the value specified in the[DefaultValue]
attribute for a property. -
TreatEnumsAsStrings - if
true
, enum values will be generated asstring
instead ofint
. This is to respect the behavior of some serializers that treat enums differently. E.g., JSON.Net treats enums asint
by default. Jil, on the other hand, treats enums asstring
by default. -
CustomFunctionProcessors - is a
List<Action<StringBuilder, IEnumerable<PropertyBag>, JsGeneratorOptions>>
that can be passed to the generator in order to insert custom functions per type. At the end of generation of each type, before the closing tag is written, the delegates in this list will be called, allowing you to inject functions with custom behavior as necessary.
In the T4
, calling:
var str = Castle.Sharp2Js.JsGenerator.Generate(new [] { typeof(Castle.Sharp2Js.SampleData.AddressInformation) }, options);
will build a string that can then be written to the output.