Monday, May 21, 2007

Referencing the Connection Monitor Service

Get a reference to the Connection Monitor service from another class

If you have stored an instance of the ConnectionMonitor class in your WorkItem, you can reference it from any other class within the scope of that WorkItem. You can use a [ServiceDependency] attribute on a parameter of the class constructor to accept an injected reference and store it in a class-level variable.
public class MyNewClass
{
private ConnectionMonitor sampleMonitor;

public MyNewClass([ServiceDependency] ConnectionMonitor cm)
{
this.sampleMonitor = cm;
}
...
}
Alternatively, you can expose a public property for the ConnectionMonitor and have ObjectBuilder set it to the ConnectionMonitor instance in the WorkItem.
public class MyNewClass
{
private ConnectionMonitor sampleMonitor;

[ServiceDependency]
public ConnectionMonitor ConnectionMonitor
{
get { return this.sampleMonitor; }
set { this.sampleMonitor = value; }
}
...
}
Another approach is to directly query the Services collection of the WorkItem to obtain a reference to the service you want.
public class MyNewClass
{
private ConnectionMonitor sampleMonitor;

public MyNewClass();
{
this.sampleMonitor = myWorkItem.Services.Get();
}
...
}

No comments: