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;
}
}