I recently studied the castle open-source project and learned some simple understanding of startable facility in the afternoon,
Because Castle has a relatively small amount of learning materials and most of them are in English, some Chinese materials are self-learning,
Therefore, I hope to learn about him in a simple and easy-to-understand way, so there will inevitably be some incomplete points.
Facility is an extension unit of the castle container and is injection-oriented. Simply put, you need to inject the required functions without changing the original components.Code,
Facility is equivalent to the "Parasite" of the original component. The startable facility introduced in this article is a very simple facility built by Castle,
This facility can automatically execute code when the original component is created and destroyed.
First, we need to establish this "Parasite" class teststart, which inherits istartable and defines the start and stop methods.
Public class teststart: istartable
{
Public teststart ()
{
}
Public void start ()
{
MessageBox. Show ("testetsete ");
}
Public void stop ()
{
... Omitted
}
}
Then, load the "Parasite" into the container
Public class app
{
[Stathread]
Public static void main ()
{
Iwindsorcontainer Container = new windsorcontainer ();
Container. addfacility ("startable", new startablefacility ());
Container. addcomponent ("test", typeof (teststart ));
}
}
In this way, a window will pop up when you are running, and the START () method of teststart will be executed.