OO design advice - virtual constant?

  • Thread starter Thread starter Paul E Collins
  • Start date Start date
P

Paul E Collins

I have a class hierarchy representing data importers, each of which
reads lines from a particular type of comma-separated data file and
creates corresponding entries in a database. There is an abstract base
class, Importer, which contains the file-reading logic, and a series
of inherited classes - CarrierImporter, RouteImporter, etc. - that
validate and process specific file types.

To help identify valid files, each type of import file has a distinct
line prefix (for instance, "CARRI" for carriers, and "LRTIN" for
routes). This is obviously constant for any one class, but it needs to
be virtual because (i) it is different for each class and (ii) it is
used polymorphically in the Importer base class. Since 'const' and
'readonly' fields cannot be virtual, I have used a read-only property:

public virtual string LinePrefix { get; }
.... and then ...
public override string LinePrefix { get { return "CARRI"; } } // ...
etc.

The read-only property - when I really want a "virtual constant" -
seems like a bit of a hack. Additionally, I would like to make
LinePrefix static, so that I can access it from outside without having
to create an instance. However, static fields can't be virtual either!

Is there a better pattern for this kind of thing, where a virtual
field is constant for any single class but varies between classes?

P.
 
Paul,

Yes, there is. Define an attribute and then attach it to the specific
type. The attribute would be something like "LinePrefixAttribute" or
something like that, and the constructor would take the prefix. Then, you
would have a read only property of type string which would return the
prefix.

This would allow you to get the attribute, and then the prefix, without
having to create an instance of the type, and you would be able to set the
prefix for each specific type.

Hope this helps.
 
There are two ways that I might handle this.

1) You could give the subclasses complete freedom by having a non-static
method called CanHandleContent(Stream s) which returns a boolean. This
method would do whatever it needs to determine if this is appropriate
content. If it returns true, then you can pass this to the method that
processes the content.
2) Have each Importer "registered", meaning that the class type is
maintained in some collection like a Hashtable. Once the classes are
registered, then you can have a method called GetImporter(Stream s) which
will return an instance based on the passed content. This is similar to how
the WebRequest class works. You will notice how WebRequest has a method for
registering a prefix and creating an object instance. You could even
combine item 1 with this to create a truly flexible system.
 
Lot simpler then my suggestion.

Nicholas Paldino said:
Paul,

Yes, there is. Define an attribute and then attach it to the specific
type. The attribute would be something like "LinePrefixAttribute" or
something like that, and the constructor would take the prefix. Then, you
would have a read only property of type string which would return the
prefix.

This would allow you to get the attribute, and then the prefix, without
having to create an instance of the type, and you would be able to set the
prefix for each specific type.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Paul E Collins said:
I have a class hierarchy representing data importers, each of which reads
lines from a particular type of comma-separated data file and creates
corresponding entries in a database. There is an abstract base class,
Importer, which contains the file-reading logic, and a series of inherited
classes - CarrierImporter, RouteImporter, etc. - that validate and process
specific file types.

To help identify valid files, each type of import file has a distinct line
prefix (for instance, "CARRI" for carriers, and "LRTIN" for routes). This
is obviously constant for any one class, but it needs to be virtual
because (i) it is different for each class and (ii) it is used
polymorphically in the Importer base class. Since 'const' and 'readonly'
fields cannot be virtual, I have used a read-only property:

public virtual string LinePrefix { get; }
... and then ...
public override string LinePrefix { get { return "CARRI"; } } // ... etc.

The read-only property - when I really want a "virtual constant" - seems
like a bit of a hack. Additionally, I would like to make LinePrefix
static, so that I can access it from outside without having to create an
instance. However, static fields can't be virtual either!

Is there a better pattern for this kind of thing, where a virtual field is
constant for any single class but varies between classes?

P.
 
Yes, there is. Define an attribute and then attach it
to the specific type. The attribute would be something
like "LinePrefixAttribute" or something like that, and
the constructor would take the prefix. Then, you would
have a read only property of type string which would
return the prefix.

That sounds like an interesting idea. I'll try it out. Thanks!

P.
 
I think in this case, attribute is even an overkill. since each specific
derived importer class only recognize one line prefix, just pass that string
literal to the base class constructor from the derived class constructor.

Nicholas Paldino said:
Paul,

Yes, there is. Define an attribute and then attach it to the specific
type. The attribute would be something like "LinePrefixAttribute" or
something like that, and the constructor would take the prefix. Then, you
would have a read only property of type string which would return the
prefix.

This would allow you to get the attribute, and then the prefix, without
having to create an instance of the type, and you would be able to set the
prefix for each specific type.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Paul E Collins said:
I have a class hierarchy representing data importers, each of which reads
lines from a particular type of comma-separated data file and creates
corresponding entries in a database. There is an abstract base class,
Importer, which contains the file-reading logic, and a series of inherited
classes - CarrierImporter, RouteImporter, etc. - that validate and process
specific file types.

To help identify valid files, each type of import file has a distinct line
prefix (for instance, "CARRI" for carriers, and "LRTIN" for routes). This
is obviously constant for any one class, but it needs to be virtual
because (i) it is different for each class and (ii) it is used
polymorphically in the Importer base class. Since 'const' and 'readonly'
fields cannot be virtual, I have used a read-only property:

public virtual string LinePrefix { get; }
... and then ...
public override string LinePrefix { get { return "CARRI"; } } // ... etc.

The read-only property - when I really want a "virtual constant" - seems
like a bit of a hack. Additionally, I would like to make LinePrefix
static, so that I can access it from outside without having to create an
instance. However, static fields can't be virtual either!

Is there a better pattern for this kind of thing, where a virtual field is
constant for any single class but varies between classes?

P.
 
Back
Top