Last active
November 30, 2016 16:32
-
-
Save lira92/2e797b8504761bb1ade1fb3240a3358a to your computer and use it in GitHub Desktop.
Global file
This file contains hidden or 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
[RemotingService] | |
public class BancosController | |
{ | |
private IBancoRepository _bancoRepository { get; set; } | |
public BancosController(IBancoRepository bancoRepository) | |
{ | |
_bancoRepository = bancoRepository; | |
} | |
} |
This file contains hidden or 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 NewService.Domain.Interfaces.Repositories; | |
using NewService.Infra.Data.Repositories; | |
using ServiceWeb.AutoMapper; | |
using SimpleInjector; | |
using SimpleInjector.Advanced; | |
using System; | |
using System.ComponentModel.Composition; | |
using Microsoft.Web.Infrastructure.DynamicModuleHelper; | |
using System.Reflection; | |
using System.Web.UI; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Compilation; | |
using SimpleInjector.Diagnostics; | |
[assembly: PreApplicationStartMethod( | |
typeof(ServiceWeb.PageInitializerModule), | |
"Initialize")] | |
namespace ServiceWeb | |
{ | |
public sealed class PageInitializerModule : IHttpModule | |
{ | |
public static void Initialize() | |
{ | |
DynamicModuleUtility.RegisterModule(typeof(PageInitializerModule)); | |
} | |
void IHttpModule.Init(HttpApplication context) | |
{ | |
context.PreRequestHandlerExecute += (sender, e) => { | |
var handler = context.Context.CurrentHandler; | |
if (handler != null) | |
{ | |
string name = handler.GetType().Assembly.FullName; | |
if (!name.StartsWith("System.Web") && | |
!name.StartsWith("Microsoft")) | |
{ | |
Global.InitializeHandler(handler); | |
} | |
} | |
}; | |
} | |
void IHttpModule.Dispose() { } | |
} | |
public class Global : System.Web.HttpApplication | |
{ | |
private static Container container; | |
public static void InitializeHandler(IHttpHandler handler) | |
{ | |
container.GetRegistration(handler.GetType(), true).Registration | |
.InitializeInstance(handler); | |
} | |
protected void Application_Start(object sender, EventArgs e) | |
{ | |
AutoMapperConfig.RegisterMappings(); | |
// 1. Create a new Simple Injector container | |
var container = new Container(); | |
container.Options.PropertySelectionBehavior = new ImportAttributePropertySelectionBehavior(); | |
// 2. Configure the container (register) | |
container.Register<IConfiguracaoChequeRepository, ConfiguracaoChequeRepository>(Lifestyle.Transient); | |
container.Register<IBancoRepository, BancoRepository>(Lifestyle.Transient); | |
//container.Register<ILogger, MailLogger>(Lifestyle.Singleton); | |
RegisterWebPages(container); | |
// 3. Store the container for use by Page classes. | |
Global.container = container; | |
// 3. Optionally verify the container's configuration. | |
container.Verify(); | |
// 4. Register the container as MVC3 IDependencyResolver. | |
//DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container)); | |
} | |
private static void RegisterWebPages(Container container) | |
{ | |
var pageTypes = | |
from assembly in BuildManager.GetReferencedAssemblies().Cast<Assembly>() | |
where !assembly.IsDynamic | |
where !assembly.GlobalAssemblyCache | |
from type in assembly.GetExportedTypes() | |
where type.IsSubclassOf(typeof(Page)) | |
where !type.IsAbstract && !type.IsGenericType | |
select type; | |
foreach (Type type in pageTypes) | |
{ | |
var registration = Lifestyle.Transient.CreateRegistration(type, container); | |
registration.SuppressDiagnosticWarning( | |
DiagnosticType.DisposableTransientComponent, | |
"ASP.NET creates and disposes page classes for us."); | |
container.AddRegistration(type, registration); | |
} | |
} | |
class ImportAttributePropertySelectionBehavior : IPropertySelectionBehavior | |
{ | |
public bool SelectProperty(Type serviceType, PropertyInfo propertyInfo) | |
{ | |
// Makes use of the System.ComponentModel.Composition assembly | |
return typeof(Page).IsAssignableFrom(serviceType) && | |
propertyInfo.GetCustomAttributes<ImportAttribute>().Any(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment