I am a new tester and while reading legacy code I had the following two classes:
public class TestCommon : Component
{
public void Initialize()
{
var serviceContainer = (IServiceContainer)this.GetService(typeof(TestFramework));
serviceContainer.AddService(typeof(TestCommon), this);
}
}
public class TestFramework : ISite, IServiceContainer
{
readonly Hashtable services = new Hashtable();
public TestFramework()
{
this.AddService(this);
var bedrockModuleInstance = (TestCommon)Activator.CreateInstance(typeof(TestCommon));
((TestCommon)bedrockModuleInstance).Site = this;
((TestCommon)bedrockModuleInstance).Initialize();
}
}
I don't understand why in the class TestCommon's Initialize method, one could call GetService and return somehow the TestFramework' GetService is invoked? I tried understand it by reading the MSDN about Container, Component and Site, but couldn't understand the idea of Site.
Update: Read the implementation of GetService, found that component's GetService acutally return its site's GetService, answered my question.
protected virtual object GetService(Type service) {
ISite s = site;
return((s== null) ? null : s.GetService(service));
}
Component is a class with cleanup and containment. A component can be hosted in a container, and has the ability to query and get services from its container. Containment is logical and does not have to be visual. These components can be deployed in middle tier container as business components.
A component is a class that implements the System. ComponentModel. IComponent interface or that derives directly or indirectly from a class that implements IComponent. A . NET component is an object that is reusable, can interact with other objects, and provides control over external resources and design-time support.
Specifically in . NET, a component is a class that implements the IComponent interface, which indicates that a class can interact with it's logical container. More often than not, you see this in the form of design support, in that classes interact with their host in the designer, but that's not a strict requirement.
NET Core application side by side with your application seamlessly. It is a general-purpose development platform that consists of several components. These include the managed compilers, the runtime, and the base class libraries. It also includes many application models, such as the ASP.NET Core.
Found the answer. Read the implementation of GetService, found that component's GetService acutally return its site's GetService, answered my question.
protected virtual object GetService(Type service) {
ISite s = site;
return((s== null) ? null : s.GetService(service));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With