zhanwu
zhanwu

Reputation: 1528

what is a "site" of a "component" in .net?

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

Upvotes: 6

Views: 2252

Answers (1)

zhanwu
zhanwu

Reputation: 1528

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

Upvotes: 3

Related Questions