Last active
August 29, 2015 14:17
-
-
Save gon250/da17337ff857688a54b2 to your computer and use it in GitHub Desktop.
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
namespace Solution.IoC | |
{ | |
public class DependencyInjectionProvider | |
{ | |
private IKernel _kernel; | |
private void SetUpDependencyInjection() | |
{ | |
//create _kernel | |
_kernel = new StandardKernel(); | |
//bindings | |
_kernel.Bind<IJsDateHelper>().To<JsDateHelper>(); | |
_kernel.Bind<IDbHelper>().To<DbHelper>(); | |
_kernel.Bind<IUserRepository>().To<UserRepository>(); | |
_kernel.Bind<ISiteRepository>().To<SiteRepository>(); | |
_kernel.Bind<IClientDetailsRepository>().To<ClientDetailsRepository>(); | |
_kernel.Bind<IFileDetailsRepository>().To<FileDetailsRepository>(); | |
_kernel.Bind<IPublicationRepository>().To<PublicationRepository>(); | |
} | |
public T Resolve<T>(System.Type service) { | |
return (T)_kernel.Get(service); | |
} | |
public void Dispose() | |
{ | |
_kernel.Dispose(); | |
} | |
public System.Web.Mvc.IDependencyResolver GetMvcDependencyResolver() | |
{ | |
SetUpDependencyInjection(); | |
return new MvcDependencyResolver(_kernel); | |
} | |
public System.Web.Http.Dependencies.IDependencyResolver GetHttpDependencyResolver() | |
{ | |
SetUpDependencyInjection(); | |
return new HttpDependencyResolver(_kernel); | |
} | |
} | |
} |
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
namespace Solution.CMS | |
{ | |
public class MvcApplication : System.Web.HttpApplication | |
{ | |
protected void Application_Start() | |
{ | |
AreaRegistration.RegisterAllAreas(); | |
var dependencyInjectionProvider = new DependencyInjectionProvider(); | |
// Set MVC depencendy resolver for asp mvc web project | |
DependencyResolver.SetResolver(dependencyInjectionProvider.GetMvcDependencyResolver()); | |
// Set MVC depencendy resolver for asp mvc web API project | |
GlobalConfiguration.Configuration.DependencyResolver = dependencyInjectionProvider.GetHttpDependencyResolver(); | |
// Remove the XM Formatter from the web api | |
GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter); | |
WebApiConfig.Register(GlobalConfiguration.Configuration); | |
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); | |
RouteConfig.RegisterRoutes(RouteTable.Routes); | |
} | |
} | |
} |
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
public class HttpDependencyResolver : IDependencyResolver | |
{ | |
private readonly IResolutionRoot _resolutionRoot; | |
public HttpDependencyResolver(IResolutionRoot kernel) | |
{ | |
this._resolutionRoot = kernel; | |
} | |
public object GetService(Type serviceType) | |
{ | |
return _resolutionRoot.TryGet(serviceType); | |
} | |
public IEnumerable<object> GetServices(Type serviceType) | |
{ | |
return _resolutionRoot.GetAll(serviceType); | |
} | |
public IDependencyScope BeginScope() | |
{ | |
// This example does not support child scopes, so we simply return 'this'. | |
return this; | |
} | |
public void Dispose() | |
{ | |
// When BeginScope returns 'this', the Dispose method must be a no-op. | |
} | |
} |
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
public class MvcDependencyResolver : IDependencyResolver | |
{ | |
private readonly IResolutionRoot _resolutionRoot; | |
public MvcDependencyResolver(IResolutionRoot kernel) | |
{ | |
this._resolutionRoot = kernel; | |
} | |
public object GetService(Type serviceType) | |
{ | |
return _resolutionRoot.TryGet(serviceType); | |
} | |
public IEnumerable<object> GetServices(Type serviceType) | |
{ | |
return _resolutionRoot.GetAll(serviceType); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment