Commit 76ac5cd9 authored by Helgi Hafþórsson's avatar Helgi Hafþórsson
Browse files

Added samples. Reverted EnvironmentProviderExtensions since we have a single...

Added samples. Reverted EnvironmentProviderExtensions since we have a single DLL. Added license and readme.
parent 299a8319
......@@ -20,6 +20,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{ED35
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample1", "samples\Sample1\Sample1.csproj", "{83B95ECA-576D-44F1-9FDA-31E582D61187}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7CFD7249-5969-4F42-B072-8C8892B90BE2}"
ProjectSection(SolutionItems) = preProject
LICENSE.md = LICENSE.md
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample2", "samples\Sample2\Sample2.csproj", "{AA40D389-895A-481D-A924-06411DF667B3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -38,6 +46,10 @@ Global
{83B95ECA-576D-44F1-9FDA-31E582D61187}.Debug|Any CPU.Build.0 = Debug|Any CPU
{83B95ECA-576D-44F1-9FDA-31E582D61187}.Release|Any CPU.ActiveCfg = Release|Any CPU
{83B95ECA-576D-44F1-9FDA-31E582D61187}.Release|Any CPU.Build.0 = Release|Any CPU
{AA40D389-895A-481D-A924-06411DF667B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AA40D389-895A-481D-A924-06411DF667B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AA40D389-895A-481D-A924-06411DF667B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AA40D389-895A-481D-A924-06411DF667B3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -46,5 +58,6 @@ Global
{5A8FB352-10B9-4EB2-9297-49C9A66A65CE} = {871C6815-F7D6-4747-8B65-F39063EF1DBF}
{7D9B2CBD-DB01-4EE6-9743-68B0590D844D} = {22873875-9BB7-423E-9B7B-E8565A69D525}
{83B95ECA-576D-44F1-9FDA-31E582D61187} = {ED35E57D-7432-4DFB-99E3-9EAFD4812242}
{AA40D389-895A-481D-A924-06411DF667B3} = {ED35E57D-7432-4DFB-99E3-9EAFD4812242}
EndGlobalSection
EndGlobal
MIT License
Copyright (c) 2017 Helgi Hafþórsson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
\ No newline at end of file
## Synopsis
This is a simple library that makes writing database provider agnostic code slightly more convenient.
## Database Provider Agnostic Code?
Instead of your code directly depending on libraries such as `Oracle.ManagedDataAccess.Client` you can reference the standard `System.Data` and decide at runtime which database provider you will be using.
## Features
Today you can already write provider agnostic code without this library. The library adds the following features:
* Easy method of obtaining connection strings and provider from config file.
* Automatic creation of data provider based on config data.
* Easy to obtain correct connection type for either default connection string or a named connection string.
* Automatic opening of connections with the ability to run custom code when a connection is opened.
* Convenience methods for adding parameters to DbCommand-s.
* Convenience methods for getting values from DbDataReader-s.
* Convenience methods to handle Oracle specific types (ref cursors).
* An ISqlFactory interface and classes that implemented it for dealing with different flavors of SQL.
## Code Examples
### Sample 1: The Simple Default
Packages needed:
```Powershell
Install-Package Marsonsoft.Database
Install-Package Oracle.ManagedDataAccess
```
Code:
```C#
using Marsonsoft.Database;
namespace Sample1
{
class Program
{
static void Main(string[] args)
{
var dataEnvironment = DataEnvironmentProvider.Default.GetDefault();
using (var connection = dataEnvironment.ConnectionProvider.OpenConnection())
{
//...
}
}
}
}
```
The following snippet is required in app.config for Sample1 to work:
```xml
<connectionStrings>
<add name="DefaultConnection" providerName="Oracle.ManagedDataAccess.Client" connectionString="Data Source=myOracle;User Id=myUser;Password=myPass;" />
</connectionStrings>```
By convention, the name of the default connection string is `DefaultConnection`. In this XML example the `providerName` is required because we don't specify a default provider invariant name.
#### Example 2: Named Connection String
Packages needed:
```Powershell
Install-Package Marsonsoft.Database
Install-Package MySql.Data
```
```C#
using Marsonsoft.Database;
namespace Sample2
{
class Program
{
static void Main(string[] args)
{
var dataEnvironment = DataEnvironmentProvider.Default.GetByConnectionStringName("OtherConnection");
using (var connection = dataEnvironment.ConnectionProvider.OpenConnection())
{
//...
}
}
}
}
```
The following snippet is required in app.config for Sample1 to work:
```xml
<connectionStrings>
<add name="OtherConnection" providerName="Oracle.ManagedDataAccess.Client" connectionString="Data Source=myDataSource;User Id=myUserId;Password=myPassword;"/>
</connectionStrings>
```
## License
A short snippet describing the license (MIT, Apache, etc.)
\ No newline at end of file
......@@ -2,7 +2,7 @@
<configuration>
<connectionStrings>
<add name="DefaultConnection" providerName="Oracle.ManagedDataAccess.Client" connectionString="Data Source=isgen01;User Id=SERV_PING_SUPPORT_USR;Password=F8QMHPKI578W;" />
<add name="DefaultConnection" providerName="Oracle.ManagedDataAccess.Client" connectionString="Data Source=myOracle;User Id=myUser;Password=myPass;" />
</connectionStrings>
<startup>
......
using Marsonsoft.Database;
using Marsonsoft.Database.Oracle;
namespace Sample1
{
......@@ -7,15 +6,11 @@ namespace Sample1
{
static void Main(string[] args)
{
var environmentProvider = DataEnvironmentProvider.Default;
//environmentProvider.AddOracle();
var dataEnvironment = environmentProvider.GetDefault();
var dataEnvironment = DataEnvironmentProvider.Default.GetDefault();
using (var connection = dataEnvironment.ConnectionProvider.OpenConnection())
{
//...
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="DefaultConnection" providerName="Oracle.ManagedDataAccess.Client" connectionString="Data Source=myOracle;User Id=myUser;Password=myPass;" />
<add name="OtherConnection" providerName="MySql.Data.MySqlClient" connectionString="Server=myServer;Database=myDatabase;Uid=myUid;Pwd=myPass;" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
</configuration>
\ No newline at end of file
using Marsonsoft.Database;
namespace Sample2
{
class Program
{
static void Main(string[] args)
{
var dataEnvironment = DataEnvironmentProvider.Default.GetByConnectionStringName("OtherConnection");
using (var connection = dataEnvironment.ConnectionProvider.OpenConnection())
{
//...
}
}
}
}
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("Sample2")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Landsbankinn")]
[assembly: AssemblyProduct("Sample2")]
[assembly: AssemblyCopyright("Copyright © Landsbankinn 2017")]
[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("aa40d389-895a-481d-a924-06411df667b3")]
// 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")]
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" 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>{AA40D389-895A-481D-A924-06411DF667B3}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Sample2</RootNamespace>
<AssemblyName>Sample2</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</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="MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
<HintPath>..\..\packages\MySql.Data.6.9.9\lib\net45\MySql.Data.dll</HintPath>
</Reference>
<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.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Marsonsoft.Database\Marsonsoft.Database.csproj">
<Project>{5a8fb352-10b9-4eb2-9297-49c9a66a65ce}</Project>
<Name>Marsonsoft.Database</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MySql.Data" version="6.9.9" targetFramework="net462" />
</packages>
\ No newline at end of file
......@@ -31,7 +31,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet />
<CodeAnalysisRuleSet>..\..\codeAnalysis\MarsonsoftRecommended.ruleset</CodeAnalysisRuleSet>
<DocumentationFile>bin\Release\Marsonsoft.Database.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
......@@ -62,14 +62,12 @@
<Compile Include="ISqlFactory.cs" />
<Compile Include="ITextValidator.cs" />
<Compile Include="MySql\DataEnvironment.cs" />
<Compile Include="MySql\DataEnvironmentProviderExtensions.cs" />
<Compile Include="MySql\DbExceptionExtensions.cs" />
<Compile Include="MySql\MySqlExceptionNumber.cs" />
<Compile Include="MySql\MySqlTextValidator.cs" />
<Compile Include="MySql\SqlFactory.cs" />
<Compile Include="Oracle\AdapterFactory.cs" />
<Compile Include="Oracle\DataEnvironment.cs" />
<Compile Include="Oracle\DataEnvironmentProviderExtensions.cs" />
<Compile Include="Oracle\DbCommandExtensions.cs" />
<Compile Include="Oracle\DbConnectionExtensions.cs" />
<Compile Include="Oracle\DbExceptionExtensions.cs" />
......@@ -86,6 +84,7 @@
<Compile Include="TableName.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Marsonsoft.Database.nuspec" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
......
<?xml version="1.0" encoding="utf-8" ?>
<package >
<metadata>
<id>$id$</id>
<version>$version$</version>
<authors>$author$</authors>
<owners>Helgi Hafþórsson</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<copyright>$copyright$</copyright>
<projectUrl>https://gitlab.com/helgihaf/Database</projectUrl>
<licenseUrl>https://gitlab.com/helgihaf/Database/blob/master/LICENSE.md</licenseUrl>
</metadata>
</package>
\ No newline at end of file
using System;
namespace Marsonsoft.Database.MySql
{
/// <summary>
/// Contains extensions methods for the <see cref="DataEnvironmentProvider"/> class.
/// </summary>
public static class DataEnvironmentProviderExtensions
{
/// <summary>
/// Adds the <see cref="MySql.DataEnvironment"/> to the data environment provider's list of environments.
/// </summary>
/// <param name="dataEnvironmentProvider">The <see cref="DataEnvironmentProvider"/>.</param>
public static void AddMySql(this DataEnvironmentProvider dataEnvironmentProvider)
{
try
{
dataEnvironmentProvider.Add(typeof(DataEnvironment));
}
catch (ArgumentException)
{
// Silently continue if type has already been added.
}
}
}
}
using System;
namespace Marsonsoft.Database.Oracle
{
/// <summary>
/// Contains extensions methods for the <see cref="DataEnvironmentProvider"/> class.
/// </summary>
public static class DataEnvironmentProviderExtensions
{
/// <summary>
/// Adds the <see cref="Oracle.DataEnvironment"/> to the data environment provider's list of environments.
/// </summary>
/// <param name="dataEnvironmentProvider">The <see cref="DataEnvironmentProvider"/>.</param>
public static void AddOracle(this DataEnvironmentProvider dataEnvironmentProvider)
{
try
{
dataEnvironmentProvider.Add(typeof(DataEnvironment));
}
catch (ArgumentException)
{
// Silently continue if type has already been added.
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment