Created
October 10, 2013 13:01
-
-
Save saintc0d3r/6917937 to your computer and use it in GitHub Desktop.
[Ninject][ASP NET Web API] A demonstration of Dependency injection on ASP NET Web API's controller using Ninject & Dependency Resolver.
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.Dependencies; | |
namespace Xtremecode.Infrastructure.Library.Di | |
{ | |
public class DependencyResolver : DependencyScope, IDependencyResolver | |
{ | |
public DependencyResolver() : base(DependencyInjector.GetKernel()) { } | |
public IDependencyScope BeginScope() | |
{ | |
return new DependencyScope(DependencyInjector.BeginBlock()); | |
} | |
} | |
} |
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.Http.Dependencies; | |
using Ninject.Parameters; | |
using Ninject.Syntax; | |
namespace Xtremecode.Infrastructure.Library.Di | |
{ | |
public class DependencyScope : IDependencyScope | |
{ | |
public DependencyScope(IResolutionRoot resolutionRoot) | |
{ | |
ResolutionRoot = resolutionRoot; | |
} | |
protected IResolutionRoot ResolutionRoot { get; set; } | |
public void Dispose() | |
{ | |
var disposable = ResolutionRoot as IDisposable; | |
if (disposable != null) | |
{ | |
disposable.Dispose(); | |
} | |
} | |
public object GetService(Type serviceType) | |
{ | |
return resolveType(serviceType).SingleOrDefault(); | |
} | |
public IEnumerable<object> GetServices(Type serviceType) | |
{ | |
return resolveType(serviceType).ToArray(); | |
} | |
private IEnumerable<object> resolveType(Type serviceType) | |
{ | |
var request = ResolutionRoot.CreateRequest(serviceType, null, new IParameter[0], true, true); | |
return ResolutionRoot.Resolve(request); | |
} | |
} | |
} |
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; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
using Xtreme.Gaming.Strore.Web.Controllers; | |
using DependencyResolver = Xtremecode.Infrastructure.Library.Di.DependencyResolver; | |
namespace Xtreme.Gaming.Store.Web.Spa | |
{ | |
// Note: For instructions on enabling IIS6 or IIS7 classic mode, | |
// visit http://go.microsoft.com/?LinkId=9394801 | |
public class MvcApplication : System.Web.HttpApplication | |
{ | |
protected void Application_Start() | |
{ | |
AreaRegistration.RegisterAllAreas(); | |
WebApiConfig.Register(GlobalConfiguration.Configuration); | |
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); | |
RouteConfig.RegisterRoutes(RouteTable.Routes); | |
ControllerBuilder.Current.SetControllerFactory(new ControllerFactory()); | |
// The part that do the magic on the Web Api's Controllers | |
GlobalConfiguration.Configuration.DependencyResolver = new DependencyResolver(); | |
// Web API's Trick #1: Force the any controller's actions to return JSON string | |
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear(); | |
} | |
} | |
} |
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.Collections.Generic; | |
using System.Web.Http; | |
using Xtreme.Gaming.Store.Domain.Models; | |
using Xtreme.Gaming.Store.Domain.Services; | |
namespace Xtreme.Gaming.Store.Web.Spa.Controllers | |
{ | |
public class ProductController : ApiController | |
{ | |
private readonly IProductService _productService; | |
public ProductController(IProductService productService) | |
{ | |
_productService = productService; | |
} | |
// | |
// GET: /SpecialOffers | |
[AcceptVerbs("GET")] | |
public IEnumerable<Product> SpecialOffers() | |
{ | |
// Get a list of Products which are offered as current discounted items | |
var getSpecialOffersResponse = _productService.GetSpecialOffers(); | |
return getSpecialOffersResponse.IsSuccess ? getSpecialOffersResponse.Products : new Product[0]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment