DotNetBlocks.System
Overview
Extends functionality for classes in the system namespace.
Getting started
Licensing and other information
Functionality
Extensions.
Guid generation
Extension methods on the Guid type to solve the problem of true Guid randomness on databases,
Problem solved
- first described by Jimmy Nillson who developed the CombGuid strategy. Part Guid, part timestamp, these probably unique numbers are ever increasing within a system without requiring database generation.
-
Extensions add functionality Initialize a Guid, CreateNewIds, Generate NewIds. and allow multiple strategies, depending on the type of system consuming the ids.
# Examples
Guid myId;
myId.Initialize();
Guid id2 = id2.NewId();
Guid id3 = id3.GenerateId();
id3.Initialize( GuidTypes.Oracle);
Stream Buffer
A stream buffer wraps a source stream, allowing the consumer code to read the stream without pulling the entire stream into memory. Supports background process threading.
Problem solved
In the code pattern where a source wants to write into a sink stream and another process wants to read the stream to write it into another destination, developers often use a MemoryStream class. This causes temporary spikes in memory usage, especially if the streams are files of unknown size. A 2gb file for example can kill a server along with other side effects of memory starvation, especially in cloud environments.
The stream buffer provides as destination stream that can be written to and read from at the same time, but limiting the amount of "in flight data" or buffer size.
How it works: Understanding pipes, flow control and not dead-locking.
# Examples
- streaming a file as an MVC http response
// Using memory stream - this is the in memory problem
var blobClient = container.GetBlobClient(blobPath);
var resultStream = new MemoryStream();
await blobClient.DownloadToAsync(resultStream);
// Reset the stream.
resultStream.position = 0;
// Return the stream.
return new FileStreamResult(stream, "application/pdf")
// Using buffer stream - eliminates memory issue
var blobClient = container.GetBlobClient(blobPath);
var resultStream = new StreamBuffer();
await blobClient.DownloadToAsync(resultStream.WriteStream);
// Return the stream.
return new FileStreamResult(resultStream.ReadStream, "application/pdf")
This example is very basic and does not show other advantages including built in alternative threading models etc.
- Stream Extensions - New functions to copy between streams and to calculate CRC values for a stream.
Stream Copy extensions
Problem solved
Existing methods on the streams to copy from one stream to another expect to copy the entire stream and block until the process is complete. For streams like the StreamBuffer implementation that block until there is capacity, the applications can hang.
These functions let you copy a subset of the stream from one stream to another and not the entire stream.
When you are testing copy functionality and working with streams where the may be hidden data issues, CRC functions can detect issues and ensure there are no errors in the process.
Examples
- copying parts of a a stream.
using(var sourceStream = new FileStream("sourcefile", FileMode.Open))
{
// Copy 100 bytes from the source stream to the destination stream.
using(var destinationStream = new FileStream("destinationfile", FileMode.Create))
{
sourceStream.CopyBytes(destinationStream, 100);
}
}