J
Jeff Poste
Hi,
I'm developing software that requires business rules that
constantly need new versions for different quarters,
years, etc. I created a business rule factory that stores
these different business rules in a hashtable indexed by
the year, ie "3Q2003". Now based upon the key that I give
the factory, it will return a reference to the correct
business object for me to use. This works well as long as
all of the business objects in the factory implement the
same interface exactly.
If I want to extend a business rule by adding a method to
the "4Q2003" object, for example, my code breaks, since my
interface Type has no knowledge of the extra method in the
extended object reference, and hence can not call it.
I can, however, using reflection, get the fully qualified
name of the object, as it knows what it really is,
discover the method names on that object reference, and
actually call the extra method, but this is VERY
cumbersome. (I can send the code for this if necessary.)
It would be great if I could dymanically cast the returned
object reference from my factory to the extended class
represented by the fully qualified name that I can
retrieve from the object reference itself, then I could
just use my extended object.
An example of the problem in a code snippet is:
if (rs == "4Q2003")
{
IBusRule br1;
br1 = BRFactory.getBusRule(rs);
//works: useBusRule in IBusRule intfc
br1.useBusRule(); // OK
//doesn't work: multiplyParams not in IBusRule intfc
double dd = br1.multiplyParams(5.0, 7.0); // ERROR
}
In Java the solution to the problem is simple and
powerful. I can check the fully qulified name of my
returned object reference. I can then dynamically create a
reference of that object type at runtime by using the
Class.forName() method. If it is a valid class it will be
created, and I can assign my factory returned object
reference to it. I can now use the object. This allows
me to store ANY object in my factory, check its type when
its reference is returned, and use it.
Is it in the plans for C# to implement some sort of
dynamic casting functionality like this in the future? I
sure found it useful when I was developing in Java. Or, is
there a way to do this in C#, and I'm just not getting it?
Thanks for your help.
I'm developing software that requires business rules that
constantly need new versions for different quarters,
years, etc. I created a business rule factory that stores
these different business rules in a hashtable indexed by
the year, ie "3Q2003". Now based upon the key that I give
the factory, it will return a reference to the correct
business object for me to use. This works well as long as
all of the business objects in the factory implement the
same interface exactly.
If I want to extend a business rule by adding a method to
the "4Q2003" object, for example, my code breaks, since my
interface Type has no knowledge of the extra method in the
extended object reference, and hence can not call it.
I can, however, using reflection, get the fully qualified
name of the object, as it knows what it really is,
discover the method names on that object reference, and
actually call the extra method, but this is VERY
cumbersome. (I can send the code for this if necessary.)
It would be great if I could dymanically cast the returned
object reference from my factory to the extended class
represented by the fully qualified name that I can
retrieve from the object reference itself, then I could
just use my extended object.
An example of the problem in a code snippet is:
if (rs == "4Q2003")
{
IBusRule br1;
br1 = BRFactory.getBusRule(rs);
//works: useBusRule in IBusRule intfc
br1.useBusRule(); // OK
//doesn't work: multiplyParams not in IBusRule intfc
double dd = br1.multiplyParams(5.0, 7.0); // ERROR
}
In Java the solution to the problem is simple and
powerful. I can check the fully qulified name of my
returned object reference. I can then dynamically create a
reference of that object type at runtime by using the
Class.forName() method. If it is a valid class it will be
created, and I can assign my factory returned object
reference to it. I can now use the object. This allows
me to store ANY object in my factory, check its type when
its reference is returned, and use it.
Is it in the plans for C# to implement some sort of
dynamic casting functionality like this in the future? I
sure found it useful when I was developing in Java. Or, is
there a way to do this in C#, and I'm just not getting it?
Thanks for your help.