-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Accompanies blog post http://www.ipreferjim.com/?p=1200
- Loading branch information
1 parent
15fa6f4
commit e70ff75
Showing
9 changed files
with
336 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 2013 | ||
VisualStudioVersion = 12.0.31101.0 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SolidExample", "SolidExample\SolidExample.csproj", "{911E78F5-7543-4EB4-83D5-C037321C373A}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{911E78F5-7543-4EB4-83D5-C037321C373A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{911E78F5-7543-4EB4-83D5-C037321C373A}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{911E78F5-7543-4EB4-83D5-C037321C373A}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{911E78F5-7543-4EB4-83D5-C037321C373A}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> | ||
</startup> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Year,Make,Model,Description,Price | ||
1997,Ford,E350,"ac, abs, moon",3000.00 | ||
1999,Chevy,"Venture ""Extended Edition""","",4900.00 | ||
1999,Chevy,"Venture ""Extended Edition, Very Large""",,5000.00 | ||
1996,Jeep,Grand Cherokee,"MUST SELL! | ||
air, moon roof, loaded",4799.00 |
31 changes: 31 additions & 0 deletions
31
2014-12-08/SolidExample/SolidExample/DataReaderStringIndexedWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Data; | ||
|
||
namespace SolidExample | ||
{ | ||
/// <summary> | ||
/// Wraps a DataRow record to provide a consistent indexer interface | ||
/// </summary> | ||
internal class DataRowStringIndexedWrapper : IStringIndexed | ||
{ | ||
private readonly DataRow _row; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of <see cref="DataRowStringIndexedWrapper"/> | ||
/// </summary> | ||
/// <param name="row"></param> | ||
public DataRowStringIndexedWrapper(DataRow row) | ||
{ | ||
_row = row; | ||
} | ||
|
||
#region IStringIndexed Members | ||
|
||
/// <inheritdoc/> | ||
object IStringIndexed.this[string key] | ||
{ | ||
get { return _row[key]; } | ||
} | ||
|
||
#endregion | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
2014-12-08/SolidExample/SolidExample/DataRecordStringIndexedWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Data; | ||
|
||
namespace SolidExample | ||
{ | ||
/// <summary> | ||
/// Wraps an IDataRecord record to provide a consistent indexer interface | ||
/// </summary> | ||
internal class DataRecordStringIndexedWrapper : IStringIndexed | ||
{ | ||
private readonly IDataRecord _record; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of <see cref="DataRecordStringIndexedWrapper"/> | ||
/// </summary> | ||
/// <param name="record">A record</param> | ||
public DataRecordStringIndexedWrapper(IDataRecord record) | ||
{ | ||
_record = record; | ||
} | ||
|
||
#region IStringIndexed Members | ||
|
||
/// <inheritdoc/> | ||
object IStringIndexed.this[string key] | ||
{ | ||
get { return _record[key]; } | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace SolidExample | ||
{ | ||
/// <summary> | ||
/// A contract for instances that offer objects via keyed index | ||
/// </summary> | ||
public interface IStringIndexed | ||
{ | ||
/// <summary> | ||
/// An indexer by key | ||
/// </summary> | ||
/// <param name="key">A unique string-based key for accessing objects</param> | ||
/// <returns>A found object at given <param name="key"></param></returns> | ||
object this[string key] { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Data.Odbc; | ||
using System.Data.OleDb; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace SolidExample | ||
{ | ||
class Program | ||
{ | ||
private static string cnxTemplate = | ||
@"Driver={{Microsoft Text Driver (*.txt; *.csv)}};Dbq={0};Extensions=csv,txt"; | ||
|
||
static void Main(string[] args) | ||
{ | ||
/** The Problem **/ | ||
Console.WriteLine("A standard DataReader:"); | ||
string connectionString = String.Format(cnxTemplate, Environment.CurrentDirectory); | ||
using (var connection = new OdbcConnection(connectionString)) | ||
{ | ||
connection.Open(); | ||
using (var cmd = connection.CreateCommand()) | ||
{ | ||
// csv from http://www.wikiwand.com/en/Comma-separated_values#/Example | ||
cmd.CommandText = "select * from Cars.csv"; | ||
|
||
using (var reader = cmd.ExecuteReader()) | ||
while (reader.Read()) | ||
{ | ||
OverloadedDumpRow(reader); | ||
} | ||
} | ||
} | ||
Console.WriteLine(); | ||
|
||
Console.WriteLine("A standard DataSet example."); | ||
using (var connection = new OdbcConnection(connectionString)) | ||
{ | ||
connection.Open(); | ||
|
||
var adapter = new OdbcDataAdapter("SELECT * FROM Cars.csv", connection); | ||
|
||
DataSet ds = new DataSet("Temp"); | ||
adapter.Fill(ds); | ||
|
||
foreach (DataRow row in ds.Tables[0].Rows) | ||
{ | ||
OverloadedDumpRow(row); | ||
} | ||
} | ||
Console.WriteLine(); | ||
|
||
/** The Solution **/ | ||
Console.WriteLine("A wrapped DataReader (shared mapping function):"); | ||
using (var connection = new OdbcConnection(connectionString)) | ||
{ | ||
connection.Open(); | ||
using (var cmd = connection.CreateCommand()) | ||
{ | ||
// csv from http://www.wikiwand.com/en/Comma-separated_values#/Example | ||
cmd.CommandText = "select * from Cars.csv"; | ||
|
||
using (var reader = cmd.ExecuteReader()) | ||
while (reader.Read()) | ||
{ | ||
DumpRow(new DataRecordStringIndexedWrapper(reader)); | ||
} | ||
} | ||
} | ||
Console.WriteLine(); | ||
|
||
Console.WriteLine("A wrapped DataSet example (shared mapping function)."); | ||
using (var connection = new OdbcConnection(connectionString)) | ||
{ | ||
connection.Open(); | ||
|
||
var adapter = new OdbcDataAdapter("SELECT * FROM Cars.csv", connection); | ||
|
||
DataSet ds = new DataSet("Temp"); | ||
adapter.Fill(ds); | ||
|
||
foreach (DataRow row in ds.Tables[0].Rows) | ||
{ | ||
DumpRow(new DataRowStringIndexedWrapper(row)); | ||
} | ||
} | ||
|
||
Console.ReadLine(); | ||
} | ||
|
||
/// <summary> | ||
/// Example function operatoring on a single instance implementing IDataRecord | ||
/// </summary> | ||
/// <param name="row"></param> | ||
static void OverloadedDumpRow(IDataRecord row) | ||
{ | ||
Console.WriteLine("A {0} {1} {2}", | ||
row["Year"], row["Make"], row["Model"]); | ||
} | ||
|
||
/// <summary> | ||
/// A needlessly redundant method to achieve the same as IDataRecord version above | ||
/// </summary> | ||
/// <param name="row"></param> | ||
static void OverloadedDumpRow(DataRow row) | ||
{ | ||
Console.WriteLine("A {0} {1} {2}", | ||
row["Year"], row["Make"], row["Model"]); | ||
} | ||
|
||
/// <summary> | ||
/// An adapter is created to implement a known interface. | ||
/// </summary> | ||
/// <param name="record"></param> | ||
static void DumpRow(IStringIndexed record) | ||
{ | ||
Console.WriteLine("A {0} {1} {2}", | ||
record["Year"], record["Make"], record["Model"]); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
2014-12-08/SolidExample/SolidExample/Properties/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("SolidExample")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("SolidExample")] | ||
[assembly: AssemblyCopyright("Copyright © 2014")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("840bf2c0-b6e5-4dd8-8fd3-d8060d1228c8")] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// You can specify all the values or you can default the Build and Revision Numbers | ||
// by using the '*' as shown below: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{911E78F5-7543-4EB4-83D5-C037321C373A}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>SolidExample</RootNamespace> | ||
<AssemblyName>SolidExample</AssemblyName> | ||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="DataReaderStringIndexedWrapper.cs" /> | ||
<Compile Include="DataRecordStringIndexedWrapper.cs" /> | ||
<Compile Include="IStringIndexed.cs" /> | ||
<Compile Include="Program.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="App.config" /> | ||
<None Include="Cars.csv"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
Other similar extension points exist, see Microsoft.Common.targets. | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
--> | ||
</Project> |