Skip to content

Commit f1fd2e9

Browse files
author
Jeff Treuting
committed
Added Ioc projects for MVC and WebApi
They use DependencyResolver.Current and GlobalConfiguration.Configuration.DependencyResolver respectively
1 parent 83e5c97 commit f1fd2e9

62 files changed

Lines changed: 31625 additions & 4146 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.
Lines changed: 124 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,124 @@
1-
using System;
2-
using System.Collections.Concurrent;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using SharpRepository.Repository;
6-
using SharpRepository.Repository.Caching;
7-
using SharpRepository.Repository.FetchStrategies;
8-
9-
namespace SharpRepository.InMemoryRepository
10-
{
11-
public abstract class InMemoryRepositoryBase<T, TKey> : LinqRepositoryBase<T, TKey> where T : class, new()
12-
{
13-
private readonly ConcurrentDictionary<TKey, T> _items = new ConcurrentDictionary<TKey, T>();
14-
15-
internal InMemoryRepositoryBase(ICachingStrategy<T, TKey> cachingStrategy = null) : base(cachingStrategy)
16-
{
17-
}
18-
19-
protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = null)
20-
{
21-
return CloneDictionary(_items).AsQueryable();
22-
}
23-
24-
protected override T GetQuery(TKey key)
25-
{
26-
T result;
27-
_items.TryGetValue(key, out result);
28-
29-
return result;
30-
}
31-
32-
private static IEnumerable<T> CloneDictionary(ConcurrentDictionary<TKey, T> list)
33-
{
34-
// when you Google deep copy of generic list every answer uses either the IClonable interface on the T or having the T be Serializable
35-
// since we can't really put those constraints on T I'm going to do it via reflection
36-
37-
var type = typeof (T);
38-
var properties = type.GetProperties();
39-
40-
var clonedList = new List<T>(list.Count);
41-
42-
foreach (var keyValuePair in list)
43-
{
44-
var newItem = new T();
45-
foreach (var propInfo in properties)
46-
{
47-
propInfo.SetValue(newItem, propInfo.GetValue(keyValuePair.Value, null), null);
48-
}
49-
50-
clonedList.Add(newItem);
51-
}
52-
53-
return clonedList;
54-
}
55-
56-
protected override void AddItem(T entity)
57-
{
58-
TKey id;
59-
60-
if (GetPrimaryKey(entity, out id) && Equals(id, default(TKey)))
61-
{
62-
id = GeneratePrimaryKey();
63-
SetPrimaryKey(entity, id);
64-
}
65-
66-
_items[id] = entity;
67-
}
68-
69-
protected override void DeleteItem(T entity)
70-
{
71-
TKey pkValue;
72-
GetPrimaryKey(entity, out pkValue);
73-
74-
T tmp;
75-
_items.TryRemove(pkValue, out tmp);
76-
}
77-
78-
protected override void UpdateItem(T entity)
79-
{
80-
TKey pkValue;
81-
GetPrimaryKey(entity, out pkValue);
82-
83-
_items[pkValue] = entity;
84-
}
85-
86-
protected override void SaveChanges()
87-
{
88-
89-
}
90-
91-
public override void Dispose()
92-
{
93-
94-
}
95-
96-
private TKey GeneratePrimaryKey()
97-
{
98-
if (typeof(TKey) == typeof(Guid))
99-
{
100-
return (TKey)Convert.ChangeType(Guid.NewGuid(), typeof(TKey));
101-
}
102-
103-
if (typeof(TKey) == typeof(string))
104-
{
105-
return (TKey)Convert.ChangeType(Guid.NewGuid().ToString("N"), typeof(TKey));
106-
}
107-
108-
if (typeof(TKey) == typeof(Int32))
109-
{
110-
var pkValue = _items.Keys.LastOrDefault();
111-
112-
var nextInt = Convert.ToInt32(pkValue) + 1;
113-
return (TKey)Convert.ChangeType(nextInt, typeof(TKey));
114-
}
115-
116-
throw new InvalidOperationException("Primary key could not be generated. This only works for GUID, Int32 and String.");
117-
}
118-
119-
public override string ToString()
120-
{
121-
return "SharpRepository.InMemoryRepository";
122-
}
123-
}
124-
}
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using SharpRepository.Repository;
6+
using SharpRepository.Repository.Caching;
7+
using SharpRepository.Repository.FetchStrategies;
8+
9+
namespace SharpRepository.InMemoryRepository
10+
{
11+
public abstract class InMemoryRepositoryBase<T, TKey> : LinqRepositoryBase<T, TKey> where T : class, new()
12+
{
13+
private readonly ConcurrentDictionary<TKey, T> _items = new ConcurrentDictionary<TKey, T>();
14+
15+
internal InMemoryRepositoryBase(ICachingStrategy<T, TKey> cachingStrategy = null) : base(cachingStrategy)
16+
{
17+
}
18+
19+
protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = null)
20+
{
21+
return CloneDictionary(_items).AsQueryable();
22+
}
23+
24+
protected override T GetQuery(TKey key)
25+
{
26+
T result;
27+
_items.TryGetValue(key, out result);
28+
29+
return result;
30+
}
31+
32+
private static IEnumerable<T> CloneDictionary(ConcurrentDictionary<TKey, T> list)
33+
{
34+
// when you Google deep copy of generic list every answer uses either the IClonable interface on the T or having the T be Serializable
35+
// since we can't really put those constraints on T I'm going to do it via reflection
36+
37+
var type = typeof (T);
38+
var properties = type.GetProperties();
39+
40+
var clonedList = new List<T>(list.Count);
41+
42+
foreach (var keyValuePair in list)
43+
{
44+
var newItem = new T();
45+
foreach (var propInfo in properties)
46+
{
47+
propInfo.SetValue(newItem, propInfo.GetValue(keyValuePair.Value, null), null);
48+
}
49+
50+
clonedList.Add(newItem);
51+
}
52+
53+
return clonedList;
54+
}
55+
56+
protected override void AddItem(T entity)
57+
{
58+
TKey id;
59+
60+
if (GetPrimaryKey(entity, out id) && Equals(id, default(TKey)))
61+
{
62+
id = GeneratePrimaryKey();
63+
SetPrimaryKey(entity, id);
64+
}
65+
66+
_items[id] = entity;
67+
}
68+
69+
protected override void DeleteItem(T entity)
70+
{
71+
TKey pkValue;
72+
GetPrimaryKey(entity, out pkValue);
73+
74+
T tmp;
75+
_items.TryRemove(pkValue, out tmp);
76+
}
77+
78+
protected override void UpdateItem(T entity)
79+
{
80+
TKey pkValue;
81+
GetPrimaryKey(entity, out pkValue);
82+
83+
_items[pkValue] = entity;
84+
}
85+
86+
protected override void SaveChanges()
87+
{
88+
89+
}
90+
91+
public override void Dispose()
92+
{
93+
94+
}
95+
96+
private TKey GeneratePrimaryKey()
97+
{
98+
if (typeof(TKey) == typeof(Guid))
99+
{
100+
return (TKey)Convert.ChangeType(Guid.NewGuid(), typeof(TKey));
101+
}
102+
103+
if (typeof(TKey) == typeof(string))
104+
{
105+
return (TKey)Convert.ChangeType(Guid.NewGuid().ToString("N"), typeof(TKey));
106+
}
107+
108+
if (typeof(TKey) == typeof(Int32))
109+
{
110+
var pkValue = _items.Keys.LastOrDefault();
111+
112+
var nextInt = Convert.ToInt32(pkValue) + 1;
113+
return (TKey)Convert.ChangeType(nextInt, typeof(TKey));
114+
}
115+
116+
throw new InvalidOperationException("Primary key could not be generated. This only works for GUID, Int32 and String.");
117+
}
118+
119+
public override string ToString()
120+
{
121+
return "SharpRepository.InMemoryRepository";
122+
}
123+
}
124+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Web.Mvc;
3+
using SharpRepository.Repository.Ioc;
4+
5+
namespace SharpRepository.Ioc.Mvc
6+
{
7+
public class DependencyResolverWrapper : IRepositoryDependencyResolver
8+
{
9+
private readonly IDependencyResolver _resolver;
10+
11+
public DependencyResolverWrapper(IDependencyResolver resolver)
12+
{
13+
_resolver = resolver;
14+
}
15+
16+
public T Resolve<T>()
17+
{
18+
return _resolver.GetService<T>();
19+
}
20+
21+
public object Resolve(Type type)
22+
{
23+
return _resolver.GetService(type);
24+
}
25+
}
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Web.Mvc;
3+
using SharpRepository.Repository.Ioc;
4+
5+
namespace SharpRepository.Ioc.Mvc
6+
{
7+
public class MvcDependencyResolver : IRepositoryDependencyResolver
8+
{
9+
public T Resolve<T>()
10+
{
11+
return DependencyResolver.Current.GetService<T>();
12+
}
13+
14+
public object Resolve(Type type)
15+
{
16+
return DependencyResolver.Current.GetService(type);
17+
}
18+
}
19+
}
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.Mvc")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("SharpRepository.Ioc.Mvc")]
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("8fb0214c-ce76-4e93-8ee9-32204d259329")]
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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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>{1AA81ACD-74D6-4619-AC34-B3ED8B2035A8}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>SharpRepository.Ioc.Mvc</RootNamespace>
11+
<AssemblyName>SharpRepository.Ioc.Mvc</AssemblyName>
12+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<TargetFrameworkProfile />
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.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
37+
<Private>True</Private>
38+
<HintPath>..\packages\Microsoft.AspNet.Mvc.4.0.30506.0\lib\net40\System.Web.Mvc.dll</HintPath>
39+
</Reference>
40+
</ItemGroup>
41+
<ItemGroup>
42+
<Compile Include="DependencyResolverWrapper.cs" />
43+
<Compile Include="MvcDependencyResolver.cs" />
44+
<Compile Include="Properties\AssemblyInfo.cs" />
45+
</ItemGroup>
46+
<ItemGroup>
47+
<ProjectReference Include="..\SharpRepository.Repository\SharpRepository.Repository.csproj">
48+
<Project>{710dee79-25ce-4f68-b8b1-d08a135ad154}</Project>
49+
<Name>SharpRepository.Repository</Name>
50+
</ProjectReference>
51+
</ItemGroup>
52+
<ItemGroup>
53+
<None Include="packages.config" />
54+
</ItemGroup>
55+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
56+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
57+
Other similar extension points exist, see Microsoft.Common.targets.
58+
<Target Name="BeforeBuild">
59+
</Target>
60+
<Target Name="AfterBuild">
61+
</Target>
62+
-->
63+
</Project>

0 commit comments

Comments
 (0)