Created
February 10, 2010 21:38
-
-
Save tucaz/300878 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
/// <summary> | |
/// Factory de controllers utilizada no lugar da padrão | |
/// para usar o Unity pra resolver as dependências dos objetos | |
/// </summary> | |
public class UnityControllerFactory : DefaultControllerFactory | |
{ | |
readonly IUnityContainer _container; | |
public UnityControllerFactory(IUnityContainer container) | |
{ | |
_container = container; | |
} | |
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) | |
{ | |
if (controllerType == null) | |
throw new ArgumentNullException("controllerType"); | |
if (!typeof(IController).IsAssignableFrom(controllerType)) | |
throw new ArgumentException(string.Format( | |
"Type requested is not a controller: {0}", controllerType.Name), | |
"controllerType"); | |
return _container.Resolve(controllerType) as IController; | |
} | |
} | |
public interface IContainerAccessor | |
{ | |
IUnityContainer Container { get; } | |
} | |
public class MvcApplication : System.Web.HttpApplication, IContainerAccessor | |
{ | |
#region Injeção de dependência | |
private static UnityContainer _container; | |
public static IUnityContainer Container | |
{ | |
get { return _container; } | |
} | |
IUnityContainer IContainerAccessor.Container | |
{ | |
get { return Container; } | |
} | |
private static void InicializarDI() | |
{ | |
if (_container == null) | |
_container = new UnityContainer(); | |
RegistrarDependencias(); | |
IControllerFactory controllerFactory = new UnityControllerFactory(_container); | |
ControllerBuilder.Current.SetControllerFactory(controllerFactory); | |
} | |
/// <summary> | |
/// Registra no objeto do Unity as dependências dos objetos | |
/// </summary> | |
private static void RegistrarDependencias() | |
{ | |
_container | |
.RegisterType<IContaUsuarioService, ContaUsuarioService>() | |
.Configure<InjectedMembers>() | |
.ConfigureInjectionFor<UsuarioController>(new InjectionConstructor(typeof(IContaUsuarioService))); | |
_container | |
.RegisterType<IUsuarioRepository, UsuarioRepository>() | |
.Configure<InjectedMembers>() | |
.ConfigureInjectionFor<ContaUsuarioService>(new InjectionConstructor(typeof(IUsuarioRepository))); | |
} | |
#endregion | |
public static void RegisterRoutes(RouteCollection routes) | |
{ | |
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); | |
routes.MapRoute( | |
"Default", // Route name | |
"{controller}/{action}/{id}", // URL with parameters | |
new { controller = "Home", action = "Index", id = "" } // Parameter defaults | |
); | |
} | |
protected void Application_Start() | |
{ | |
AreaRegistration.RegisterAllAreas(); | |
RegisterRoutes(RouteTable.Routes); | |
//Chama o Unity para registrar as dependências dos objetos | |
InicializarDI(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment