Skip to content

Instantly share code, notes, and snippets.

@danielplawgo
Created June 1, 2018 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielplawgo/1e5cc12308ca336dbe375593343b7c98 to your computer and use it in GitHub Desktop.
Save danielplawgo/1e5cc12308ca336dbe375593343b7c98 to your computer and use it in GitHub Desktop.
Wykorzystanie modułów do konfiguracji kontenera Autofac
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;
}
}
public class ControllersModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterControllers(typeof(AutofacConfig).Assembly);
}
}
public class DataContextModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<DataContext>()
.InstancePerRequest();
}
}
public class DefaultModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterAssemblyTypes(typeof(DefaultModule).Assembly)
.AsImplementedInterfaces()
.EnableInterfaceInterceptors()
.InterceptedBy(typeof(LoggerInterceptor));
}
}
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}");
}
}
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