avalon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "hammett" <hamm...@uol.com.br>
Subject [Avalon.Net] Dependencies and extensions
Date Mon, 15 Sep 2003 03:06:25 GMT
I've been playing around with Avalon.Net this sunday and implemented (for
the simplest case)

- dependencies handling
- extensions

As my first intention, I centralized lifecycle handling in a common class.
This class expose events to anyone who wants to participate someway. That
has been said, I must tell you that was particulary easy to code a
PicoContainer style of dependency setting:

Begin PicoContainerExtension.cs

namespace Apache.Avalon.Container.Test.Module
{
 using System;
 using System.Reflection;

 using Apache.Avalon.Container.Extension;

 public class PicoContainerExtension : IExtensionModule
 {
  public PicoContainerExtension()
  {
  }

  #region IExtensionModule Members

  public void Init(LifecycleManager manager)
  {
   manager.SetupDependencies += new LifecycleHandler(OnSetupDependencies);
  }

  #endregion

  private void OnSetupDependencies(LifecycleEventArgs e)
  {
   object instance = e.Component;

   MethodInfo method = instance.GetType().GetMethod("set_" +
e.DependencyKey,
    BindingFlags.SetProperty|BindingFlags.Public|BindingFlags.Instance);

   if (method != null)
   {
    method.Invoke(instance, new object[] { e.DependencyInstance } );
   }
  }
 }
}

End PicoContainerExtension.cs



Follows Vehicle.cs:

namespace Apache.Avalon.Container.Test.Components
{
 using System;
 using NUnit.Framework;

 using Apache.Avalon.Framework;

 public interface IVehicle
 {
  IEngine Engine
  {
   get;
   set;
  }

  IRadio Radio
  {
   get;
   set;
  }
 }

 public interface IEngine
 {
  void TurnOn();

  void TurnOff();
 }

 public interface IRadio
 {
 }

 [AvalonService( typeof(IVehicle) )]
 [AvalonComponent( "Vehicle", Lifestyle.Transient )]
 [AvalonDependency( typeof(IEngine), "Engine", Optional.False)]
 [AvalonDependency( typeof(IRadio), "Radio", Optional.False)]
 public class Vehicle : IVehicle, ILookupEnabled
 {
  private IEngine m_engine;
  private IRadio  m_radio;

  public IEngine Engine
  {
   get
   {
    return m_engine;
   }
   set
   {
    m_engine = value;
   }
  }

  public IRadio Radio
  {
   get
   {
    return m_radio;
   }
   set
   {
    m_radio = value;
   }
  }

  #region ILookupEnabled Members

  public void EnableLookups(ILookupManager manager)
  {
   Assertion.AssertNotNull(manager);

   Assertion.AssertNotNull(manager["Engine"]);
   Assertion.AssertNotNull(manager["Radio"]);

   Assertion.Equals( typeof(IEngine), manager["Engine"].GetType() );
   Assertion.Equals( typeof(IRadio), manager["Radio"].GetType() );
  }

  #endregion
 }

 [AvalonService( typeof(IEngine) )]
 [AvalonComponent( "Engine", Lifestyle.Transient )]
 public class Engine : IEngine
 {
  #region IEngine Members

  public void TurnOn()
  {
  }

  public void TurnOff()
  {
  }

  #endregion
 }

 [AvalonService( typeof(IRadio) )]
 [AvalonComponent( "Radio", Lifestyle.Transient )]
 public class Radio : IRadio
 {
 }
}


There are other assumptions I've made that are very different from Fortress,
but let's discuss it in the next days.


Regards

hammett





---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@avalon.apache.org
For additional commands, e-mail: dev-help@avalon.apache.org


Mime
View raw message