FrameworkContainers 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package FrameworkContainers --version 2.0.0                
NuGet\Install-Package FrameworkContainers -Version 2.0.0                
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="FrameworkContainers" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FrameworkContainers --version 2.0.0                
#r "nuget: FrameworkContainers, 2.0.0"                
#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 FrameworkContainers as a Cake Addin
#addin nuget:?package=FrameworkContainers&version=2.0.0

// Install FrameworkContainers as a Cake Tool
#tool nuget:?package=FrameworkContainers&version=2.0.0                

FrameworkContainers

Useful parts of Frameworks wrapped into single types.

PM> Install-Package FrameworkContainers

Intro

This library contains snippets of code I find myself adding over, and over again to new projects.
The code is reusable, as it's not domain specific, though it is specific to the way I code (I guess).

There is not a lot in here, as I didn't want to download the kitchen sink each time I needed a component from this library.
The components are not complex, any developer could easily create them in a few hours, to the same quality.
That said, it's rather the point; I want those few hours back for each new project (and I'm sick of copy-pasting!).

Components

Each component has a static core, from here the methods can be accessed easily inline; as a standard type (i.e. no dependency injection required).
If dependency injection is needed, then each component also have a Client type, with an accompanying interface.

The components have been designed to handle most use cases, with the true aim to be simple for the developer to use.
If you find some component does not meet your needs, then drop it and write custom code instead of fighting the framework.
The components are built to be performant, and have the best defaults set; they are hard to use in a wrong way.

There are three distinct ways to use a component (excluding the static / client options mentioned above).
You can use the raw type T, which will throw any encountered exceptions.
You can use Response<T>, which will internally log any exceptions, and only return a value of T when it can.
You can use Maybe<T, Exception>, which will return either T, or the error type Exception.
If you would like to know more about types: Response, and Maybe; please visit their repo ContainerExpressions.

Http Component

Send, and receive all the things over the network.
This component has support for get, post, put, and delete; with both sync, and async APIs.
The sync APIs are built on top of System.Net.WebRequest.
The async APIs are built on top of System.Net.Http.HttpClient.

[Things we do for you]

  • Set a default timeout of 30 seconds.
  • Set the content type, and content length.
  • Write in UTF8.
  • Read error responses, adding them to the generated HttpException.
  • Renew DNS every minute (for the domain(s) you are calling).
  • Disable insecure ciphers, only leaving up to date ones active.

[Examples]

Get http body T:

string body = Http.Get("/api/weather");

Get http body Response<T>:

Response<string> body = Http.Response.Get("/api/weather");

Get http body Maybe<T, Exception>:

Maybe<string, HttpException> body = Http.Response.Get("/api/weather");

Get http body IHttpClient:

IHttpClient http = new HttpClient();
string body = http.Get("/api/weather");

Create a new User from an Onboard request object using JSON, and the async API:

User user = await Http.PostJsonAsync<User, Onboard>(request);

Json Component

Serialize, and deserialize JSON to, and from domain models.
This component's APIs are built on top of System.Text.Json.JsonSerializer.

[Things we do for you]

  • Two serializer categories: Performant, and Permissive; one for speed, and one that "just works".
  • JSON converters for standard types to "just work", they include: bool, DateTime, Enum, Float, Guid, Int, and Long.

[Examples]

Json to model:

string json = "{\"name\": \"John Smith\"}";
User user = Json.ToModel<User>(json);

Model to json:

User user = new User { Name = "John Smith" };
string json = Json.FromModel(user);

Setting the JSON options:

User user = Json.ToModel<User>(json, JsonOptions.Performant);

Using a JSON converter (strings are converted to ints, nulls are converetd to 0, invalid values still throw errors):

[JsonConverter(typeof(DefaultIntConverter))]
public int Age { get; set; }

using Response<T>:

Response<User> user = Json.Response.ToModel<User>(json);

Using Maybe<T, Exception>:

Maybe<User, FormatDeserializeException> user = Json.Maybe.ToModel<User>(json);

Using IJsonClient:

IJsonClient json = new JsonClient();
User user = json.ToModel<User>(json);

Xml Component

Serialize, and deserialize XML to, and from domain models.
This component's APIs are built on top of System.Xml.Serialization.XmlSerializer.

[Things we do for you]

  • Remove default namespaces, and omit the XML declaration.
  • Allow custom character encodings.

[Examples]

XML to model:

User user = Xml.ToModel<User>(xml);

Model to XML:

string xml = Xml.FromModel(user);

Setting the XML options:

string xml = Xml.FromModel(user, new XmlOptions(true, true, null));

using Response<T>:

Response<User> user = Xml.Response.ToModel<User>(json);

Using Maybe<T, Exception>:

Maybe<User, FormatDeserializeException> user = Xml.Maybe.ToModel<User>(json);

Using IXmlClient:

IXmlClient xml = new XmlClient();
User user = xml.ToModel<User>(json);

Sql Component

Insert, update, select, and delete your way though the database (via stored procedures).
This component's APIs are built on top of System.Data.SqlClient.SqlConnection.

[Things we do for you]

  • Provide both sync, and async APIs for: ExecuteReader, ExecuteNonQuery, and BulkInsert.
  • Allow easy access to returned values with the Get<T> extension method.

[Examples]

Setting the global connection string:

Sql.ConnectionString = connectionString;

ExecuteNonQuery:

int rows = Sql.ExecuteNonQuery("usp_insert_user", new SqlParameter("@name", "John Smith"));

ExecuteNonQuery, overriding the global connection string:

int rows = Sql.ExecuteNonQuery("usp_insert_user", connectionString, new SqlParameter("@name", "John Smith"));

Async ExecuteReader using Get<T>:

IEnumerable<User> Read(IDataReader dr)
{
	List<User> results = new List<User>();
	while (dr.Read())
	{
		results.Add(new User
		{
			name = dr.Get<string>("Name")
		});
	}
	return results;
}

IEnumerable<User> users = await Sql.ExecuteReaderAsync(Read, "usp_select_users");

BulkInsert:

DataTable table = new DataTable();
table.Columns.Add("Name");
foreach (User user in users)
{
	var row = table.NewRow();
	row["Name"] = user.Name;
	table.Rows.Add(row);
}
table.AcceptChanges();

Sql.BulkInsert("tblUsers", table);

using Response<T>:

Response<int> rows = Sql.Response.ExecuteNonQuery("usp_insert_user", new SqlParameter("@name", "John Smith"));

Using Maybe<T, Exception>:

Maybe<int, Exception> rows = Sql.Maybe.ExecuteNonQuery("usp_insert_user", new SqlParameter("@name", "John Smith"));

Using ISqlClient:

ISqlClient sql = new SqlClient();
int rows = sql.ExecuteNonQuery("usp_insert_user", new SqlParameter("@name", "John Smith"));

Credits

Changelog

2.0.0

  • Started a changelog - reset the project.
  • Added Sql for ExecuteReader, ExecuteNonQuery, and BulkInsert database operations.
  • Added Http for Get, Post, Put, and Delete` network operations.
  • Added Json for Serialize, and Deserialize model operations.
  • Added Xml for Serialize, and Deserialize model operations.
  • Updated nuget pack settings, to make the project easier to deploy.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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.

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
4.2.0 153 9/18/2024
4.1.0 145 7/2/2024
4.0.0 98 7/1/2024
3.1.0 387 4/25/2023
3.0.0 175 4/19/2023
2.2.0 954 11/21/2021
2.1.0 1,184 11/20/2021
2.0.0 362 11/11/2021
1.0.0 1,431 4/21/2016

Added types for XML, JSON, HTTP, and SQL.