Skip to content

Commit

Permalink
Added Ioc projects for MVC and WebApi
Browse files Browse the repository at this point in the history
They use DependencyResolver.Current and
GlobalConfiguration.Configuration.DependencyResolver respectively
  • Loading branch information
Jeff Treuting committed Oct 10, 2013
1 parent 83e5c97 commit f1fd2e9
Show file tree
Hide file tree
Showing 62 changed files with 31,625 additions and 4,146 deletions.
248 changes: 124 additions & 124 deletions SharpRepository.InMemoryRepository/InMemoryRepositoryBase.cs
Original file line number Diff line number Diff line change
@@ -1,124 +1,124 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using SharpRepository.Repository;
using SharpRepository.Repository.Caching;
using SharpRepository.Repository.FetchStrategies;
namespace SharpRepository.InMemoryRepository
{
public abstract class InMemoryRepositoryBase<T, TKey> : LinqRepositoryBase<T, TKey> where T : class, new()
{
private readonly ConcurrentDictionary<TKey, T> _items = new ConcurrentDictionary<TKey, T>();
internal InMemoryRepositoryBase(ICachingStrategy<T, TKey> cachingStrategy = null) : base(cachingStrategy)
{
}
protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = null)
{
return CloneDictionary(_items).AsQueryable();
}
protected override T GetQuery(TKey key)
{
T result;
_items.TryGetValue(key, out result);
return result;
}
private static IEnumerable<T> CloneDictionary(ConcurrentDictionary<TKey, T> list)
{
// when you Google deep copy of generic list every answer uses either the IClonable interface on the T or having the T be Serializable
// since we can't really put those constraints on T I'm going to do it via reflection
var type = typeof (T);
var properties = type.GetProperties();
var clonedList = new List<T>(list.Count);
foreach (var keyValuePair in list)
{
var newItem = new T();
foreach (var propInfo in properties)
{
propInfo.SetValue(newItem, propInfo.GetValue(keyValuePair.Value, null), null);
}
clonedList.Add(newItem);
}
return clonedList;
}
protected override void AddItem(T entity)
{
TKey id;
if (GetPrimaryKey(entity, out id) && Equals(id, default(TKey)))
{
id = GeneratePrimaryKey();
SetPrimaryKey(entity, id);
}
_items[id] = entity;
}
protected override void DeleteItem(T entity)
{
TKey pkValue;
GetPrimaryKey(entity, out pkValue);
T tmp;
_items.TryRemove(pkValue, out tmp);
}
protected override void UpdateItem(T entity)
{
TKey pkValue;
GetPrimaryKey(entity, out pkValue);
_items[pkValue] = entity;
}
protected override void SaveChanges()
{
}
public override void Dispose()
{
}
private TKey GeneratePrimaryKey()
{
if (typeof(TKey) == typeof(Guid))
{
return (TKey)Convert.ChangeType(Guid.NewGuid(), typeof(TKey));
}
if (typeof(TKey) == typeof(string))
{
return (TKey)Convert.ChangeType(Guid.NewGuid().ToString("N"), typeof(TKey));
}
if (typeof(TKey) == typeof(Int32))
{
var pkValue = _items.Keys.LastOrDefault();
var nextInt = Convert.ToInt32(pkValue) + 1;
return (TKey)Convert.ChangeType(nextInt, typeof(TKey));
}
throw new InvalidOperationException("Primary key could not be generated. This only works for GUID, Int32 and String.");
}
public override string ToString()
{
return "SharpRepository.InMemoryRepository";
}
}
}
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using SharpRepository.Repository;
using SharpRepository.Repository.Caching;
using SharpRepository.Repository.FetchStrategies;

namespace SharpRepository.InMemoryRepository
{
public abstract class InMemoryRepositoryBase<T, TKey> : LinqRepositoryBase<T, TKey> where T : class, new()
{
private readonly ConcurrentDictionary<TKey, T> _items = new ConcurrentDictionary<TKey, T>();

internal InMemoryRepositoryBase(ICachingStrategy<T, TKey> cachingStrategy = null) : base(cachingStrategy)
{
}

protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = null)
{
return CloneDictionary(_items).AsQueryable();
}

protected override T GetQuery(TKey key)
{
T result;
_items.TryGetValue(key, out result);

return result;
}

private static IEnumerable<T> CloneDictionary(ConcurrentDictionary<TKey, T> list)
{
// when you Google deep copy of generic list every answer uses either the IClonable interface on the T or having the T be Serializable
// since we can't really put those constraints on T I'm going to do it via reflection

var type = typeof (T);
var properties = type.GetProperties();

var clonedList = new List<T>(list.Count);

foreach (var keyValuePair in list)
{
var newItem = new T();
foreach (var propInfo in properties)
{
propInfo.SetValue(newItem, propInfo.GetValue(keyValuePair.Value, null), null);
}

clonedList.Add(newItem);
}

return clonedList;
}

protected override void AddItem(T entity)
{
TKey id;

if (GetPrimaryKey(entity, out id) && Equals(id, default(TKey)))
{
id = GeneratePrimaryKey();
SetPrimaryKey(entity, id);
}

_items[id] = entity;
}

protected override void DeleteItem(T entity)
{
TKey pkValue;
GetPrimaryKey(entity, out pkValue);

T tmp;
_items.TryRemove(pkValue, out tmp);
}

protected override void UpdateItem(T entity)
{
TKey pkValue;
GetPrimaryKey(entity, out pkValue);

_items[pkValue] = entity;
}

protected override void SaveChanges()
{

}

public override void Dispose()
{

}

private TKey GeneratePrimaryKey()
{
if (typeof(TKey) == typeof(Guid))
{
return (TKey)Convert.ChangeType(Guid.NewGuid(), typeof(TKey));
}

if (typeof(TKey) == typeof(string))
{
return (TKey)Convert.ChangeType(Guid.NewGuid().ToString("N"), typeof(TKey));
}

if (typeof(TKey) == typeof(Int32))
{
var pkValue = _items.Keys.LastOrDefault();

var nextInt = Convert.ToInt32(pkValue) + 1;
return (TKey)Convert.ChangeType(nextInt, typeof(TKey));
}

throw new InvalidOperationException("Primary key could not be generated. This only works for GUID, Int32 and String.");
}

public override string ToString()
{
return "SharpRepository.InMemoryRepository";
}
}
}
26 changes: 26 additions & 0 deletions SharpRepository.Ioc.Mvc/DependencyResolverWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Web.Mvc;
using SharpRepository.Repository.Ioc;

namespace SharpRepository.Ioc.Mvc
{
public class DependencyResolverWrapper : IRepositoryDependencyResolver
{
private readonly IDependencyResolver _resolver;

public DependencyResolverWrapper(IDependencyResolver resolver)
{
_resolver = resolver;
}

public T Resolve<T>()
{
return _resolver.GetService<T>();
}

public object Resolve(Type type)
{
return _resolver.GetService(type);
}
}
}
19 changes: 19 additions & 0 deletions SharpRepository.Ioc.Mvc/MvcDependencyResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Web.Mvc;
using SharpRepository.Repository.Ioc;

namespace SharpRepository.Ioc.Mvc
{
public class MvcDependencyResolver : IRepositoryDependencyResolver
{
public T Resolve<T>()
{
return DependencyResolver.Current.GetService<T>();
}

public object Resolve(Type type)
{
return DependencyResolver.Current.GetService(type);
}
}
}
36 changes: 36 additions & 0 deletions SharpRepository.Ioc.Mvc/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SharpRepository.Ioc.Mvc")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SharpRepository.Ioc.Mvc")]
[assembly: AssemblyCopyright("Copyright © 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("8fb0214c-ce76-4e93-8ee9-32204d259329")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
63 changes: 63 additions & 0 deletions SharpRepository.Ioc.Mvc/SharpRepository.Ioc.Mvc.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{1AA81ACD-74D6-4619-AC34-B3ED8B2035A8}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SharpRepository.Ioc.Mvc</RootNamespace>
<AssemblyName>SharpRepository.Ioc.Mvc</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.Mvc.4.0.30506.0\lib\net40\System.Web.Mvc.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="DependencyResolverWrapper.cs" />
<Compile Include="MvcDependencyResolver.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SharpRepository.Repository\SharpRepository.Repository.csproj">
<Project>{710dee79-25ce-4f68-b8b1-d08a135ad154}</Project>
<Name>SharpRepository.Repository</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Loading

0 comments on commit f1fd2e9

Please sign in to comment.