Skip to content

Commit 8e2c798

Browse files
committed
Db4o initial implementation
1 parent edcbc44 commit 8e2c798

6 files changed

Lines changed: 226 additions & 1 deletion

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using SharpRepository.Repository.Caching;
3+
4+
namespace SharpRepository.Db4o
5+
{
6+
/// <summary>
7+
/// XML Repository layer
8+
/// </summary>
9+
/// <typeparam name="T">The object type that is stored as XML.</typeparam>
10+
/// <typeparam name="TKey">The type of the primary key.</typeparam>
11+
public class Db4oRepository<T, TKey> : Db4oRepositoryBase<T, TKey> where T : class, new()
12+
{
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="Db4oRepository&lt;T, TKey&gt;"/> class.
15+
/// </summary>
16+
/// <param name="storagePath">Path to the directory where the yap files are stored. The yap filename is determined by the TypeName</param>
17+
/// <param name="cachingStrategy">The caching strategy. Defaults to <see cref="NoCachingStrategy{T,TKey}" />.</param>
18+
public Db4oRepository(string storagePath, ICachingStrategy<T, TKey> cachingStrategy = null)
19+
: base(storagePath, cachingStrategy)
20+
{
21+
if (String.IsNullOrEmpty(storagePath)) throw new ArgumentNullException("storagePath");
22+
}
23+
}
24+
25+
/// <summary>
26+
/// XML Repository layer
27+
/// </summary>
28+
/// <typeparam name="T">The object type that is stored.</typeparam>
29+
public class Db4oRepository<T> : Db4oRepositoryBase<T, int> where T : class, new()
30+
{
31+
/// <summary>
32+
/// Initializes a new instance of the <see cref="Db4oRepository&lt;T&gt;"/> class.
33+
/// </summary>
34+
/// <param name="storagePath">Path to the directory where the yap files are stored. The yap filename is determined by the TypeName</param>
35+
/// <param name="cachingStrategy">The caching strategy. Defaults to <see cref="NoCachingStrategy&lt;T&gt;" />.</param>
36+
public Db4oRepository(string storagePath, ICachingStrategy<T, int> cachingStrategy = null)
37+
: base(storagePath, cachingStrategy)
38+
{
39+
}
40+
}
41+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.Linq;
3+
using Db4objects.Db4o;
4+
using Db4objects.Db4o.Linq;
5+
using SharpRepository.Repository;
6+
using SharpRepository.Repository.Caching;
7+
using SharpRepository.Repository.FetchStrategies;
8+
9+
namespace SharpRepository.Db4o
10+
{
11+
public class Db4oRepositoryBase<T, TKey> : LinqRepositoryBase<T, TKey> where T : class, new()
12+
{
13+
//Nice reference: http://jamesrhicks.blogspot.com/2010/02/getting-started-with-db4o.html
14+
private IObjectContainer _documentStore;
15+
16+
internal Db4oRepositoryBase(string storagePath, ICachingStrategy<T, TKey> cachingStrategy = null)
17+
: base(cachingStrategy)
18+
{
19+
Initialize(storagePath);
20+
}
21+
22+
private void Initialize(string storagePath)
23+
{
24+
_documentStore = Db4oEmbedded.OpenFile(Db4oEmbedded.NewConfiguration(), storagePath);
25+
}
26+
27+
protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = null)
28+
{
29+
return _documentStore.AsQueryable<T>();
30+
}
31+
32+
protected override T GetQuery(TKey key)
33+
{
34+
return _documentStore.AsQueryable<T>().FirstOrDefault(x => MatchOnPrimaryKey(x, key));
35+
}
36+
37+
private bool MatchOnPrimaryKey(T item, TKey keyValue)
38+
{
39+
TKey value;
40+
return GetPrimaryKey(item, out value) && keyValue.Equals(value);
41+
}
42+
43+
protected override void AddItem(T entity)
44+
{
45+
TKey id;
46+
47+
if (GetPrimaryKey(entity, out id) && Equals(id, default(TKey)))
48+
{
49+
id = GeneratePrimaryKey();
50+
SetPrimaryKey(entity, id);
51+
}
52+
53+
_documentStore.Store(entity);
54+
}
55+
56+
protected override void DeleteItem(T entity)
57+
{
58+
_documentStore.Delete(entity);
59+
}
60+
61+
protected override void UpdateItem(T entity)
62+
{
63+
_documentStore.Store(entity);
64+
}
65+
66+
protected override void SaveChanges()
67+
{
68+
_documentStore.Commit();
69+
}
70+
71+
public override void Dispose()
72+
{
73+
_documentStore.Close();
74+
if (_documentStore != null)
75+
_documentStore.Dispose();
76+
}
77+
78+
private TKey GeneratePrimaryKey()
79+
{
80+
if (typeof (TKey) == typeof (Guid))
81+
{
82+
return (TKey) Convert.ChangeType(Guid.NewGuid(), typeof (TKey));
83+
}
84+
85+
if (typeof (TKey) == typeof (Int32))
86+
{
87+
TKey pkValue;
88+
89+
T last = GetAll().LastOrDefault() ?? new T();
90+
GetPrimaryKey(last, out pkValue);
91+
92+
int nextInt = Convert.ToInt32(pkValue) + 1;
93+
return (TKey) Convert.ChangeType(nextInt, typeof (TKey));
94+
}
95+
96+
throw new InvalidOperationException(
97+
"Primary key could not be generated. This only works for GUID and Int32.");
98+
}
99+
}
100+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Reflection;
2+
using System.Runtime.InteropServices;
3+
4+
// General Information about an assembly is controlled through the following
5+
// set of attributes. Change these attribute values to modify the information
6+
// associated with an assembly.
7+
[assembly: AssemblyTitle("SharpRepository.Db4o")]
8+
[assembly: AssemblyDescription("")]
9+
10+
// The following GUID is for the ID of the typelib if this project is exposed to COM
11+
[assembly: Guid("26f03070-2502-4e3f-9f4e-f047a032606b")]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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>{12DD3830-5A12-484C-98B4-D30811DEC8A9}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>SharpRepository.Db4o</RootNamespace>
12+
<AssemblyName>SharpRepository.Db4o</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="Db4objects.Db4o">
35+
<HintPath>..\packages\db4o-devel.8.1.184.15492\lib\net40\Db4objects.Db4o.dll</HintPath>
36+
</Reference>
37+
<Reference Include="Db4objects.Db4o.Linq">
38+
<HintPath>..\packages\db4o-devel.8.1.184.15492\lib\net40\Db4objects.Db4o.Linq.dll</HintPath>
39+
</Reference>
40+
<Reference Include="System" />
41+
<Reference Include="System.Core" />
42+
<Reference Include="Microsoft.CSharp" />
43+
</ItemGroup>
44+
<ItemGroup>
45+
<Compile Include="..\CommonAssembly.cs">
46+
<Link>Properties\CommonAssembly.cs</Link>
47+
</Compile>
48+
<Compile Include="Properties\AssemblyInfo.cs" />
49+
<Compile Include="Db4ORepository.cs" />
50+
<Compile Include="Db4ORepositoryBase.cs" />
51+
</ItemGroup>
52+
<ItemGroup>
53+
<ProjectReference Include="..\SharpRepository.Repository\SharpRepository.Repository.csproj">
54+
<Project>{710DEE79-25CE-4F68-B8B1-D08A135AD154}</Project>
55+
<Name>SharpRepository.Repository</Name>
56+
</ProjectReference>
57+
</ItemGroup>
58+
<ItemGroup>
59+
<None Include="packages.config" />
60+
</ItemGroup>
61+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
62+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
63+
Other similar extension points exist, see Microsoft.Common.targets.
64+
<Target Name="BeforeBuild">
65+
</Target>
66+
<Target Name="AfterBuild">
67+
</Target>
68+
-->
69+
</Project>
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="db4o-devel" version="8.1.184.15492" />
4+
</packages>

SharpRepository.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
3030
EndProject
3131
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpRepository.InMemoryRepository", "SharpRepository.InMemoryRepository\SharpRepository.InMemoryRepository.csproj", "{13AD3FCA-4C9D-4674-B786-F30843638D3B}"
3232
EndProject
33-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpRepository.Db4o", "SharpRepository.Sqlite\SharpRepository.Db4o.csproj", "{12DD3830-5A12-484C-98B4-D30811DEC8A9}"
33+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpRepository.Db4o", "SharpRepository.Db4o\SharpRepository.Db4o.csproj", "{12DD3830-5A12-484C-98B4-D30811DEC8A9}"
3434
EndProject
3535
Global
3636
GlobalSection(SolutionConfigurationPlatforms) = preSolution

0 commit comments

Comments
 (0)