Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.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)); 
    } 
like image 898
zhanwu Avatar asked Dec 19 '12 00:12

zhanwu


People also ask

What is component in c#?

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.

What is component class in vb net?

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.

What is .NET component class?

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.

What is the component of .NET core?

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.


1 Answers

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)); 
} 
like image 101
zhanwu Avatar answered Nov 14 '22 22:11

zhanwu