Reputation:
I have the following method in public abstract A : IVirtualMachineExporter
:
public override void Prepare() { ... }
I call the Prepare
from another class B
:
public sealed class B
{
public new ExportJobRequest Request { get { return (ExportJobRequest)base.Request; } }
private void ExportTask()
{
IVirtualMachineExporter exporter = CreateExporter();
exporter.Prepare();
}
}
Request
containing public bool isAdHoc { get; set; }
. I want to use information from this property inside Prepare()
. How can I do this? I don't want to change Prepare()
signature.
Upvotes: 1
Views: 77
Reputation: 29282
It looks like B
depends on an IVirtualMachineExporter
, and the implementation of IVirtualMachineExporter
(A
) depends on a Request
.
B
shouldn't know anything about A
or what it depends on. It should only care about the IVirtualMachineExporter
interface and calling Prepare()
.
You could create it like this:
public abstract class A : IVirtualMachineExporter
{
private readonly ExportJobRequest _request;
public A(ExportJobRequest request)
{
_request = request;
}
public override void Prepare()
{
//Now Prepare() has access to the Request because
//it's contained within A, the class that actually needs it.
}
}
Similarly, pass an interface (not a concrete implementation) to the constructor for B
.
public sealed class B
{
private readonly IVirtualMachineExporter _exporter;
public B(IVirtualMachineExporter exporter)
{
_exporter = exporter;
}
private void ExportTask()
{
//Can this isAdhoc property be a property of IVirtualMachineExporter,
//or can the Request be a property? Will every implementation of the
//interface have a request?
//exporter.IsAdhoc = this.Request.isAdhoc;
_exporter.Prepare();
}
}
I don't have the specifics of your design. But if B is going to depend on an interface (IVirtualMachineExplorer
) then it shouldn't know or care about any of the inner details of any class that implements the interface.
Upvotes: 0
Reputation: 6503
In class A, expose a public property IsAdhoc.
Set the IsAdhoc property on class A, prior to calling Prepare from class B.
So...
Class A
public bool IsAdhoc { get; set; }
// Inside this method, do something based on the IsAdhoc property above.
public override void Prepare() { ... }
Class B
public sealed class B
{
public new ExportJobRequest Request { get { return (ExportJobRequest)base.Request; } }
private void ExportTask()
{
IVirtualMachineExporter exporter = CreateExporter();
exporter.IsAdhoc = this.Request.isAdhoc;
exporter.Prepare();
}
}
Or, you could pass the boolean value to the CreateExporter method, which could set it on the new exporter class via its constructor.
Upvotes: 0
Reputation: 152624
How can I do this without changing Prepare signature?
Well, somehow Prepare
needs an instance to call isAdHoc
on, so if you don't want to change the method signature, can you change the class or interface?
something like:
IVirtualMachineExporter exporter = CreateExporter(Request);
exporter.Prepare();
or
IVirtualMachineExporter exporter = CreateExporter();
exporter.Request = Request;
exporter.Prepare();
Upvotes: 2