Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ Or, to connect just one context to another database:

var context = new DataContext("AnotherDatabaseConnection");

If you need to connect to a dynamic connection string, pass a ConnectionStringSettings object to the DataContext class:

var context = new DataContext(new ConnectionStringSettings("myDb", "myConnectionString"));

Parameters binding of the Thunderstruck commands prevents string concatenation, errors, conversions and SQL injection:

var insertParams = new { Name = "Esprit Turbo", ModelYear = 1981 };
Expand Down
50 changes: 50 additions & 0 deletions Thunderstruck-3.5.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30523.141
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Thunderstruck-3.5", "Thunderstruck\Thunderstruck-3.5.csproj", "{277E5AD1-EC26-4273-B6F5-18CEB36CDC0F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{F35E2E1A-DCD9-4807-9EE6-0BF0FCAA050B}"
ProjectSection(SolutionItems) = preProject
.nuget\NuGet.Config = .nuget\NuGet.Config
.nuget\NuGet.exe = .nuget\NuGet.exe
.nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E9FAB6F7-94F5-482B-8419-A38EFFEC9A53}"
ProjectSection(SolutionItems) = preProject
.gitattributes = .gitattributes
.gitignore = .gitignore
LICENSE.txt = LICENSE.txt
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|Mixed Platforms = Debug|Mixed Platforms
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|Mixed Platforms = Release|Mixed Platforms
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{277E5AD1-EC26-4273-B6F5-18CEB36CDC0F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{277E5AD1-EC26-4273-B6F5-18CEB36CDC0F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{277E5AD1-EC26-4273-B6F5-18CEB36CDC0F}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{277E5AD1-EC26-4273-B6F5-18CEB36CDC0F}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{277E5AD1-EC26-4273-B6F5-18CEB36CDC0F}.Debug|x86.ActiveCfg = Debug|Any CPU
{277E5AD1-EC26-4273-B6F5-18CEB36CDC0F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{277E5AD1-EC26-4273-B6F5-18CEB36CDC0F}.Release|Any CPU.Build.0 = Release|Any CPU
{277E5AD1-EC26-4273-B6F5-18CEB36CDC0F}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{277E5AD1-EC26-4273-B6F5-18CEB36CDC0F}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{277E5AD1-EC26-4273-B6F5-18CEB36CDC0F}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9A46D863-5935-4A2B-98FA-966BF9C9F981}
EndGlobalSection
EndGlobal
14 changes: 12 additions & 2 deletions Thunderstruck.Test/Unit/DataContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Thunderstruck.Runtime;
using System.Data;
using FluentAssertions;
using System.Configuration;

namespace Thunderstruck.Test.Unit
{
Expand Down Expand Up @@ -132,9 +133,18 @@ public void DataContext_Should_Be_Possible_To_Use_Another_Connection_String()
var context = new DataContext("AnotherDatabase", Transaction.Begin);

context.ConnectionSettings.ConnectionString.Should().Be("whatever");
}
}

[TestMethod]
[TestMethod]
public void DataContext_Should_Be_Possible_To_Use_Connection_String_Settings()
{
var css = new ConnectionStringSettings("SettingsDatabase", "connectionStringSettings");
var context = new DataContext(css, Transaction.Begin);

context.ConnectionSettings.ConnectionString.Should().Be("connectionStringSettings");
}

[TestMethod]
public void DataContext_Should_Not_Create_Connection_If_Not_Use_Commands()
{
var context = new DataContext();
Expand Down
7 changes: 6 additions & 1 deletion Thunderstruck.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
# Visual Studio Version 16
VisualStudioVersion = 16.0.30523.141
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Thunderstruck", "Thunderstruck\Thunderstruck.csproj", "{0DC5AB48-1DA5-4185-A18E-46C111A82454}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Thunderstruck.Test", "Thunderstruck.Test\Thunderstruck.Test.csproj", "{1516ECE1-30A2-443A-BB17-1FEE1997ADD3}"
Expand Down Expand Up @@ -54,4 +56,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9A46D863-5935-4A2B-98FA-966BF9C9F981}
EndGlobalSection
EndGlobal
36 changes: 24 additions & 12 deletions Thunderstruck/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,41 @@ static DataContext()
/// <summary>
/// Creates a new transactional data context to connection string named "Default".
/// </summary>
public DataContext() : this(null, Transaction.Begin) { }
public DataContext() : this((string)null, Transaction.Begin) { }

/// <summary>
/// Creates a new data context to connection string named "Default".
/// </summary>
/// <param name="transactionMode">Defines if data context is transactional.</param>
public DataContext(Transaction transactionMode) : this(null, transactionMode) { }
public DataContext(Transaction transactionMode) : this((string)null, transactionMode) { }

/// <summary>
/// Creates a new data context.
/// </summary>
/// <param name="connectionStringName">Connection string name of target database.</param>
/// <param name="transaction">Defines if data context is transactional.</param>
public DataContext(string connectionStringName, Transaction transaction)
{
TransactionMode = transaction;

var connectionName = connectionStringName ?? DefaultConnectionStringName;
ConnectionSettings = ConnectionStringBuffer.Instance.Get(connectionName);

var providerFactory = new ProviderFactory();
Provider = providerFactory.Create(ConnectionSettings, transaction);
}
public DataContext(string connectionStringName, Transaction transaction) : this(ConnectionStringBuffer.Instance.Get(connectionStringName ?? DefaultConnectionStringName), transaction) { }

/// <summary>
/// Creates a new data context.
/// </summary>
/// <param name="connectionStringSettings">Connection string settings of target database.</param>
public DataContext(ConnectionStringSettings connectionStringSettings) : this(connectionStringSettings, Transaction.Begin) { }

/// <summary>
/// Creates a new data context.
/// </summary>
/// <param name="connectionStringSettings">Connection string settings of target database.</param>
/// <param name="transaction">Defines if data context is transactional.</param>
public DataContext(ConnectionStringSettings connectionStringSettings, Transaction transaction)
{
TransactionMode = transaction;

ConnectionSettings = connectionStringSettings;

var providerFactory = new ProviderFactory();
Provider = providerFactory.Create(ConnectionSettings, transaction);
}

/// <summary>
/// Thunderstruck data provider.
Expand Down
5 changes: 5 additions & 0 deletions Thunderstruck/Provider/ProviderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,15 @@ private IDbConnection CreateConnection(string providerName, ConnectionStringSett

public static void AddProvider(string providerName, Type providerType)
{
#if !NET35
if (String.IsNullOrWhiteSpace(providerName))
#else
if (String.IsNullOrEmpty(providerName))
#endif
{
throw new ArgumentException("Parameter 'providerName' can not be null and must contain data.", "providerName");
}

if (providerType == null)
{
throw new ArgumentNullException("providerType");
Expand Down
2 changes: 1 addition & 1 deletion Thunderstruck/Runtime/DataObjectCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private string GetCommaFields(DataContext context)
{
var fields = _runtimeObject.GetFields(removePrimaryKey: _primaryKey.Name);
var formatedFields = fields.Select(f => String.Format(context.Provider.FieldFormat, f));
return String.Join(", ", formatedFields);
return String.Join(", ", formatedFields.ToArray());
}
}
}
2 changes: 1 addition & 1 deletion Thunderstruck/Runtime/DataObjectQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private string GetCommaFields(DataContext context)
{
var fields = _runtimeObject.GetFields(removePrimaryKey: null);
var formatedFields = fields.Select(f => String.Format(context.Provider.FieldFormat, f));
return String.Join(", ", formatedFields);
return String.Join(", ", formatedFields.ToArray());
}
}
}
12 changes: 7 additions & 5 deletions Thunderstruck/Runtime/DataReader.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Data;
#if !NET35
using System.Dynamic;
#endif
using System.Linq;
using System.Reflection;

Expand All @@ -24,8 +26,12 @@ public DataReader(IDataReader dataReader)
var properties = typeof(T).GetProperties();
var readerFields = GetFields();

#if !NET35
bool isDynamic = (typeof(IDynamicMetaObjectProvider).IsAssignableFrom(typeof(T)));
IDictionary<string, object> dynamicValues = null;
#else
bool isDynamic = false;
#endif

while (_dataReader.Read())
{
Expand All @@ -35,11 +41,7 @@ public DataReader(IDataReader dataReader)
{
if (isDynamic)
{
if (dynamicValues == null)
{
dynamicValues = (IDictionary<string, object>)item;
}

IDictionary<string, object> dynamicValues = (IDictionary<string, object>)item;
dynamicValues[field] = GetSafeValue(_dataReader[field].GetType(), field);

continue;
Expand Down
2 changes: 1 addition & 1 deletion Thunderstruck/Runtime/DataRuntimeObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private string CreateFieldAndParameter(string field, string paramIdentifier)

private string Comma(IEnumerable<string> list)
{
return String.Join(", ", list);
return String.Join(", ", list.ToArray());
}
}
}
84 changes: 84 additions & 0 deletions Thunderstruck/Thunderstruck-3.5.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{277E5AD1-EC26-4273-B6F5-18CEB36CDC0F}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Thunderstruck</RootNamespace>
<AssemblyName>Thunderstruck</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile>
</TargetFrameworkProfile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\Net35\</OutputPath>
<DefineConstants>TRACE;DEBUG;NET35</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\Net35\Thunderstruck.xml</DocumentationFile>
<NoWarn>1591</NoWarn>
<RunCodeAnalysis>false</RunCodeAnalysis>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<RegisterForComInterop>false</RegisterForComInterop>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisIgnoreGeneratedCode>false</CodeAnalysisIgnoreGeneratedCode>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Release\Net35\</OutputPath>
<DefineConstants>TRACE;NET35</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\Net35\Thunderstruck.xml</DocumentationFile>
<NoWarn>1591</NoWarn>
<BuildPackage>true</BuildPackage>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisIgnoreGeneratedCode>false</CodeAnalysisIgnoreGeneratedCode>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
</ItemGroup>
<ItemGroup>
<Compile Include="DataContext.cs" />
<Compile Include="DataObject.cs" />
<Compile Include="IgnoreAttribute.cs" />
<Compile Include="Provider\Common\MySqlProvider.cs" />
<Compile Include="Provider\Common\OracleProvider.cs" />
<Compile Include="Provider\Common\SqlProvider.cs" />
<Compile Include="Provider\IDataProvider.cs" />
<Compile Include="Provider\DefaultProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Runtime\ConnectionStringBuffer.cs" />
<Compile Include="Runtime\DataObjectCommand.cs" />
<Compile Include="Runtime\DataObjectQuery.cs" />
<Compile Include="Runtime\ParametersBinder.cs" />
<Compile Include="Runtime\DataRuntimeObject.cs" />
<Compile Include="Runtime\DataReader.cs" />
<Compile Include="Provider\ProviderFactory.cs" />
<Compile Include="ThunderException.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Thunderstruck.nuspec" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.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>