Skip to content

Commit 31af76c

Browse files
committed
Added CachingAdapterFactory
1 parent 375caf6 commit 31af76c

13 files changed

Lines changed: 190 additions & 25 deletions
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Simple.Data.AdapterApi
7+
{
8+
/// <summary>
9+
/// Provides access to internal types and methods of the Simple.Data main assembly
10+
/// to be used by Adapter authors for extending the library.
11+
/// </summary>
12+
public static class DatabaseHelper
13+
{
14+
/// <summary>
15+
/// Creates a <see cref="Database"/> using the specified adapter.
16+
/// </summary>
17+
/// <param name="adapter">The adapter.</param>
18+
/// <returns>A new <see cref="Database"/> instance.</returns>
19+
public static Database Create(Adapter adapter)
20+
{
21+
return new Database(adapter);
22+
}
23+
}
24+
}
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("Simple.Data.AdapterApi")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Simple.Data.AdapterApi")]
13+
[assembly: AssemblyCopyright("Copyright © 2010")]
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("2a8fe8e9-6a4b-4832-99cd-bfb522f7e298")]
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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{67ACEAD5-B457-4B52-9C55-611A61A73B56}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Simple.Data.AdapterApi</RootNamespace>
12+
<AssemblyName>Simple.Data.AdapterApi</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="System" />
35+
<Reference Include="System.Core" />
36+
<Reference Include="System.Xml.Linq" />
37+
<Reference Include="System.Data.DataSetExtensions" />
38+
<Reference Include="Microsoft.CSharp" />
39+
<Reference Include="System.Data" />
40+
<Reference Include="System.Xml" />
41+
</ItemGroup>
42+
<ItemGroup>
43+
<Compile Include="DatabaseHelper.cs" />
44+
<Compile Include="Properties\AssemblyInfo.cs" />
45+
</ItemGroup>
46+
<ItemGroup>
47+
<ProjectReference Include="..\Simple.Data\Simple.Data.csproj">
48+
<Project>{148CEE80-2E84-4ABD-B5AB-20415B2BBD21}</Project>
49+
<Name>Simple.Data</Name>
50+
</ProjectReference>
51+
</ItemGroup>
52+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
53+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
54+
Other similar extension points exist, see Microsoft.Common.targets.
55+
<Target Name="BeforeBuild">
56+
</Target>
57+
<Target Name="AfterBuild">
58+
</Target>
59+
-->
60+
</Project>

Simple.Data.SqlTest/ObservableDataReaderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ObservableDataReaderTest
1313
{
1414
private static SqlConnection MakeConnection()
1515
{
16-
return new SqlConnection(@"data source=.;initial catalog=SimpleTest;integrated security=true");
16+
return new SqlConnection(@"data source=.\SQLSERVER2008;initial catalog=SimpleTest;integrated security=true");
1717
}
1818

1919
private static SqlCommand MakeCommand()

Simple.Data.SqlTest/app.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</configSections>
55
<connectionStrings>
66
<add name="Simple.Data.SqlTest.Properties.Settings.ConnectionString"
7-
connectionString="Data Source=.;Initial Catalog=SimpleTest;Integrated Security=True"
7+
connectionString="Data Source=.\SQLSERVER2008;Initial Catalog=SimpleTest;Integrated Security=True"
88
providerName="System.Data.SqlClient" />
99
</connectionStrings>
1010
</configuration>

Simple.Data.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Simple.Azure.Tests", "Simpl
5757
EndProject
5858
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Simple.NExtLib.Tests", "Simple.NExtLib.Tests", "{EAEEE2B0-E5FB-4FCE-97CD-3C77E593E5A1}"
5959
EndProject
60+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Simple.Data.AdapterApi", "Simple.Data.AdapterApi\Simple.Data.AdapterApi.csproj", "{67ACEAD5-B457-4B52-9C55-611A61A73B56}"
61+
EndProject
6062
Global
6163
GlobalSection(TestCaseManagementSettings) = postSolution
6264
CategoryFile = Simple.Data.vsmdi
@@ -132,6 +134,10 @@ Global
132134
{E8CBD0A4-4770-4F6B-9BBE-A37681E119F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
133135
{E8CBD0A4-4770-4F6B-9BBE-A37681E119F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
134136
{E8CBD0A4-4770-4F6B-9BBE-A37681E119F2}.Release|Any CPU.Build.0 = Release|Any CPU
137+
{67ACEAD5-B457-4B52-9C55-611A61A73B56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
138+
{67ACEAD5-B457-4B52-9C55-611A61A73B56}.Debug|Any CPU.Build.0 = Debug|Any CPU
139+
{67ACEAD5-B457-4B52-9C55-611A61A73B56}.Release|Any CPU.ActiveCfg = Release|Any CPU
140+
{67ACEAD5-B457-4B52-9C55-611A61A73B56}.Release|Any CPU.Build.0 = Release|Any CPU
135141
EndGlobalSection
136142
GlobalSection(SolutionProperties) = preSolution
137143
HideSolutionNode = FALSE

Simple.Data/AdapterFactory.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@
66

77
namespace Simple.Data
88
{
9-
internal interface IAdapterFactory
10-
{
11-
Adapter Create(object settings);
12-
Adapter Create(string adapterName, object settings);
13-
Adapter Create(IEnumerable<KeyValuePair<string,object>> settings);
14-
Adapter Create(string adapterName, IEnumerable<KeyValuePair<string, object>> settings);
15-
}
16-
179
class AdapterFactory : IAdapterFactory
1810
{
1911
public Adapter Create(object settings)
@@ -36,7 +28,12 @@ public Adapter Create(IEnumerable<KeyValuePair<string,object>> settings)
3628
throw new ArgumentException("Cannot infer adapter type from settings.");
3729
}
3830

39-
public Adapter Create(string adapterName, IEnumerable<KeyValuePair<string, object>> settings)
31+
public virtual Adapter Create(string adapterName, IEnumerable<KeyValuePair<string, object>> settings)
32+
{
33+
return DoCreate(adapterName, settings);
34+
}
35+
36+
protected Adapter DoCreate(string adapterName, IEnumerable<KeyValuePair<string, object>> settings)
4037
{
4138
var adapter = MefHelper.Compose<Adapter>(adapterName);
4239
adapter.Setup(settings);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace Simple.Data
8+
{
9+
class CachingAdapterFactory : AdapterFactory
10+
{
11+
private readonly ConcurrentDictionary<string, Adapter> _cache = new ConcurrentDictionary<string, Adapter>();
12+
public override Adapter Create(string adapterName, IEnumerable<KeyValuePair<string, object>> settings)
13+
{
14+
return _cache.GetOrAdd(HashSettings(adapterName, settings), s => DoCreate(adapterName, settings));
15+
}
16+
17+
private static string HashSettings(string adapterName, IEnumerable<KeyValuePair<string, object>> settings)
18+
{
19+
return adapterName +
20+
string.Join("#", settings.Select(kvp => kvp.Key + kvp.Value));
21+
}
22+
}
23+
}

Simple.Data/Database.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace Simple.Data
1515
/// </summary>
1616
public class Database : DynamicObject
1717
{
18+
private static readonly IDatabaseOpener DatabaseOpener = new DatabaseOpener();
1819
private static readonly Lazy<dynamic> LazyDefault = new Lazy<dynamic>(Open, LazyThreadSafetyMode.ExecutionAndPublication);
1920
private readonly ConcurrentDictionary<string, DynamicTable> _tables = new ConcurrentDictionary<string, DynamicTable>();
2021
private readonly Adapter _adapter;
@@ -28,15 +29,6 @@ internal Database(Adapter adapter)
2829
_adapter = adapter;
2930
}
3031

31-
/// <summary>
32-
/// Initializes a new instance of the <see cref="Database"/> class.
33-
/// </summary>
34-
/// <param name="connectionProvider">The connection provider to use with <see cref="AdoAdapter"/> for data access.</param>
35-
internal Database(IConnectionProvider connectionProvider)
36-
{
37-
_adapter = new AdoAdapter(connectionProvider);
38-
}
39-
4032
/// <summary>
4133
/// Gets the adapter being used by the <see cref="Database"/> instance.
4234
/// </summary>
@@ -46,6 +38,11 @@ internal Adapter Adapter
4638
get { return _adapter; }
4739
}
4840

41+
public static IDatabaseOpener Opener
42+
{
43+
get { return DatabaseOpener; }
44+
}
45+
4946
/// <summary>
5047
/// Gets a default instance of the Database. This connects to an ADO.NET data source
5148
/// specified in the 'Simple.Data.Properties.Settings.ConnectionString' config ConnectionStrings setting.

Simple.Data/DatabaseOpener.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,31 @@
77

88
namespace Simple.Data
99
{
10-
internal static class DatabaseOpener
10+
public interface IDatabaseOpener
1111
{
12-
private static readonly IAdapterFactory AdapterFactory = new AdapterFactory();
12+
Database OpenDefault();
13+
Database OpenFile(string filename);
14+
Database OpenConnection(string connectionString);
15+
}
16+
17+
internal class DatabaseOpener : IDatabaseOpener
18+
{
19+
private static readonly IAdapterFactory AdapterFactory = new CachingAdapterFactory();
1320
private static Func<Database> _openDefault = OpenDefaultMethod;
1421
private static Func<string, Database> _openFile = OpenFileMethod;
1522
private static Func<string, Database> _openConnection = OpenConnectionMethod;
1623

17-
public static Database OpenDefault()
24+
public Database OpenDefault()
1825
{
1926
return _openDefault();
2027
}
2128

22-
public static Database OpenFile(string filename)
29+
public Database OpenFile(string filename)
2330
{
2431
return _openFile(filename);
2532
}
2633

27-
public static Database OpenConnection(string connectionString)
34+
public Database OpenConnection(string connectionString)
2835
{
2936
return _openConnection(connectionString);
3037
}

0 commit comments

Comments
 (0)