Created
September 21, 2012 10:15
-
-
Save satish860/3760770 to your computer and use it in GitHub Desktop.
Controller Registry for Webapi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Http.Controllers; | |
using DependencyResolver.Controllers; | |
using Raven.Client; | |
using Raven.Client.Embedded; | |
using StructureMap.Configuration.DSL; | |
namespace DependencyResolver | |
{ | |
public class ControllerRegistry : Registry | |
{ | |
public ControllerRegistry() | |
{ | |
Scan(p => | |
{ | |
p.TheCallingAssembly(); | |
p.AddAllTypesOf<IHttpController>(); | |
}); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Http.Dependencies; | |
using StructureMap; | |
namespace DependencyResolver | |
{ | |
public class DependencyScope:IDependencyScope | |
{ | |
private readonly IContainer container; | |
public DependencyScope(IContainer container) | |
{ | |
this.container = container; | |
} | |
public object GetService(Type serviceType) | |
{ | |
return serviceType.IsAbstract || serviceType.IsInterface ? container.TryGetInstance(serviceType) | |
: container.GetInstance(serviceType); | |
} | |
public IEnumerable<object> GetServices(Type serviceType) | |
{ | |
return container.GetAllInstances(serviceType).Cast<object>(); | |
} | |
public void Dispose() | |
{ | |
if (container != null) | |
container.Dispose(); | |
} | |
} | |
public class DependencyResolver:DependencyScope,IDependencyResolver | |
{ | |
private readonly IContainer container; | |
public DependencyResolver(IContainer container): | |
base(container.GetNestedContainer()) | |
{ | |
this.container = container; | |
} | |
public IDependencyScope BeginScope() | |
{ | |
var NestedContainer = this.container.GetNestedContainer(); | |
return new DependencyScope(NestedContainer); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment