PvWay.MultiPartVarChar.nc8
1.0.0
dotnet add package PvWay.MultiPartVarChar.nc8 --version 1.0.0
NuGet\Install-Package PvWay.MultiPartVarChar.nc8 -Version 1.0.0
<PackageReference Include="PvWay.MultiPartVarChar.nc8" Version="1.0.0" />
paket add PvWay.MultiPartVarChar.nc8 --version 1.0.0
#r "nuget: PvWay.MultiPartVarChar.nc8, 1.0.0"
// Install PvWay.MultiPartVarChar.nc8 as a Cake Addin #addin nuget:?package=PvWay.MultiPartVarChar.nc8&version=1.0.0 // Install PvWay.MultiPartVarChar.nc8 as a Cake Tool #tool nuget:?package=PvWay.MultiPartVarChar.nc8&version=1.0.0
MultiPart VarChar for .Net Core 8
Persists (and retrieves) multi part text values IDictionary<string, string> into (and from) a single VARCHAR column into a database.
Usage
Constructor
From the business layer of your application use the Dictionary constructor
var dic = new Dictionary<string, string>()
{
{"en", "bear"},
{"fr", "ours"}
};
IPvWayMpVarChar myMpVarChar = new PvWayMpVarChar(dic);
Persisting into the Db
Now let's persist this value in one single NVARCHAR() column into the Db.
The following example prepares a simple SQL statement for a DAO implementation of the DAL but of course you may want to use this with the ORM of your choice (EF, NHibernate...)
// convert myMpVarChar to a string for insertion into the Db.
var mpText = myMpVarChar.ToString();
// hum yes... in this case we should make sure we escape the single quotes if any
mpText = mpText.Replace("'", "''");
// now we can use this var into an insert statement
var insertStatement = $"INSERT INTO [dbo].[MyTable] ([MpText]) VALUES ('{mpText}');";
// The line above will generate the following text
// INSERT INTO [dbo].[MyTable] ([MpText]) VALUES ('<en>bear</en><fr>ours</fr>');
// for the simplicity I do not provide here the code executing this insert
The key value dictionary is serialized to a single string that can be saved into the db into a VARCHAR(xxx) (or NVARCHAR(xxx)) column.
Up to you to see if you need a MAX length or if a smaller column will do the job.
It takes the using XML tags '<key>value</key>.
Example: <en>Bear</en><fr>Ours</fr>
Retrieving the data from the Db
// (not shown) here above the SELECT code that populates the reader object
var ord = reader.GetOrdinal("MpText");
// let's retreive the raw text from the Db
var retrievedMpText = reader.GetString(ord);
// time to deserialize
// for this we will use the static method TryDeserialize
var deserializeResult = PvWayMpVarChar.TryDeserialize(
retrievedMpText,
out var retrievedMpVarChar,
out var errorMessageIfAny);
if (!deserializeResult == PvWayDeserializationResult.Failed)
{
Console.WriteLine("it failed");
Console.WriteLine(errorMessageIfAny);
// log and throw
}
else
{
// Deserialization succeeded
// three ways to get the data
// (1) using the Dicionnary
var enVal = retrievedMpVarChar.MpDic["en"];
Console.WriteLine(enVal);
// ==> displays "bear"... or throws an error if there is no entry for "en"
// using the GetPartForKey method
var frVal = retrievedMpVarChar.GetPartForKey("fr");
Console.WriteLine(frVal);
// ==> displays "ours"... or null if there is not entry for "fr"
// using the FindPartForKey method
var findResult = retrievedMpVarChar.FindPartForKey("de", out var deVal);
Console.WriteLine(deVal);
// ==> displays "bear" taking de first key in the dic as default value
// the findResult equals PvWayFindPartResult.FirstValue
}
Thanks for reading so far 😃
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- No dependencies.
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 |
---|---|---|
1.0.0 | 194 | 1/2/2024 |
Initial version for dotNet Core 8