Skip to content
George Njeri (Swagfin) edited this page Mar 7, 2026 · 8 revisions

ObjectSemantics.NET Wiki

Fast, readable object-to-template mapping for .NET applications.

ObjectSemantics.NET helps you build email, SMS, receipt, invoice, and notification templates from your domain objects with minimal code.

Getting Started

1. Install

Install-Package ObjectSemantics.NET

2. Basic Mapping

using ObjectSemantics.NET;

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

var person = new Person { Name = "John", Age = 30 };
string result = person.Map("Hi {{ Name }}, age {{ Age }}");
// Hi John, age 30

You can also map from the template side:

string result = "Hi {{ Name }}".Map(person);

3. Nested Property Mapping

public class Customer
{
    public string CompanyName { get; set; }
}

public class Payment
{
    public decimal Amount { get; set; }
    public Customer Customer { get; set; }
}

var payment = new Payment
{
    Amount = 100000000m,
    Customer = new Customer { CompanyName = "CRUDSOFT TECHNOLOGIES" }
};

string result = payment.Map("Paid {{ Amount:N2 }} by {{ Customer.CompanyName }}");
// Paid 100,000,000.00 by CRUDSOFT TECHNOLOGIES

4. Additional Parameters

Pass runtime values that are not on your model:

public class MessageItem
{
    public string Name { get; set; }
    public int Qty { get; set; }
}

public class MessageModel
{
    public string Name { get; set; }
    public bool IsPaid { get; set; }
    public List<MessageItem> Items { get; set; } = new List<MessageItem>();
}

var model = new MessageModel { Name = "Jane" };
var extra = new Dictionary<string, object>
{
    ["AppName"] = "ObjectSemantics.NET",
    ["Meta.Channel"] = "EMAIL"
};

string result = model.Map("{{ Name }} via {{ Meta.Channel }} in {{ AppName }}", extra);

5. XML Escaping Option

Use TemplateMapperOptions when rendering XML-safe output:

var model = new MessageModel { Name = "Tom & Jerry <Ltd>" };
string result = model.Map("{{ Name }}", null, new TemplateMapperOptions
{
    XmlCharEscaping = true
});
// Tom &amp; Jerry &lt;Ltd&gt;

6. First Loop and Condition

var model = new MessageModel
{
    Name = "John",
    IsPaid = true,
    Items = new List<MessageItem>
    {
        new MessageItem { Name = "Keyboard", Qty = 1 },
        new MessageItem { Name = "Mouse", Qty = 2 }
    }
};

string template = @"Status: {{ #if(IsPaid == true) }}PAID{{ #else }}UNPAID{{ #endif }}
{{ #foreach(Items) }}- {{ Qty }} x {{ Name }}
{{ #endforeach }}";

string result = model.Map(template);

Clone this wiki locally