Skip to content
This repository has been archived by the owner on Aug 16, 2018. It is now read-only.

Commit

Permalink
Use DependencyResolver, see sharparchitecture#61
Browse files Browse the repository at this point in the history
  • Loading branch information
cd21h committed Jun 3, 2015
1 parent 7b9c844 commit 79f9d80
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions Solutions/SharpArch.Web.Mvc/ModelBinder/GenericRepositoryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@ namespace SharpArch.Web.Mvc.ModelBinder
using System;
using System.Web.Mvc;

using SharpArch.Domain.PersistenceSupport;
using Domain.PersistenceSupport;

internal class GenericRepositoryFactory
{
/// <summary>
/// Resolve repository for given entity type.
/// </summary>
/// <param name="entityType">Type of the entity.</param>
/// <param name="idType">Type of the identifier.</param>
/// <returns>Repository instance.</returns>
/// <exception cref="InvalidOperationException">If repository can not be resolved by <see cref="DependencyResolver"/>.</exception>
public static object CreateEntityRepositoryFor(Type entityType, Type idType)
{
var genericRepositoryType = typeof(IRepositoryWithTypedId<,>);
var concreteRepositoryType = genericRepositoryType.MakeGenericType(new[] { entityType, idType });
var genericRepositoryType = typeof (IRepositoryWithTypedId<,>);
var concreteRepositoryType = genericRepositoryType.MakeGenericType(entityType, idType);

object repository;

try
{
repository = DependencyResolver.Current.GetService(concreteRepositoryType);
}
catch (NullReferenceException)
{
throw new NullReferenceException(
"ServiceLocator has not been initialized; " + "I was trying to retrieve " + concreteRepositoryType);
}
var repository = DependencyResolver.Current.GetService(concreteRepositoryType);
if (repository == null)
throw new InvalidOperationException("Can not resolve " + concreteRepositoryType);

return repository;
}
Expand Down

0 comments on commit 79f9d80

Please sign in to comment.