471,071 Members | 911 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 software developers and data experts.

OO design advice - virtual constant?

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.
Nov 16 '05 #1
5 3836
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]
- mv*@spam.guard.caspershouse.com

"Paul E Collins" <fi******************@CL4.org> wrote in message
news:cm**********@titan.btinternet.com...
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.

Nov 16 '05 #2
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.


"Paul E Collins" <fi******************@CL4.org> wrote in message
news:cm**********@titan.btinternet.com...
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.

Nov 16 '05 #3
Lot simpler then my suggestion.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Or**************@TK2MSFTNGP12.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Paul E Collins" <fi******************@CL4.org> wrote in message
news:cm**********@titan.btinternet.com...
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 inheritedclasses - CarrierImporter, RouteImporter, etc. - that validate and processspecific 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.


Nov 16 '05 #4
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
wrote:
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.
Nov 16 '05 #5
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Paul E Collins" <fi******************@CL4.org> wrote in message
news:cm**********@titan.btinternet.com...
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.


Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Sebastian Faust | last post: by
5 posts views Thread by Carl Bevil | last post: by
13 posts views Thread by Bryan Parkoff | last post: by
7 posts views Thread by bartek | last post: by
4 posts views Thread by Ian Giblin | last post: by
31 posts views Thread by grahamo | last post: by
5 posts views Thread by Tony Johansson | last post: by
3 posts views Thread by fernandez.dan | last post: by
3 posts views Thread by Sorin Dolha | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.