Skip to content

Commit b81747a

Browse files
committed
Added unity ioc container
1 parent 0c5f097 commit b81747a

3 files changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
2+
<PropertyGroup>
3+
<TargetFrameworks>net451</TargetFrameworks>
4+
<Authors>Ben Griswold, Jeff Treuting</Authors>
5+
<Description>Configures Unity to resolve IRepository as the right configured implementation of SharpRepository</Description>
6+
<Summary>Written in C#, includes support for various relational, document and object databases including Entity Framework, RavenDB, MongoDB, CouchDB and Db4o. SharpRepository includes Xml and InMemory repository implementations as well. SharpRepository offers built-in caching options for AppFabric, memcached and the standard System.Runtime.Caching. SharpRepository also supports Specifications, FetchStrategies, Batches and Traits!</Summary>
7+
<PackageId>SharpRepository.Ioc.Autofac</PackageId>
8+
<PackageVersion>2.0.2-beta1</PackageVersion>
9+
<PackageReleaseNotes>2.0.2-beta1: registers all IRepository and ICompoundKeyRepository implementations, supports scoping</PackageReleaseNotes>
10+
<PackageTags>SharpRepository Repository IoC Autofac</PackageTags>
11+
<PackageIconUrl>https://user-images.githubusercontent.com/6349515/28491142-7b6350c4-6eeb-11e7-9c5b-e3b8ef1e73b8.png</PackageIconUrl>
12+
<PackageProjectUrl>https://github.com/SharpRepository/SharpRepository</PackageProjectUrl>
13+
<PackageLicenseUrl>https://raw.githubusercontent.com/SharpRepository/SharpRepository/master/license.txt</PackageLicenseUrl>
14+
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
15+
<RepositoryUrl>https://github.com/SharpRepository/SharpRepository.git</RepositoryUrl>
16+
<RepositoryType>git</RepositoryType>
17+
<Version>2.0.2-beta1</Version>
18+
</PropertyGroup>
19+
<ItemGroup>
20+
<PackageReference Include="Unity" Version="5.7.0" />
21+
</ItemGroup>
22+
<ItemGroup>
23+
<ProjectReference Include="..\SharpRepository.Repository\SharpRepository.Repository.csproj" />
24+
</ItemGroup>
25+
</Project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using SharpRepository.Repository;
4+
using SharpRepository.Repository.Configuration;
5+
using Unity;
6+
using Unity.Injection;
7+
using Unity.Lifetime;
8+
9+
namespace SharpRepository.Ioc.Unity
10+
{
11+
public static class UnityExtensions
12+
{
13+
/// <summary>
14+
/// Registers in unity container all IRepository and ICompoundKeyRepository resolutions.
15+
/// </summary>
16+
/// <param name="container"></param>
17+
/// <param name="configuration"></param>
18+
/// <param name="repositoryName"></param>
19+
/// <param name="lifetimeScopeTag">Accepts any MatchingScopeLifetimeTags scope enum tag</param>
20+
public static void RegisterSharpRepository(this IUnityContainer container, ISharpRepositoryConfiguration configuration, string repositoryName = null)
21+
{
22+
container.RegisterType(typeof(IRepository<>), new InjectionFactory((c, t, n) => RepositoryFactory.GetInstance(t.GetGenericArguments()[0], configuration, repositoryName)));
23+
container.RegisterType(typeof(IRepository<,>), new InjectionFactory((c, t, n) => RepositoryFactory.GetInstance(t.GetGenericArguments()[0], t.GetGenericArguments()[1], configuration, repositoryName)));
24+
container.RegisterType(typeof(ICompoundKeyRepository<,,>), new InjectionFactory((c, t, n) => RepositoryFactory.GetInstance(t.GetGenericArguments()[0], t.GetGenericArguments()[1], t.GetGenericArguments()[2], configuration, repositoryName)));
25+
container.RegisterType(typeof(ICompoundKeyRepository<,,,>), new InjectionFactory((c, t, n) => RepositoryFactory.GetInstance(t.GetGenericArguments()[0], t.GetGenericArguments()[1], t.GetGenericArguments()[2], t.GetGenericArguments()[3], configuration, repositoryName)));
26+
container.RegisterType(typeof(ICompoundKeyRepository<>), new InjectionFactory((c, t, n) => RepositoryFactory.GetInstance(t.GetGenericArguments()[0], configuration, repositoryName)));
27+
}
28+
}
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using SharpRepository.Repository.Ioc;
3+
using Unity;
4+
5+
namespace SharpRepository.Ioc.Unity
6+
{
7+
public class UnityRepositoryDependencyResolver : BaseRepositoryDependencyResolver
8+
{
9+
private readonly IUnityContainer _container;
10+
public UnityRepositoryDependencyResolver(IUnityContainer 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+
}

0 commit comments

Comments
 (0)