Wykorzystanie modułów do konfiguracji kontenera Autofac
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 AutofacConfig | |
{ | |
public static IContainer Configure() | |
{ | |
var builder = new ContainerBuilder(); | |
builder.RegisterAssemblyModules(typeof(AutofacConfig).Assembly); | |
var container = builder.Build(); | |
DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); | |
return container; | |
} | |
} |
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 ControllersModule : Module | |
{ | |
protected override void Load(ContainerBuilder builder) | |
{ | |
builder.RegisterControllers(typeof(AutofacConfig).Assembly); | |
} | |
} |
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 DataContextModule : Module | |
{ | |
protected override void Load(ContainerBuilder builder) | |
{ | |
builder.RegisterType<DataContext>() | |
.InstancePerRequest(); | |
} | |
} |
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 DefaultModule : Module | |
{ | |
protected override void Load(ContainerBuilder builder) | |
{ | |
builder.RegisterAssemblyTypes(typeof(DefaultModule).Assembly) | |
.AsImplementedInterfaces() | |
.EnableInterfaceInterceptors() | |
.InterceptedBy(typeof(LoggerInterceptor)); | |
} | |
} |
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 LoggerModule : Module | |
{ | |
private static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger(); | |
protected override void Load(ContainerBuilder builder) | |
{ | |
builder.RegisterInstance(new LoggerInterceptor()); | |
} | |
protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, | |
IComponentRegistration registration) | |
{ | |
registration.Activating += OnActivating; | |
base.AttachToComponentRegistration(componentRegistry, registration); | |
} | |
private void OnActivating(object sender, ActivatingEventArgs<object> e) | |
{ | |
_logger.Info($"Activating: {e.Component.Activator.LimitType}"); | |
} | |
} |
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 ValidatorsModule : Module | |
{ | |
protected override void Load(ContainerBuilder builder) | |
{ | |
builder.RegisterAssemblyTypes(typeof(ValidatorsModule).Assembly) | |
.Where(t => t.GetInterfaces().Contains(typeof(IValidator))) | |
.InstancePerRequest(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment