Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 24th, 2012  |  syntax: None  |  size: 3.14 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. NHibernate: How to inject dependency on an entity
  2. public class Employee
  3. {
  4.     public int Id { get; set; }
  5.     public string Name { get; set; }
  6.  
  7.     public void SendNotification(string message, INotifier notifier)
  8.     {
  9.         notifier.SendMessage(string.Format("Message for customer '{0}' ({1}): {2}", Name, Id, message));
  10.     }
  11. }
  12.        
  13. public interface INotifier
  14. {
  15.     void SendMessage(string message);
  16. }
  17.  
  18. class EmailNotifier : INotifier
  19. {
  20.     public void SendMessage(string message)
  21.     {
  22.         // SmtpClient...
  23.     }
  24. }
  25.  
  26. class SMSNotifier : INotifier
  27. {
  28.     public void SendMessage(string message)
  29.     {
  30.         // SMS ...
  31.     }
  32. }
  33.        
  34. public class NotificationCommandHandler : ICommandHandler<NotificationCommand>
  35. {
  36.     private readonly INotifier _notifier;
  37.  
  38.     public NotificationCommandHandler(INotifier notifier)
  39.     {
  40.         _notifier = notifier;
  41.     }
  42.  
  43.     public void Execute(NotificationCommand commandMessage)
  44.     {
  45.         commandMessage.Employee.SendNotification(commandMessage.Message, _notifier);
  46.     }
  47. }
  48.  
  49. public class NotificationCommand
  50. {
  51.     public string Message { get; set; }
  52.     public Employee Employee { get; set; }
  53. }
  54.        
  55. public class Controller
  56. {
  57.     private readonly IMessageProcessor _messageProcessor;
  58.  
  59.     public Controller(IMessageProcessor messageProcessor)
  60.     {
  61.         _messageProcessor = messageProcessor;
  62.     }
  63.  
  64.     public void SendNotification (Employee employee, string message)
  65.     {
  66.         var sendMailCommand = new NotificationCommand
  67.         {
  68.             Employee = employee,
  69.             Message = message
  70.         };
  71.  
  72.         _messageProcessor.Process(sendMailCommand);
  73.     }
  74. }
  75.        
  76. public class DependencyInjectionEntityInterceptor : EmptyInterceptor
  77. {
  78.     IContainer _container;
  79.     ISession _session;
  80.  
  81.     public DependencyInjectionEntityInterceptor(IContainer container)
  82.     {
  83.         _container = container;            
  84.     }
  85.  
  86.     public override void SetSession(ISession session)
  87.     {
  88.        _session = session;            
  89.     }
  90.  
  91.     public override object Instantiate(string clazz, EntityMode entityMode, object id)
  92.     {
  93.         if (entityMode == EntityMode.Poco)
  94.         {
  95.             var type = Assembly.GetAssembly(typeof (SomeClass)).GetTypes().FirstOrDefault(x => x.FullName == clazz);
  96.             var hasParameters = type.GetConstructors().Any(x => x.GetParameters().Any());
  97.             if (type != null && hasParameters)
  98.             {
  99.                 var instance = _container.GetInstance(type);
  100.  
  101.                 var md = _session.SessionFactory.GetClassMetadata(clazz);
  102.                 md.SetIdentifier(instance, id, entityMode);
  103.                 return instance;
  104.             }
  105.         }
  106.         return base.Instantiate(clazz, entityMode, id);
  107.     }
  108. }
  109.        
  110. public FluentConfiguration GetFluentConfiguration(IContainer container)
  111. {
  112.     return Fluently.Configure()
  113.         .Database(MsSqlConfiguration.MsSql2008
  114.                   .ConnectionString(c => c.FromConnectionStringWithKey("Database"))
  115.                       .ShowSql())
  116.         .Mappings(m =>
  117.             m.AutoMappings.Add(AutoMap.AssemblyOf<SomeClass>()))
  118.         .ExposeConfiguration(x =>
  119.             x.SetInterceptor(new DependencyInjectionEntityInterceptor(container)));                
  120. }