I’m making a framework (for internal use only) that has common code among 3 o 4 delphi database CRUD applications..
A common object of mi framework is a TContext,
TContext = class (IContext)
function DB: IDatabase;
function CurrentSettings: ISettings;
..
end;
that is passed to the initialization method of lots of other objects.. example (this will be application code):
TCustomer.Initialize(Context: IContext)
TProjectList.Initialize(Context: IContext)
..
Every application has some specific context functions (that only will be called from application code):
IApp1Context = interface (IContext)
procedure DoSomethingSpecificToApp1;
procedure DoOtherThing;
..
end;
So when I create a Context, Im creating a IApp1Context, and sending it to the initialization methods.. from the framework code everything is fine, the problem is that from the application code I’m constantly casting from IContext to IApp1Context to access the specific
App1 functions.. so all my application code looks like (and its a lot of code like this):
(FContext as IApp1Context).DoSomethingSpecificToApp1
(FContext as IApp1Context).DoOtherThing;
..
The thing is clearly usable, but doesnt reads well in my opinion. Maybe I’m exaggerating; is there is a clever design technique for this situation that I’m not aware of?
Use a temporary variable. Assign it once at the start of the method, and then use it where you need it.
Or, since it looks like your
IContextobject is a field of an object, make yourIApp1Contextvariable be a field of the same object. It could even replace theIContextfield sinceIApp1Contextalready exposes everything theIContextdoes.