Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Partial Classes vs. Inheritance [closed]

Okay, so we have a utility here in-house that generates business-model classes from our database tables and views, similar to (but not quite exactly like) an ORM. In maintaining it, it occurred to me that the data in the schemas isn't likely going to change a whole lot. The functionality, however, might. We may want to add additional functionality down the road. We may want to generate some of that functionality, and we may want to extend it.

The classes we're building will reside in a class library for consumption by other libraries and applications. No big surprise there. But the stumper here is how to design generated classes in such a way that we disrupt as little code as possible when we regenerate a class. If, for example, code has been added to a property (which represents a column in a database table), we don't want to lose that.

So, there are two approaches that leap to mind:

Classic inheritance, where the entire thing is done in a single, "monolithic" class and consumers are free to override the base implementation. This gets kind of tricky from time to time, though, and frequently introduces casting headaches. Further, if the derived class isn't careful and forgets to invoke base class functionality, things can quickly go awry.

Partial classes. In this scheme, we separate the business object into distinct parts: the properties (which map to columns), and the behaviors. Behaviors could even be broken down further into generated behaviors and custom behaviors. The problem with this approach, as you can see, is its inherent complexity. Further, there's the naming problem.

Here's my question for you folks: When you're dealing with a scenario like this (if you ever have), or if you were presented with a scenario like this, what solutions would you consider, and why?

like image 673
Mike Hofer Avatar asked Jun 26 '09 17:06

Mike Hofer


People also ask

Should I use partial classes C#?

Use of Partial ClassesIf you are working on a bigger project, splitting the files over different classes helps developers work on the same project simultaneously. If you are working on an automatically generated source, then the code can be added to the class without regenerating the source file.

Can partial class be inherited?

Inheritance cannot be applied to partial classes.

What is the point of a partial class?

Partial classes are portions of a class that the compiler can combine to form a complete class. Although you could define two or more partial classes within the same file, the general purpose of a partial class is to allow the splitting of a class definition across multiple files.

What is the difference between class and partial class in C#?

A partial class is a special feature of C#. It provides a special ability to implement the functionality of a single class into multiple files and all these files are combined into a single class file when the application is compiled. A partial class is created by using a partial keyword.


1 Answers

The whole reason Partial Classes are included in .Net is to make it easier to utilize code generation tools.

If you are generating code, use partial classes. It's really that simple. Why would you take the risk that the code might have to be re-generated down the road and you'd have to do a ton of work to re-update it with customizations?

I don't find there is much complexity to partials either - it's simply one class split over multiple files. Generated code in one file, custom code in the other.

like image 121
womp Avatar answered Oct 23 '22 18:10

womp