UnityContainer + EventBroker

using Microsoft.Practices.Unity;
using EventBrokerExtension;

 // declare the container, the container using the eventBroker

using (IUnityContainer container = new UnityContainer())
        {
                container
                  .AddNewExtension<SimpleEventBrokerExtension>()
                  .RegisterType<T>(
                    new InjectionProperty("MdiParent", Program.MDIContainer),
                    new InjectionProperty("Text", title),                 
                    new InjectionConstructor(entity)
                    );
     
                childForm = container.Resolve<T>();
            }

= = =

T childForm = new T(entity);
childForm.MdiParent = Program.MDIContainer;
childForm.Text = title;

// the childForm has registered to the eventBroker

-----------------------------------------------------------------------------------------------------------------------------------
// declare the container using the eventBroker, the IUIController is a property of MainForm, UIController is a class.

// RegisterType<IUIController, UIController>() means assign the instance of UIController to IUIController. 
// normally not used.

using (IUnityContainer mainContainer = new UnityContainer()
{
    mainContainer
  .AddNewExtension<SimpleEventBrokerExtension>()
        .RegisterType<IUIController, UIController>();

 MDIContainer = mainContainer.Resolve<MainForm>();
 
 Application.Run(MDIContainer);
}

= = =

Mainform MDIContainer = new Mainform();

----------------------------------------------------------------------------------------------------------------------------------

//event broker

Class A
{
 [SubscribesTo("StringA")]
 public void handlerA (object sender,EventArgs e)
 {
  ........
 }
}

Class B
{
 //publish the event has two way/
 [Publishes("StringA")]
 public event EventHandler XXChange;

 //or //
 public event EventHandler XXChange;
 EventBroker eb = new EventBroker();
 eb.registerPublisher ("StringA",this,"XXChange") ;
 //

 
 // a function to fire the event

 public void fireEvent()
 {
  if(XXChange != null)
  {
   XXChange.Invoke(this,EventArgs.Empty)
  }
 }
}

Class C
{
 public void someFunction()
 {
  using (IUnityContainer container = new UnityContainer())
  {           
   container.AddNewExtension<SimpleEventBrokerExtension>();
   ClassA myClassA = container.Resolve<ClassA>();
   // can't not use  -- ClassA myClassA = new classA();    
   // it will not use the EventBroker
   
  }
 }
}

= = = = = = ----------------------------------------------------------------------------------

// normal way we write the event. (basic use)


Class M
{


 public M() 
 {
  this.xxClicked += new System.EventHandler(this.handleFunction);                //register the event and the handler function
 } 
 

public event EventHandler xxClicked;       // an event, like button Click
 

public void RaiseEvent()     // raise the event, when this function invoked, the event raised
{


    if (xxClicked != null)
  { 
   xxClicked(this,new EntityEventArgs(object a));   //  (EventArgs.Empty (no parameter))
   }


 }

 

public void handleFunction (object sender, EventArgs e)   // handler function for the event,
{
  var args = e as EntityEventArgs;     // cast the EventArgs to custom EventArgs

 

  if (args != null && EntityEventArgs.param != null)
  {
   // do something
  }
}

 

 // some function to invoke the RaiseEvent function, fire the event
public void FireEventFunction()
{
  ......
  ......
  RaiseEvent();      // fire the event
  ......
  ......
 }

 

}


// declare a custom EventArgs

public class EntityEventArgs : EventArgs
{
 object param = null; 

 public EntityEventArgs(object a)
        {
            param = a;
        }

}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值