Skip to content

Commit b480a51

Browse files
committed
Dynamic parameters
1 parent 81d42b9 commit b480a51

File tree

12 files changed

+481
-16
lines changed

12 files changed

+481
-16
lines changed

src/CfCommand.cs

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
using System.Data;
44
using System.Data.Common;
55
using System.Diagnostics;
6+
using System.Dynamic;
7+
using System.Linq;
8+
using System.Linq.Expressions;
9+
using System.Reflection;
10+
using System.Runtime.CompilerServices;
611
using log4net;
12+
using Microsoft.CSharp.RuntimeBinder;
713

814
namespace ConnectionFactory
915
{
@@ -33,7 +39,7 @@ public void Dispose()
3339
#region ExecuteNonQuery
3440

3541
//[System.Diagnostics.DebuggerStepThrough]
36-
public int ExecuteNonQuery(CommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null)
42+
public int ExecuteNonQuery(CfCommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null)
3743
{
3844
Logger.Debug("Begin Method");
3945
try
@@ -72,7 +78,7 @@ public int ExecuteNonQuery(CommandType cmdType, string cmdText, IEnumerable<CfPa
7278
/// <param name="cmdParms">Command Parameters (@parameter)</param>
7379
/// <returns>IEnumerable IDataRecord</returns>
7480
[System.Diagnostics.DebuggerStepThrough]
75-
public IEnumerable<IDataReader> LazyLoad(CommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null)
81+
public IEnumerable<IDataReader> LazyLoad(CfCommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null)
7682
{
7783
Logger.InfoFormat("LazyLoad: {0}", cmdText);
7884
Logger.Info(cmdParms);
@@ -103,7 +109,7 @@ public IEnumerable<IDataReader> LazyLoad(CommandType cmdType, string cmdText, IE
103109
/// <returns>list of entities</returns>
104110
[System.Diagnostics.DebuggerStepThrough]
105111
public IEnumerable<T> LazyLoadForObjects<T>(
106-
CommandType cmdType, string cmdText,IEnumerable<CfParameter> cmdParms = null) where T : new()
112+
CfCommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null) where T : new()
107113
{
108114
return LazyLoadForObjects<T>(LazyLoad(cmdType, cmdText, cmdParms));
109115
}
@@ -162,7 +168,7 @@ public IEnumerable<T> LazyLoadForObjects<T>(
162168
}
163169

164170
public IEnumerable<dynamic> LazyLoadForObjects(
165-
CommandType cmdType, string cmdText,IEnumerable<CfParameter> cmdParms = null)
171+
CfCommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null)
166172
{
167173
return LazyLoadForObjects(LazyLoad(cmdType, cmdText, cmdParms));
168174
}
@@ -195,7 +201,7 @@ public static IEnumerable<dynamic> LazyLoadForObjects(IEnumerable<IDataReader> d
195201
/// <param name="cmdParms">Command Parameters (@parameter)</param>
196202
/// <returns>Data Reader</returns>
197203
[System.Diagnostics.DebuggerStepThrough]
198-
public IDataReader ExecuteReader(CommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null)
204+
public IDataReader ExecuteReader(CfCommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null)
199205
{
200206
try
201207
{
@@ -213,6 +219,55 @@ public IDataReader ExecuteReader(CommandType cmdType, string cmdText, IEnumerabl
213219
}
214220
}
215221

222+
public IDataReader ExecuteReader(CfCommandType cmdType, string cmdText, object cmdParms)
223+
{
224+
try
225+
{
226+
IList<CfParameter> cfParams = null;
227+
if (cmdParms != null)
228+
{
229+
if (!(cmdParms is IEnumerable<CfParameter>))
230+
{
231+
232+
var props = cmdParms as ExpandoObject as IDictionary<string, object>;
233+
if (props != null)
234+
{
235+
cfParams = new List<CfParameter>(props.Count());
236+
foreach (var p in props)
237+
{
238+
cfParams.Add(new CfParameter(p.Key, p.Value));
239+
}
240+
}
241+
else
242+
{
243+
var properties = cmdParms.GetType().GetProperties();
244+
if (properties.Any())
245+
{
246+
cfParams = new List<CfParameter>(properties.Count());
247+
foreach (var property in properties)
248+
{
249+
cfParams.Add(new CfParameter(property.Name, property.GetValue(cmdParms, null)));
250+
}
251+
}
252+
}
253+
}
254+
}
255+
256+
_conn.EstablishFactoryConnection();
257+
PrepareCommand(cmdType, cmdText, cfParams);
258+
return _cmd.ExecuteReader(CommandBehavior.CloseConnection);
259+
}
260+
catch (Exception ex)
261+
{
262+
Logger.Error(ex);
263+
throw new CfException("Unknown Error (Connection Factory: ExecuteReader) " + ex.Message, ex);
264+
}
265+
}
266+
267+
268+
269+
270+
216271
#endregion
217272

218273
#region QueryForList
@@ -227,7 +282,7 @@ public IDataReader ExecuteReader(CommandType cmdType, string cmdText, IEnumerabl
227282
/// <returns>list of entities</returns>
228283
[System.Diagnostics.DebuggerStepThrough]
229284
public IList<T> QueryForList<T>(
230-
CommandType cmdType, string cmdText,IEnumerable<CfParameter> cmdParms = null) where T : new()
285+
CfCommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null) where T : new()
231286
{
232287
return QueryForList<T>(ExecuteReader(cmdType, cmdText, cmdParms));
233288
}
@@ -300,7 +355,7 @@ public IList<T> QueryForList<T>(
300355
/// <param name="cmdParms">Command Parameters (@parameter)</param>
301356
/// <returns>list of entities (ExpandoObject)</returns>
302357
public IList<dynamic> QueryForList(
303-
CommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null)
358+
CfCommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null)
304359
{
305360
return QueryForList(ExecuteReader(cmdType, cmdText, cmdParms));
306361
}
@@ -339,7 +394,7 @@ public static IList<dynamic> QueryForList(IDataReader dr)
339394
/// <param name="cmdParms">Command Parameters (@parameter)</param>
340395
/// <returns>Entity</returns>
341396
[System.Diagnostics.DebuggerStepThrough]
342-
public T QueryForObject<T>(CommandType cmdType, string cmdText,
397+
public T QueryForObject<T>(CfCommandType cmdType, string cmdText,
343398
IEnumerable<CfParameter> cmdParms = null) where T : new()
344399
{
345400
return QueryForObject<T>(ExecuteReader(cmdType, cmdText, cmdParms));
@@ -406,7 +461,7 @@ public T QueryForObject<T>(CommandType cmdType, string cmdText,
406461
}
407462

408463
public dynamic QueryForObject(
409-
CommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null)
464+
CfCommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null)
410465
{
411466
return QueryForObject(ExecuteReader(cmdType, cmdText, cmdParms));
412467
}
@@ -421,7 +476,7 @@ public static dynamic QueryForObject(IDataReader dr)
421476
#region ExecuteScalar
422477

423478
[System.Diagnostics.DebuggerStepThrough]
424-
public T ExecuteScalar<T>(CommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null)
479+
public T ExecuteScalar<T>(CfCommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null)
425480
{
426481
try
427482
{
@@ -453,7 +508,7 @@ public T ExecuteScalar<T>(CommandType cmdType, string cmdText, IEnumerable<CfPar
453508
}
454509
}
455510

456-
public dynamic ExecuteScalar(CommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null)
511+
public dynamic ExecuteScalar(CfCommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null)
457512
{
458513
_conn.EstablishFactoryConnection();
459514
PrepareCommand(cmdType, cmdText, cmdParms);
@@ -480,7 +535,7 @@ public dynamic ExecuteScalar(CommandType cmdType, string cmdText, IEnumerable<Cf
480535
/// <param name="cmdParms">Command Parameters (@parameter)</param>
481536
/// <returns>DataSet</returns>
482537
[System.Diagnostics.DebuggerStepThrough]
483-
public DataSet DataAdapter(CommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null)
538+
public DataSet DataAdapter(CfCommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null)
484539
{
485540
// we use a try/catch here because if the method throws an exception we want to
486541
// close the connection throw code, because no datareader will exist, hence the
@@ -511,7 +566,7 @@ public DataSet DataAdapter(CommandType cmdType, string cmdText, IEnumerable<CfPa
511566
#region PREPARE COMMAND AND CREATE PARAMETERS (Private Methods)
512567

513568
[System.Diagnostics.DebuggerStepThrough]
514-
private void PrepareCommand(CommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null)
569+
private void PrepareCommand(CfCommandType cmdType, string cmdText, IEnumerable<CfParameter> cmdParms = null)
515570
{
516571
_conn.EstablishFactoryConnection();
517572

@@ -521,7 +576,7 @@ private void PrepareCommand(CommandType cmdType, string cmdText, IEnumerable<CfP
521576
_cmd = _conn.CreateDbCommand();
522577

523578
_cmd.CommandText = cmdText;
524-
_cmd.CommandType = cmdType;
579+
_cmd.CommandType = (CommandType)cmdType;
525580

526581
CreateDbParameters(cmdParms);
527582
}
@@ -554,4 +609,12 @@ private DbParameter CreateDbParameter(string name, object value, DbType dbType =
554609
#endregion
555610

556611
}
612+
613+
public enum CfCommandType
614+
{
615+
Text = 1,
616+
StoredProcedure = 4,
617+
TableDirect = 512,
618+
}
619+
557620
}

src/ConnectionFactory.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>ConnectionFactory</RootNamespace>
1111
<AssemblyName>ConnectionFactory</AssemblyName>
12-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
1414
<TargetFrameworkProfile />
1515
</PropertyGroup>

src/ConnectionFactory.sln

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ VisualStudioVersion = 14.0.25420.1
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConnectionFactory", "ConnectionFactory.csproj", "{A1A20978-F2B5-433A-9FA0-79ACDC7FEAAB}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConnectionFactoryTest", "..\test\ConnectionFactoryTest\ConnectionFactoryTest.csproj", "{D4D4633E-E7B8-440B-9861-6636E0D542B2}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConnectionFactoryNET45", "ConnectionFactoryNET45\ConnectionFactoryNET45.csproj", "{E4237F09-E01C-4CB3-A9AF-05970DF33082}"
11+
EndProject
812
Global
913
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1014
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +19,14 @@ Global
1519
{A1A20978-F2B5-433A-9FA0-79ACDC7FEAAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
1620
{A1A20978-F2B5-433A-9FA0-79ACDC7FEAAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
1721
{A1A20978-F2B5-433A-9FA0-79ACDC7FEAAB}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{D4D4633E-E7B8-440B-9861-6636E0D542B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{D4D4633E-E7B8-440B-9861-6636E0D542B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{D4D4633E-E7B8-440B-9861-6636E0D542B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{D4D4633E-E7B8-440B-9861-6636E0D542B2}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{E4237F09-E01C-4CB3-A9AF-05970DF33082}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{E4237F09-E01C-4CB3-A9AF-05970DF33082}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{E4237F09-E01C-4CB3-A9AF-05970DF33082}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{E4237F09-E01C-4CB3-A9AF-05970DF33082}.Release|Any CPU.Build.0 = Release|Any CPU
1830
EndGlobalSection
1931
GlobalSection(SolutionProperties) = preSolution
2032
HideSolutionNode = FALSE
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{E4237F09-E01C-4CB3-A9AF-05970DF33082}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>ConnectionFactoryNET45</RootNamespace>
11+
<AssemblyName>ConnectionFactoryNET45</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
34+
<HintPath>..\packages\log4net.2.0.8\lib\net45-full\log4net.dll</HintPath>
35+
<Private>True</Private>
36+
</Reference>
37+
<Reference Include="System" />
38+
<Reference Include="System.Configuration" />
39+
<Reference Include="System.Core" />
40+
<Reference Include="System.Data.Linq" />
41+
<Reference Include="System.Transactions" />
42+
<Reference Include="System.Xml.Linq" />
43+
<Reference Include="System.Data.DataSetExtensions" />
44+
<Reference Include="Microsoft.CSharp" />
45+
<Reference Include="System.Data" />
46+
<Reference Include="System.Net.Http" />
47+
<Reference Include="System.Xml" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<Compile Include="..\CfCommand.cs">
51+
<Link>CfCommand.cs</Link>
52+
</Compile>
53+
<Compile Include="..\CfConnection.cs">
54+
<Link>CfConnection.cs</Link>
55+
</Compile>
56+
<Compile Include="..\CfConvert.cs">
57+
<Link>CfConvert.cs</Link>
58+
</Compile>
59+
<Compile Include="..\CfException.cs">
60+
<Link>CfException.cs</Link>
61+
</Compile>
62+
<Compile Include="..\CfExtensions.cs">
63+
<Link>CfExtensions.cs</Link>
64+
</Compile>
65+
<Compile Include="..\CfMapCache.cs">
66+
<Link>CfMapCache.cs</Link>
67+
</Compile>
68+
<Compile Include="..\CfParameter.cs">
69+
<Link>CfParameter.cs</Link>
70+
</Compile>
71+
<Compile Include="..\CfTransaction.cs">
72+
<Link>CfTransaction.cs</Link>
73+
</Compile>
74+
<Compile Include="Properties\AssemblyInfo.cs" />
75+
</ItemGroup>
76+
<ItemGroup>
77+
<None Include="packages.config" />
78+
</ItemGroup>
79+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
80+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
81+
Other similar extension points exist, see Microsoft.Common.targets.
82+
<Target Name="BeforeBuild">
83+
</Target>
84+
<Target Name="AfterBuild">
85+
</Target>
86+
-->
87+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("ConnectionFactoryNET45")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("ConnectionFactoryNET45")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("e4237f09-e01c-4cb3-a9af-05970df33082")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="log4net" version="2.0.8" targetFramework="net45" />
4+
</packages>

src/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="log4net" version="2.0.8" targetFramework="net40" requireReinstallation="true" />
3+
<package id="log4net" version="2.0.8" targetFramework="net40" />
44
</packages>

0 commit comments

Comments
 (0)