Skip to content

Commit 86ba55b

Browse files
author
Jeff Treuting
committed
Ioc Dependency Resolver
Now we can use a shared dbcontext across all EF5 repositories by having IOC control it's lifetime
1 parent 7a9f711 commit 86ba55b

129 files changed

Lines changed: 155329 additions & 21 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

SharpRepository.Ef5Repository/Ef5ConfigRepositoryFactory.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Data.Entity;
44
using SharpRepository.Repository;
55
using SharpRepository.Repository.Configuration;
6+
using SharpRepository.Repository.Ioc;
67

78
namespace SharpRepository.Ef5Repository
89
{
@@ -16,9 +17,9 @@ public Ef5ConfigRepositoryFactory(IRepositoryConfiguration config)
1617
public override IRepository<T, TKey> GetInstance<T, TKey>()
1718
{
1819
// check for required parameters
19-
if (String.IsNullOrEmpty(RepositoryConfiguration["connectionString"]))
20+
if (RepositoryDependencyResolver.Current == null && String.IsNullOrEmpty(RepositoryConfiguration["connectionString"]))
2021
{
21-
throw new ConfigurationErrorsException("The connectionString attribute is required in order to use the Ef5Repository via the configuration file.");
22+
throw new ConfigurationErrorsException("The connectionString attribute is required in order to use the Ef5Repository via the configuration file, unless you set the RepositoryDependencyResolver to use an Ioc container.");
2223
}
2324

2425
Type dbContextType = null;
@@ -31,9 +32,26 @@ public override IRepository<T, TKey> GetInstance<T, TKey>()
3132
var connectionString = RepositoryConfiguration["connectionString"];
3233

3334
// TODO: look at dbContextType (from Enyim.Caching configuration bits) and how it caches, see about implementing cache or expanding FastActivator to take parameters
34-
var dbContext = dbContextType == null ?
35-
new DbContext(connectionString) :
36-
(DbContext)Activator.CreateInstance(dbContextType, connectionString);
35+
DbContext dbContext = null;
36+
37+
// if there is an IOC dependency resolver configured then use that one to get the DbContext, this will allow sharing of context across multiple repositories if the IOC is configured that way
38+
if (RepositoryDependencyResolver.Current != null)
39+
{
40+
dbContext = dbContextType == null
41+
? RepositoryDependencyResolver.Current.Resolve<DbContext>()
42+
: (DbContext)RepositoryDependencyResolver.Current.Resolve(dbContextType);
43+
44+
// if the Ioc container doesn't throw an error but still returns null we need to alert the consumer
45+
if (dbContext == null)
46+
{
47+
throw new RepositoryDependencyResolverException(typeof(DbContext));
48+
}
49+
}
50+
51+
// the default way of getting a DbContext if there is no Ioc container setup
52+
dbContext = dbContextType == null
53+
? new DbContext(connectionString)
54+
: (DbContext) Activator.CreateInstance(dbContextType, connectionString);
3755

3856
return new Ef5Repository<T, TKey>(dbContext);
3957
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<packages>
3-
<package id="EntityFramework" version="5.0.0" targetFramework="net45" />
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="EntityFramework" version="5.0.0" targetFramework="net45" />
44
</packages>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using Autofac;
3+
using SharpRepository.Repository.Ioc;
4+
5+
namespace SharpRepository.Ioc.Autofac
6+
{
7+
public class AutofacDependencyResolver : BaseRepositoryDependencyResolver
8+
{
9+
private readonly IContainer _container;
10+
public AutofacDependencyResolver(IContainer container)
11+
{
12+
_container = container;
13+
}
14+
15+
protected override T ResolveInstance<T>()
16+
{
17+
return _container.Resolve<T>();
18+
}
19+
20+
protected override object ResolveInstance(Type type)
21+
{
22+
return _container.Resolve(type);
23+
}
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Autofac;
2+
using SharpRepository.Repository.Configuration;
3+
4+
namespace SharpRepository.Ioc.Autofac
5+
{
6+
public static class AutofacExtensions
7+
{
8+
public static void RegisterSharpRepository(this ContainerBuilder container, string repositoryName = null)
9+
{
10+
11+
}
12+
13+
public static void RegisterSharpRepository(this ContainerBuilder container, ISharpRepositoryConfiguration configuration)
14+
{
15+
16+
}
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace SharpRepository.Ioc.Autofac
8+
{
9+
public class Class1
10+
{
11+
}
12+
}
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("SharpRepository.Ioc.Autofac")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("SharpRepository.Ioc.Autofac")]
13+
[assembly: AssemblyCopyright("Copyright © 2013")]
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("1a70eee1-62eb-44b7-a754-f9413c6ddc6e")]
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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{14E6AF4A-B613-418F-B046-D84074963C0F}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>SharpRepository.Ioc.Autofac</RootNamespace>
11+
<AssemblyName>SharpRepository.Ioc.Autofac</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="Autofac">
34+
<HintPath>..\packages\Autofac.3.0.1\lib\net40\Autofac.dll</HintPath>
35+
</Reference>
36+
<Reference Include="Autofac.Configuration">
37+
<HintPath>..\packages\Autofac.3.0.1\lib\net40\Autofac.Configuration.dll</HintPath>
38+
</Reference>
39+
<Reference Include="System" />
40+
<Reference Include="System.Core" />
41+
</ItemGroup>
42+
<ItemGroup>
43+
<Compile Include="AutofacDependencyResolver.cs" />
44+
<Compile Include="AutofacExtensions.cs" />
45+
<Compile Include="Properties\AssemblyInfo.cs" />
46+
</ItemGroup>
47+
<ItemGroup>
48+
<None Include="packages.config" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<ProjectReference Include="..\SharpRepository.Repository\SharpRepository.Repository.csproj">
52+
<Project>{710DEE79-25CE-4F68-B8B1-D08A135AD154}</Project>
53+
<Name>SharpRepository.Repository</Name>
54+
</ProjectReference>
55+
</ItemGroup>
56+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
57+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
58+
Other similar extension points exist, see Microsoft.Common.targets.
59+
<Target Name="BeforeBuild">
60+
</Target>
61+
<Target Name="AfterBuild">
62+
</Target>
63+
-->
64+
</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="Autofac" version="3.0.1" targetFramework="net45" />
4+
</packages>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace SharpRepository.Ioc.Ninject
8+
{
9+
public class Class1
10+
{
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace SharpRepository.Ioc.Ninject
8+
{
9+
public class Factory
10+
{
11+
}
12+
}

0 commit comments

Comments
 (0)