https://github.com/anderly/expansive
A powerful string expansion library for .NET you never knew you always wanted.
https://github.com/anderly/expansive
Last synced: 10 months ago
JSON representation
A powerful string expansion library for .NET you never knew you always wanted.
- Host: GitHub
- URL: https://github.com/anderly/expansive
- Owner: anderly
- Created: 2011-10-10T20:28:02.000Z (over 14 years ago)
- Default Branch: master
- Last Pushed: 2014-11-03T15:46:26.000Z (over 11 years ago)
- Last Synced: 2025-04-12T09:18:03.070Z (about 1 year ago)
- Language: C#
- Homepage: http://nuget.org/List/Packages/Expansive
- Size: 227 KB
- Stars: 35
- Watchers: 5
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Expansive
[](http://www.nuget.org/Packages/Expansive)
[](http://opensource.org/licenses/MS-PL)
### A powerful string expansion library for .NET you never knew you always wanted.
### View the [Release Notes](https://github.com/anderly/Expansive/blob/master/ReleaseNotes.md) to see change history.
## Why do I need it?
Config settings, string.Format, email, SMS, push notifications, Facebook posts, Twitter posts, reports, the list goes on.
We work with strings every day and it isn't fun. **Expansive** changes that by making your strings readable, intuitive, smart and...expandable!
## How would I use it?
- Use as a more readable alternative to string.Format()
- Easily embed tokens in strings and expand them easily.
- Chain together tokens to reduce redundant values.
- Simple templating for emails, SMS, push notifications, Facebook posts, Twitter posts, reports, etc.
- Use your imagination.
## Features
* Uses a Func lambda factory method as the source for token lookup/expansion
* By default string tokens are expanded using ConfigurationManager.AppSettings as the source (change this to your liking)
* Dynamic ConfigurationManager wrapper called Config wraps the Expansive API and removes need to call Expand()
* Register your own Func ExpansionFactory as the default string expansion factory or specify on the call to Expand()
* 4 Token Style Formats to pick from:
- MvcRoute Style "{token}" (default)
- Razor Style "@token" or "@(token)"
- NAnt Style "${token}"
- MSBuild Style "$(token)"
* Set your TokenStyle format globally or on a per call basis on the call to Expand()
* Support for chained expansions from one token to another
## How do I install it?
Using NuGet:
Install-Package Expansive
**OR**
Simply drop the code into your app and change it as you wish.
## Show me the code
### Simple readable alternative to string.Format() using Func<string, string> lamda
**MvcRoute-style token**
"Hello, {name}".Expand(n => "John")
// returns "Hello, John"
**Razor-style token**
"Hello, @name".Expand(n => "John")
// returns "Hello, John"
or
"Hello, @(name)".Expand(n => "John")
// returns "Hello, John"
**NAnt-style token**
"Hello, ${name}".Expand(n => "John")
// returns "Hello, John"
**MSBuild-style token**
"Hello, $(name)".Expand(n => "John")
// returns "Hello, John"
### Simple readable alternative to string.Format() using positional replacement
**One token (MvcRoute-style)**
"Hello, {name}".Expand("John")
// returns "Hello, John"
**Two tokens (MvcRoute-style)**
"Hello, {firstName} {lastName}".Expand("John","Smith")
// returns "Hello, John Smith"
**3 tokens (MvcRoute-style), 2 single tokens, 1 composite token**
var firstName = "John";
var lastName = "Smith";
var fullName = "{firstName} {lastName}";
"Your first name is {firstName}. Your last name is {lastName}. Your full name is {fullName}".Expand(fullName, lastName, fullName)
// returns "Your first name is John. Your last name is Smith. Your full name is John Smith"
### Simple Example (using AppSettings as default source for token expansion)
In app.config:
Use the **.Expand()** extension method explicitly on the string to be expanded:
"{KeyForAppSetting1} should be inserted here.".Expand();
// returns "ValueForAppSetting1 should be inserted here."
### Moderate Example (using AppSettings as default source for token expansion)
In app.config:
Use the **.Expand()** extension method on the string to be expanded:
var connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
connectionString.Expand() // returns "server=db01.mycompany.com;uid=uid;pwd=pwd;Initial Catalog=master;"
or
Use the Dynamic ConfigurationManager wrapper "Config" as follows (Explicit call to Expand() not necessary):
var serverName = Config.AppSettings.ServerName;
// returns "db01.mycompany.com"
var connectionString = Config.ConnectionStrings.Default;
// returns "server=db01.mycompany.com;uid=uid;pwd=pwd;Initial Catalog=master;"
### Advanced Example 1 (using AppSettings as default source for token expansion)
In app.config:
Use the .Expand() extension method on the string to be expanded:
var connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
connectionString.Expand() // returns "server=db01-dev.mycompany.com;uid=uid;pwd=pwd;Initial Catalog=master;"
### Advanced Example 2 (using custom Func lambda as default source for token expansion)
var tokenValueDictionary = new Dictionary {
{"setting1","The quick"}
,{"setting2","{setting1} brown fox"}
,{"setting3","jumped over"}
,{"setting4","{setting2} {setting3} the lazy dog."}
,{"setting5","{setting4}"}
};
Expansive.DefaultExpansionFactory = name => tokenValueDictionary[name];
Console.WriteLine("{setting5}".Expand());
//returns "The quick brown fox jumped over the lazy dog."
or
// Here, we specify the token expansion factory on the call to Expand()
Console.WriteLine("{setting5}".Expand(name => tokenValueDictionary[name]));
//returns "The quick brown fox jumped over the lazy dog."
### Simple model-based string templating
var model = new { FirstName = "John" };
// MvcRoute-Style (default)
var mvcRouteStyleString = "Hello, {FirstName}".Expand(model);
// Razor-Style
Expansive.DefaultTokenStyle = TokenStyle.Razor;
var razorStyleString = "Hello, @FirstName".Expand(model);
// NAnt-Style
Expansive.DefaultTokenStyle = TokenStyle.NAnt;
var nantStyleString = "Hello, ${FirstName}".Expand(model);
// MSBuild-Style
Expansive.DefaultTokenStyle = TokenStyle.MSBuild;
var msBuildStyleString = "Hello, $(FirstName)".Expand(model);
// All return "Hello, John"
### Moderate model-based string templating
var model = new {
FirstName = "John",
LastName = "Smith",
FullName = "{FirstName} {LastName}"
};
"FullName:{FullName}".Expand(model);
// Returns "FullName:John Smith"
### Advanced multi-model string templating
var model1 = new {
FirstName = "John",
LastName = "Smith",
FullName = "{FirstName} {LastName}"
};
var model2 = new {
EmailAddress = "john.smith@gmail.com"
};
"FullName:{FullName} ({EmailAddress})".Expand(model1, model2);
// Returns "FullName:John Smith (john.smith@gmail.com)"
## Copyright
Copyright 2011 Adam Anderly
## License
MS-PL: http://www.opensource.org/licenses/MS-PL