InterpolatedStrings.StrongName
2.0.0-beta1
.NET 5.0
This package targets .NET 5.0. The package is compatible with this framework or higher.
.NET Standard 2.1
This package targets .NET Standard 2.1. The package is compatible with this framework or higher.
This is a prerelease version of InterpolatedStrings.StrongName.
There is a newer prerelease version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package InterpolatedStrings.StrongName --version 2.0.0-beta1
NuGet\Install-Package InterpolatedStrings.StrongName -Version 2.0.0-beta1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="InterpolatedStrings.StrongName" Version="2.0.0-beta1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add InterpolatedStrings.StrongName --version 2.0.0-beta1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: InterpolatedStrings.StrongName, 2.0.0-beta1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install InterpolatedStrings.StrongName as a Cake Addin #addin nuget:?package=InterpolatedStrings.StrongName&version=2.0.0-beta1&prerelease // Install InterpolatedStrings.StrongName as a Cake Tool #tool nuget:?package=InterpolatedStrings.StrongName&version=2.0.0-beta1&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
InterpolatedStrings
InterpolatedStringBuilder is like a StringBuilder but for Interpolated Strings (FormattableString)
It's an implementation of FormattableString
with support for concatenating strings, replace, insert, etc.
Quickstart
- Install the NuGet package InterpolatedStrings or NuGet package InterpolatedStrings.StrongName
- Add
using InterpolatedStrings;
to your usings. - See examples below.
Basics
How to create an InterpolatedStringBuilder, append some more interpolated strings
string arg1 = "FormattableString";
var s = new InterpolatedStringBuilder($"This is exactly like {arg1}");
// s.Format now is equal to "This is exactly like {0}"
// s.Arguments contain [arg1]
string arg2 = "additional";
// += is an operator overload, but you can also call s.Append(...);
s += $"... but you can append {arg2} FormattableString instances";
// s.Format now is equal to "This is exactly like {0}... but you can append {1} FormattableString instances"
// s.Arguments now contains [arg1, arg2]
Sample Usage: Dynamic SQL building
int categoryId = 1;
double maxPrice = 20.50;
//------------------------------------------------------------------------------
// Creates an initial SQL query, and appends more conditions.
// Embedded objects are NOT converted to strings: they are still kept
// as objects (in Arguments list), and the underlying format string just keeps
// the numbered placeholders
//------------------------------------------------------------------------------
var query = new InterpolatedStringBuilder($"SELECT * FROM Products");
query += $" WHERE CategoryId={categoryId}";
query += $" AND price<={maxPrice}";
// query.Format now is "SELECT * FROM Products WHERE CategoryId={0} AND price<={1}"
// query.Arguments now is [categoryId, maxPrice]
//------------------------------------------------------------------------------
// Then you can create your own methods (or extensions) to convert back from
// InterpolatedStringBuilder into a valid SQL statement
//------------------------------------------------------------------------------
string sql = string.Format(query.Format, query.Arguments.Select((arg, i) => "@p" + i.ToString()).ToArray());
Assert.AreEqual("SELECT * FROM Products WHERE CategoryId=@p0 AND price<=@p1", sql);
// If you were using Dapper you could pass parameters like this:
// var sqlParms = new DynamicParameters();
// for (int i = 0; i < query.Arguments.Count; i++) { dbArgs.Add("p" + i.ToString(), query.Arguments[i].Argument); }
// var products = connection.Query<Product>(sql, sqlParms)
Conditional Appends
int? categoryId = null;
double? maxPrice = 20.50;
//------------------------------------------------------------------------------
// Fluent API allows short syntax for appending multiple blocks,
// and using conditions
//------------------------------------------------------------------------------
var query =
new InterpolatedStringBuilder($"SELECT * FROM Products WHERE 1=1")
.AppendIf(categoryId != null, $" AND CategoryId={categoryId}")
.AppendIf(maxPrice != null, $" AND price<={maxPrice}")
;
// Now query.Format is "SELECT * FROM Products WHERE 1=1 AND price<={0}"
Multiline Blocks, Replaces, Inserts, etc.
Using Raw String Literals:
int? categoryId = 3;
double? maxPrice = null;
//------------------------------------------------------------------------------
// Raw String Literals allows us to easily write multiline blocks
//------------------------------------------------------------------------------
var query = new InterpolatedStringBuilder($$"""
SELECT * FROM Products
/***where***/
ORDER BY Category, Name
""");
var wheres = new InterpolatedStringBuilder();
wheres.AppendIf(categoryId != null, $" AND CategoryId={categoryId}");
wheres.AppendIf(maxPrice != null, $" AND price<={maxPrice}");
if (wheres.Format.Length> 0)
{
wheres.Remove(0, " AND ".Length).Insert(0, $"WHERE ");
query.Replace("/***where***/", wheres);
}
Assert.AreEqual("""
SELECT * FROM Products
WHERE CategoryId={0}
ORDER BY Category, Name
""",
query.Format);
See more examples in unit tests.
How to Collaborate?
Please submit a pull-request or if you want to make a sugestion you can create an issue or contact me.
Stargazers over time
License
MIT License
See full documentation here
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- Microsoft.CSharp (>= 4.7.0)
-
net5.0
- Microsoft.CSharp (>= 4.7.0)
-
net6.0
- Microsoft.CSharp (>= 4.7.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.0.0-beta2 | 102 | 6/28/2023 |
2.0.0-beta1 | 88 | 6/25/2023 |