Skip to content

Commit 29af29d

Browse files
author
Jeff Treuting
committed
More progress on configuration files
Added for RavenDb, MongoDb, Ef and Ef5 EF has a dbContextType where you can set the specific context type you are using. Like in our integration we are using TestObjectEntities.
1 parent dd5a0b4 commit 29af29d

20 files changed

Lines changed: 356 additions & 122 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.Configuration;
4+
using System.Data.Entity;
5+
using SharpRepository.Repository;
6+
using SharpRepository.Repository.Configuration;
7+
8+
namespace SharpRepository.Ef5Repository
9+
{
10+
public class Config : ConfigurationSection, IRepositoryElement
11+
{
12+
[ConfigurationProperty("name", IsRequired = true)]
13+
public string Name
14+
{
15+
get { return (string)base["name"]; }
16+
set { base["name"] = value; }
17+
}
18+
19+
[ConfigurationProperty("connectionString", IsRequired = true)]
20+
public string ConnectionString
21+
{
22+
get { return (string)base["connectionString"]; }
23+
set { base["connectionString"] = value; }
24+
}
25+
26+
/// <summary>
27+
/// Gets or sets the type of the specific DbContext type.
28+
/// </summary>
29+
[ConfigurationProperty("dbContextType"), TypeConverter(typeof(TypeNameConverter))]
30+
public Type DbContextType
31+
{
32+
get { return (Type)base["dbContextType"]; }
33+
set
34+
{
35+
base["dbContextType"] = value;
36+
}
37+
}
38+
39+
public IRepository<T, TKey> GetInstance<T, TKey>() where T : class, new()
40+
{
41+
// TODO: look at FastActivator (from Enyim.Caching configuratio bits) and how it caches, see about implementing cache or expanding FastActivator to take parameters
42+
var dbContext = DbContextType == null ?
43+
new DbContext(ConnectionString) :
44+
(DbContext)Activator.CreateInstance(DbContextType, ConnectionString);
45+
46+
return new Ef5Repository<T, TKey>(dbContext);
47+
}
48+
}
49+
}

SharpRepository.Ef5Repository/Configuration/Ef5RepositorySection.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.

SharpRepository.Ef5Repository/SharpRepository.Ef5Repository.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<Compile Include="..\CommonAssembly.cs">
5151
<Link>Properties\CommonAssembly.cs</Link>
5252
</Compile>
53-
<Compile Include="Configuration\Ef5RepositorySection.cs" />
53+
<Compile Include="Config.cs" />
5454
<Compile Include="Ef5Repository.cs" />
5555
<Compile Include="Ef5RepositoryBase.cs" />
5656
<Compile Include="Properties\AssemblyInfo.cs" />
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.Configuration;
4+
using System.Data.Entity;
5+
using SharpRepository.Repository;
6+
using SharpRepository.Repository.Configuration;
7+
8+
namespace SharpRepository.EfRepository
9+
{
10+
public class Config : ConfigurationSection, IRepositoryElement
11+
{
12+
[ConfigurationProperty("name", IsRequired = true)]
13+
public string Name
14+
{
15+
get { return (string)base["name"]; }
16+
set { base["name"] = value; }
17+
}
18+
19+
[ConfigurationProperty("connectionString", IsRequired = true)]
20+
public string ConnectionString
21+
{
22+
get { return (string)base["connectionString"]; }
23+
set { base["connectionString"] = value; }
24+
}
25+
26+
/// <summary>
27+
/// Gets or sets the type of the specific DbContext type.
28+
/// </summary>
29+
[ConfigurationProperty("dbContextType"), TypeConverter(typeof(TypeNameConverter))]
30+
public Type DbContextType
31+
{
32+
get { return (Type)base["dbContextType"]; }
33+
set
34+
{
35+
base["dbContextType"] = value;
36+
}
37+
}
38+
39+
public IRepository<T, TKey> GetInstance<T, TKey>() where T : class, new()
40+
{
41+
// TODO: look at FastActivator (from Enyim.Caching configuratio bits) and how it caches, see about implementing cache or expanding FastActivator to take parameters
42+
var dbContext = DbContextType == null ?
43+
new DbContext(ConnectionString) :
44+
(DbContext)Activator.CreateInstance(DbContextType, ConnectionString);
45+
46+
return new EfRepository<T, TKey>(dbContext);
47+
}
48+
}
49+
}

SharpRepository.EfRepository/SharpRepository.EfRepository.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@
3535
<HintPath>..\packages\EntityFramework.4.3.1\lib\net40\EntityFramework.dll</HintPath>
3636
</Reference>
3737
<Reference Include="System" />
38+
<Reference Include="System.Configuration" />
3839
<Reference Include="System.Core" />
3940
<Reference Include="System.Data.Entity" />
4041
</ItemGroup>
4142
<ItemGroup>
4243
<Compile Include="..\CommonAssembly.cs">
4344
<Link>Properties\CommonAssembly.cs</Link>
4445
</Compile>
46+
<Compile Include="Config.cs" />
4547
<Compile Include="EfRepository.cs" />
4648
<Compile Include="EfRepositoryBase.cs" />
4749
<Compile Include="Properties\AssemblyInfo.cs" />

SharpRepository.InMemoryRepository/Configuration/InMemoryRepositorySection.cs renamed to SharpRepository.InMemoryRepository/Config.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
using SharpRepository.Repository;
33
using SharpRepository.Repository.Configuration;
44

5-
namespace SharpRepository.InMemoryRepository.Configuration
5+
namespace SharpRepository.InMemoryRepository
66
{
7-
public class InMemoryRepositorySection : ConfigurationSection, IRepositoryElement
7+
public class Config : ConfigurationSection, IRepositoryElement
88
{
99
[ConfigurationProperty("name", IsRequired = true)]
1010
public string Name

SharpRepository.InMemoryRepository/SharpRepository.InMemoryRepository.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<Compile Include="..\CommonAssembly.cs">
4040
<Link>Properties\CommonAssembly.cs</Link>
4141
</Compile>
42-
<Compile Include="Configuration\InMemoryRepositorySection.cs" />
42+
<Compile Include="Config.cs" />
4343
<Compile Include="InMemoryRepository.cs" />
4444
<Compile Include="InMemoryRepositoryBase.cs" />
4545
<Compile Include="Properties\AssemblyInfo.cs" />
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Configuration;
3+
using SharpRepository.Repository;
4+
using SharpRepository.Repository.Configuration;
5+
6+
namespace SharpRepository.MongoDbRepository
7+
{
8+
public class Config : ConfigurationSection, IRepositoryElement
9+
{
10+
[ConfigurationProperty("name", IsRequired = true)]
11+
public string Name
12+
{
13+
get { return (string)base["name"]; }
14+
set { base["name"] = value; }
15+
}
16+
17+
[ConfigurationProperty("url")]
18+
public string Url
19+
{
20+
get { return (string)base["url"]; }
21+
set { base["url"] = value; }
22+
}
23+
24+
public IRepository<T, TKey> GetInstance<T, TKey>() where T : class, new()
25+
{
26+
return !String.IsNullOrEmpty(Url) ? new MongoDbRepository<T, TKey>(Url) : new MongoDbRepository<T, TKey>();
27+
}
28+
}
29+
}

SharpRepository.MongoDbRepository/MongoDbRepositoryBase.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,26 @@ private void Initialize(MongoServer mongoServer = null)
4545
_database = _server.GetDatabase(DatabaseName);
4646
}
4747

48+
private MongoCollection<T> BaseCollection()
49+
{
50+
return _database.GetCollection<T>(TypeName);
51+
}
52+
4853
protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = null)
4954
{
50-
return _database.GetCollection<T>(TypeName).AsQueryable();
55+
return BaseCollection().AsQueryable();
5156
}
5257

5358
protected override T GetQuery(TKey key)
5459
{
5560
return IsValidKey(key)
56-
? _database.GetCollection<T>(TypeName).FindOne(Query.EQ("_id", new ObjectId(key.ToString())))
61+
? BaseCollection().FindOne(Query.EQ("_id", new ObjectId(key.ToString())))
5762
: default(T);
5863
}
5964

6065
protected override void AddItem(T entity)
6166
{
62-
_database.GetCollection<T>(TypeName).Insert(entity);
67+
BaseCollection().Insert(entity);
6368
}
6469

6570
protected override void DeleteItem(T entity)
@@ -69,13 +74,13 @@ protected override void DeleteItem(T entity)
6974

7075
if (IsValidKey(pkValue))
7176
{
72-
_database.GetCollection<T>(TypeName).Remove(Query.EQ("_id", new ObjectId(pkValue.ToString())));
77+
BaseCollection().Remove(Query.EQ("_id", new ObjectId(pkValue.ToString())));
7378
}
7479
}
7580

7681
protected override void UpdateItem(T entity)
7782
{
78-
_database.GetCollection<T>(TypeName).Save(entity);
83+
BaseCollection().Save(entity);
7984
}
8085

8186
protected override void SaveChanges()
Lines changed: 73 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,74 @@
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>{DC40FEBE-2E39-4CB8-AE11-DFE9F6865BD2}</ProjectGuid>
9-
<OutputType>Library</OutputType>
10-
<AppDesignerFolder>Properties</AppDesignerFolder>
11-
<RootNamespace>SharpRepository.MongoDbRepository</RootNamespace>
12-
<AssemblyName>SharpRepository.MongoDbRepository</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="MongoDB.Bson, Version=1.6.0.4624, Culture=neutral, PublicKeyToken=f686731cfb9cc103, processorArchitecture=MSIL">
35-
<SpecificVersion>False</SpecificVersion>
36-
<HintPath>..\packages\mongocsharpdriver.1.6\lib\net35\MongoDB.Bson.dll</HintPath>
37-
</Reference>
38-
<Reference Include="MongoDB.Driver, Version=1.6.0.4624, Culture=neutral, PublicKeyToken=f686731cfb9cc103, processorArchitecture=MSIL">
39-
<SpecificVersion>False</SpecificVersion>
40-
<HintPath>..\packages\mongocsharpdriver.1.6\lib\net35\MongoDB.Driver.dll</HintPath>
41-
</Reference>
42-
<Reference Include="System" />
43-
<Reference Include="System.Core" />
44-
<Reference Include="System.Data" />
45-
</ItemGroup>
46-
<ItemGroup>
47-
<Compile Include="..\CommonAssembly.cs">
48-
<Link>CommonAssembly.cs</Link>
49-
</Compile>
50-
<Compile Include="MongoDbRepositoryManager.cs" />
51-
<Compile Include="Properties\AssemblyInfo.cs" />
52-
<Compile Include="MongoDbRepository.cs" />
53-
<Compile Include="MongoDbRepositoryBase.cs" />
54-
</ItemGroup>
55-
<ItemGroup>
56-
<ProjectReference Include="..\SharpRepository.Repository\SharpRepository.Repository.csproj">
57-
<Project>{710DEE79-25CE-4F68-B8B1-D08A135AD154}</Project>
58-
<Name>SharpRepository.Repository</Name>
59-
</ProjectReference>
60-
</ItemGroup>
61-
<ItemGroup>
62-
<None Include="packages.config" />
63-
</ItemGroup>
64-
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
65-
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
66-
Other similar extension points exist, see Microsoft.Common.targets.
67-
<Target Name="BeforeBuild">
68-
</Target>
69-
<Target Name="AfterBuild">
70-
</Target>
71-
-->
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>{DC40FEBE-2E39-4CB8-AE11-DFE9F6865BD2}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>SharpRepository.MongoDbRepository</RootNamespace>
12+
<AssemblyName>SharpRepository.MongoDbRepository</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="MongoDB.Bson, Version=1.6.0.4624, Culture=neutral, PublicKeyToken=f686731cfb9cc103, processorArchitecture=MSIL">
35+
<SpecificVersion>False</SpecificVersion>
36+
<HintPath>..\packages\mongocsharpdriver.1.6\lib\net35\MongoDB.Bson.dll</HintPath>
37+
</Reference>
38+
<Reference Include="MongoDB.Driver, Version=1.6.0.4624, Culture=neutral, PublicKeyToken=f686731cfb9cc103, processorArchitecture=MSIL">
39+
<SpecificVersion>False</SpecificVersion>
40+
<HintPath>..\packages\mongocsharpdriver.1.6\lib\net35\MongoDB.Driver.dll</HintPath>
41+
</Reference>
42+
<Reference Include="System" />
43+
<Reference Include="System.Configuration" />
44+
<Reference Include="System.Core" />
45+
<Reference Include="System.Data" />
46+
</ItemGroup>
47+
<ItemGroup>
48+
<Compile Include="..\CommonAssembly.cs">
49+
<Link>CommonAssembly.cs</Link>
50+
</Compile>
51+
<Compile Include="Config.cs" />
52+
<Compile Include="MongoDbRepositoryManager.cs" />
53+
<Compile Include="Properties\AssemblyInfo.cs" />
54+
<Compile Include="MongoDbRepository.cs" />
55+
<Compile Include="MongoDbRepositoryBase.cs" />
56+
</ItemGroup>
57+
<ItemGroup>
58+
<ProjectReference Include="..\SharpRepository.Repository\SharpRepository.Repository.csproj">
59+
<Project>{710DEE79-25CE-4F68-B8B1-D08A135AD154}</Project>
60+
<Name>SharpRepository.Repository</Name>
61+
</ProjectReference>
62+
</ItemGroup>
63+
<ItemGroup>
64+
<None Include="packages.config" />
65+
</ItemGroup>
66+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
67+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
68+
Other similar extension points exist, see Microsoft.Common.targets.
69+
<Target Name="BeforeBuild">
70+
</Target>
71+
<Target Name="AfterBuild">
72+
</Target>
73+
-->
7274
</Project>

0 commit comments

Comments
 (0)