Class DataReaderDecoratorExtensions
Extension methods for the IDataReader interface hidden behind the IDecorator<T> interface.
public static class DataReaderDecoratorExtensions
- Inheritance
-
DataReaderDecoratorExtensions
Examples
DataReaderDecoratorExtensions provides extension methods on Decorator.Enclose for converting IDataReader content into encoded strings or streams, both synchronously and asynchronously. This example creates a single-column DSV data source backed by DsvDataReader with three rows of data, then wraps it and calls ToEncodedString, ToEncodedStringAsync, and ToStream. Key steps include repositioning the underlying stream between conversions and using DsvDataReader as the input source. Console output shows the string representation of the data and the stream length.
using System;
using System.Data;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Cuemon;
using Cuemon.Data;
namespace MyApp.Examples
{
public class DataReaderDecoratorExtensionsExample
{
public static async Task DemonstrateAsync()
{
// Create a single-column DSV data source (must have exactly one field)
var csv = "Value\r\n1001\r\n1002\r\n1003\r\n";
var bytes = Encoding.UTF8.GetBytes(csv);
using (var stream = new MemoryStream(bytes))
using (var reader = new DsvDataReader(new StreamReader(stream)))
{
// Convert the single-field IDataReader to an encoded string (sync)
string result = Decorator.Enclose((IDataReader)reader).ToEncodedString();
Console.WriteLine(result);
// Convert the single-field IDataReader to an encoded string (async)
stream.Position = 0;
using (var asyncReader = new DsvDataReader(new StreamReader(new MemoryStream(bytes))))
{
string asyncResult = await Decorator.Enclose((IDataReader)asyncReader).ToEncodedStringAsync();
Console.WriteLine(asyncResult);
// Convert the data reader content to a stream
stream.Position = 0;
using (var readerAgain = new DsvDataReader(new StreamReader(new MemoryStream(bytes))))
{
Stream dataStream = Decorator.Enclose((IDataReader)readerAgain).ToStream();
Console.WriteLine($"Stream length: {dataStream.Length}");
}}}}}
}
Methods
ToEncodedString(IDecorator<IDataReader>)
Converts the enclosed IDataReader of the decorator to an equivalent string representation.
public static string ToEncodedString(this IDecorator<IDataReader> decorator)
Parameters
decoratorIDecorator<IDataReader>The IDecorator<T> to extend.
Returns
- string
A string value that is equivalent to the enclosed IDataReader of the
decorator.
Remarks
IDataReader must return only one field (for instance, an XML field), otherwise an ArgumentException is thrown.
Exceptions
- ArgumentNullException
decoratoris null or its underlying value is null.- ArgumentOutOfRangeException
decoratorunderlying value is not valid.
ToEncodedStringAsync(IDecorator<IDataReader>)
Asynchronously converts the enclosed IDataReader of the decorator to an equivalent string representation.
public static Task<string> ToEncodedStringAsync(this IDecorator<IDataReader> decorator)
Parameters
decoratorIDecorator<IDataReader>The IDecorator<T> to extend.
Returns
- Task<string>
A task that represents the asynchronous operation. The task result contains a string value that is equivalent to the enclosed IDataReader of the
decorator.
Remarks
IDataReader must return only one field (for instance, an XML field), otherwise an ArgumentException is thrown.
Exceptions
- ArgumentNullException
decoratoris null or its underlying value is null.- ArgumentOutOfRangeException
decoratorunderlying value is not valid.
ToStream(IDecorator<IDataReader>)
Converts the enclosed IDataReader of the decorator to an equivalent Stream representation.
public static Stream ToStream(this IDecorator<IDataReader> decorator)
Parameters
decoratorIDecorator<IDataReader>The IDecorator<T> to extend.
Returns
- Stream
A Stream that is equivalent to the enclosed IDataReader of the
decorator.
Remarks
IDataReader must return only one field (for instance, an XML field), otherwise an ArgumentException is thrown.
Exceptions
- ArgumentNullException
decoratoris null or its underlying value is null.- ArgumentOutOfRangeException
decoratorunderlying value is not valid.