Advertisement
NickJosevski

How to have only some PropertiesAutowired with Autofac while

May 31st, 2011
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Autofac;
  6. using Autofac.Core;
  7. using log4net;
  8.  
  9. namespace AutoFacLog4NetDemo
  10. {
  11.  
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             //setup
  17.             Console.WriteLine("Attempt to use a Module to configure ILog dependency");
  18.             var builder = new ContainerBuilder();
  19.  
  20.             builder.RegisterModule<LogInjectionModule>();
  21.             // that or ...
  22.             //builder.RegisterModule(new LogInjectionModule());
  23.             //neither worked...
  24.  
  25.             //TODO: is this registration correct?
  26.             /*builder.Register(c => new SomeClassWithILogDependency(c.Resolve<ILog>())
  27.                 ).As<IUseILog>().PropertiesAutowired();*/
  28.  
  29.  
  30.             builder.RegisterType<SomethingElse>().As<ISecond>();
  31.  
  32.             //NOTE: looks like this works:
  33.             //builder.RegisterType<SomeClassWithILogDependency>().As<IUseILog>().PropertiesAutowired();
  34.  
  35.  
  36.             builder.Register(c => new SomeClassWithILogDependency(c.ResolveOptional<ILog>(), c.Resolve<ISecond>()))
  37.                 .As<IUseILog>().PropertiesAutowired();
  38.  
  39.  
  40.             var container = builder.Build();
  41.  
  42.             //attempt to create instance
  43.  
  44.             var sc = container.Resolve<IUseILog>();
  45.  
  46.             try
  47.             {
  48.                 //attempt to use
  49.                 sc.UseILog();
  50.             }
  51.             catch (Exception ex)
  52.             {
  53.                 Console.WriteLine("It didn't work{0}exception: {1}", Environment.NewLine, ex.Message);
  54.             }
  55.  
  56.             Console.ReadKey();
  57.         }
  58.     }
  59.  
  60.     //NOTE: as per - http://code.google.com/p/autofac/wiki/Log4NetIntegration
  61.     public class LogInjectionModule : Module
  62.     {
  63.         protected override void AttachToComponentRegistration(IComponentRegistry registry, IComponentRegistration registration)
  64.         {
  65.             registration.Preparing += OnComponentPreparing;
  66.         }
  67.  
  68.         private static void OnComponentPreparing(object sender, PreparingEventArgs e)
  69.         {
  70.             var t = e.Component.Activator.LimitType;
  71.  
  72.             e.Parameters = e.Parameters.Union(new[]
  73.                                                   {
  74.                                                       new ResolvedParameter(
  75.                                                           (p, i) => p.ParameterType == typeof (ILog),
  76.                                                           (p, i) => LogManager.GetLogger(t))
  77.                                                   });
  78.         }
  79.  
  80.     }
  81.  
  82.     public interface IUseILog
  83.     {
  84.         void UseILog();
  85.     }
  86.  
  87.     public class SomeClassWithILogDependency : IUseILog
  88.     {
  89.         private readonly ILog _log;
  90.         private readonly ISecond _second;
  91.  
  92.         public SomeClassWithILogDependency(ILog log, ISecond second)
  93.         {
  94.             _log = log;
  95.             _second = second;
  96.  
  97.         }
  98.         public void UseILog()
  99.         {
  100.             try
  101.             {
  102.                 _second.ConfirmRunning();
  103.                 _log.InfoFormat("it possibly worked {0}", DateTime.Now);
  104.                 Console.WriteLine("it possibly worked...");
  105.             }
  106.             catch(Exception ex)
  107.             {
  108.                 Console.WriteLine("Error inside SomeClassWithILogDependency{0}exception:{1}",Environment.NewLine, ex.Message);
  109.             }
  110.         }
  111.     }
  112.  
  113.     public interface ISecond
  114.     {
  115.         void ConfirmRunning();
  116.     }
  117.  
  118.     public class SomethingElse : ISecond
  119.     {
  120.         public SomethingElse()
  121.         {
  122.            
  123.         }
  124.  
  125.         public void ConfirmRunning()
  126.         {
  127.             Console.WriteLine("inside SomethingElse");
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement