Skip to content
axunonb edited this page Feb 6, 2022 · 1 revision

Data Sources

Data type What does it look for? Examples
Any object Properties, Fields, or parameterless methods Smart.Format("{Prop.ToString}", new{ Prop = 999 })
Dictionary Keys Smart.Format("{SomeKey}", new Dictionary<string, object>(){ { "SomeKey", 999 } } )
Dynamics Works just like Dictionary Smart.Format("{Prop}", (dynamic)(new { Prop = 999 }))
JObject
(Newtonsoft JSON)
Finds child elements by name Smart.Format("{Name}", JObject.Parse("{ 'Name':'John'}"))
JsonElement
(System.Text.Json)
Finds child elements by name Smart.Format("{Name}", JsonDocument.Parse("{ \"Name\":\"John\"}").RootElement)
ValueTuple Find elements of objects in ValueTuple by their member name Smart.Format("{Name1} {Name1}", (obj1, obj2));
XML Finds child elements by name Smart.Format("{NodeName}", someXElement)
Default Looks up arguments by index Smart.Format("{0}", 999)

JSON

JSON also comes in handy when processing data in a web API application where the argument submitted from the browser to the controller is JSON. Another scenario is working with queries from SQL Server:

SELECT 'John' AS [FirstName], 'Doe' AS [LastName], 32 AS [Age]
FOR JSON PATH, ROOT('Father')

You can parse the query result into a JObject (Newtonsoft.Json) or JsonElement (System.Text.Json) and give it to SmartFormat as an argument. JObject and JElement may contain arrays for the ListFormatter.

ValueTuples

Note:

  • The examples below use Dictionaries for simplicity. In the same manner other types than Dictionaries can be used as data sources.
  • Use ValueTuples (introduced in v2.5) instead of SmartObjects because they are less expensive.

With ValueTuples

  • all objects used for Smart.Format can be collected in one place as the first argument
  • the format string can be written like each object would be the first argument of Smart.Format
  • there is no need to bother from which argument a value should come from
  • child ValueTuples will be flattened

Example:

var dict1 = new Dictionary<string, string> { {"dict1key", "dict1 Value"} };
var dict2 = new Dictionary<string, string> { { "dict2key", "dict2 Value" } };

Smart.Format("[{dict1key}] [{dict2key}] and more...", (dict1, dict2));

// result:
// "[dict1 Value] [dict2 Value] and more"

SmartObjects

Note:

  • The examples below use Dictionaries for simplicity. In the same manner other types than Dictionaries can be used as data sources.
  • Use ValueTuples instead of SmartObjects because they are less expensive.

With SmartObjects

  • all objects used for Smart.Format can be collected in one place as the first argument
  • the format string can be written like each object would be the first argument of Smart.Format
  • there is no need to bother from which argument a value should come from

Example:

var d1 = new Dictionary<string,string> { {"myKey", "myValue"} };
var nl = new Dictionary<string,string> { {"2ndKey", "my2ndValue" } };

var smartObj = new SmartObjects();
smartObj.AddRange(new object[] {d1, nl});

Smart.Format("{myKey} {2ndKey} and more...", smartObj);

// result:
// "myValue my2ndValue and more"

More arguments to the format string

Like with string.Format it is possible to use several data sources as parameters to SmartFormat. The conecept however is a bit different:

var dict1 = new Dictionary<string, string>() { {"Name", "John"} };
var dict2 = new Dictionary<string, string>() { { "City", "Washington" } };

var result = Smart.Format("First dictionary: {0:{Name}}, second dictionary: {1:{City}}", dict1, dict2);

// alternative notation:
var result = Smart.Format("First dictionary: {0.Name}, second dictionary: {1.City}", dict1, dict2);

// result: 
// "First dictionary: John, second dictionary: Washington"
Clone this wiki locally