Duplicating Code Across Multiple Classes

Oct 4, 2011 at 9:16 PM

I'm using LinqToXSD to generate document models (code) based on a client's XSD's. Since these XSD's share common structure each time I generate a document model some objects get duplicated. To prevent compile problems, I've put each document model in its own name space. This has worked great. But now I want to do some custom validation with these objects which are public partial classes. So I defined a IValidate interface with a single Validate() method. Can you see the problem yet? Here goes, now I have some low level object generated in more than one object model but each in a different namespace.

namespace1
{
    public partial class foo : XTypedElement
       {........}
}

namespace2
{
    public partial class foo : XTypedElement
       {........}
}

Now I want to write a single partial class file to add IValidate interface and the Validate() method across all my foo classes. But, of course this want work because they are in reality different objects. I have lots of these because of the generated code and the XSD library used to generate the code. If I could just shadow the internals (read the '......'s above) this would work. I have many foo's in different namespaces (and I have foo1 through foo100 each with the same issue). I need to duplicate code, I know it's a no-no and I don't want to have to do it. But, if I do, I at least do not want to have to maintain so many copies of the same code. I'm fairly confident this can be done with dynamics but I'd rather not have to go there. Can anyone suggest some other way to write these validation methods and not have to manually copy to each namespace.class's partial class?