fearofawhackplanet
fearofawhackplanet

Reputation: 53378

How to define a template in a looser way than interfaces in C#

Say I have a group of classes which generate documents from templates. For example...

class CustomerInvoice
{
    public satic string TemplatePath 
    { 
        get { return @"C:\Templates\InvoiceTemplate.doc"; }
    }

    public static DocumentType DocumentType 
    { 
        get { return DocumentType.WordDocument; }
    }

    public static void Create(Customer customer, int orderNumber)
    {
         //...
    }
}

All of these classes share the same method names, but not necessarily method signatures.

For example, I might have

CustomerInvoice.Create(Customer customer, int orderNumber);
DespatchNote.Create(Customer customer, int orderNumber, Warehouse warehouse);
PackingLabel.Create(int orderNumber);

... or whatever (struggling to come up with sensible examples).

Is there a mechanism in OO which specifies what method names a group of classes has in this way? I'm really thinking of just having a way to enforce consistent implementation and naming accross a group of similar objects, so they are more intuitive for consumers. Would a case like this be considered a valid/worthwhile use of any such technique?

Upvotes: 3

Views: 105

Answers (2)

AakashM
AakashM

Reputation: 63338

consistent implementation and naming accross a group of similar objects, so they are more intuitive for consumers

Since you're talking about ensuring the public interface of your API conforms to a certain system, I don't think there are necessarily going to be any OOP constructs that will help.

What might help however is any one of a number of tools intended to control style and design. I'm thinking of things like StyleCop, FxCop, and NDepend, all of which allow the creation of custom rules (with varying amounts of pain!). In particular, NDepend would allow you to set up a Code Query Language rule along the lines of

WARN IF Count == 0 IN SELECT METHODS WHERE NameIs "Create" AND IsStatic

which (applied to a suitable namespace) would warn if any particular class did not have a static method (of any signature) named Create

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1062640

No, there is no construct for that. Static methods in particular have no way of enforcing any commonality. If is was an interface you could have something like Create(object) or Create(T) (for some T defined on a generic interface) and take a single parameter to represent te state.

Upvotes: 3

Related Questions