.NET Partial Classes Against Inheritance

Well, that’s why we have a utility inside the company that generates business model classes from our tables and database representations, similar (but not entirely accurate) to ORM. While maintaining it, it occurred to me that the data in the circuits are unlikely to change as a whole. However, functionality may be. We might want to add extra features along the way. We may want to generate some of these functions, and we can extend it.

The classes we create will be in the class library for consumption by other libraries and applications. No big surprise. But now is the time to create the generated classes so that we destroy as little code as possible when we restore the class. If, for example, code is added to a property (which represents a column in a database table), we do not want to lose it.

So, there are two approaches that strike the head:

Classical inheritance , where all this is done in one "monolithic" class, and consumers can redefine the basic implementation. From time to time it becomes quite difficult, and often introduces headaches. In addition, if the derived class is not careful and forgets to invoke the functionality of the base class, things can get loose quickly.

Partial classes . In this diagram, we divide the business object into separate parts: properties (which are mapped to columns) and behavior. Behavior can even be further broken down into generated behavior and user behavior. The problem with this approach, as you can see, is related to its complexity. In addition, there is a naming problem.

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

+5
partial-classes
Jun 26 '09 at 17:53
source share
5 answers

The whole reason Partial Classes are included in .Net is to simplify the use of code generation tools.

If you are creating code, use partial classes. It's really that simple. Why do you risk that the code may be reprogrammed along the way, and you have to do a ton of work to re-update it using the settings?

I do not find that there is partial complexity - it is just one class, divided into several files. Generated code in one file, native code in another.

+11
Jun 26 '09 at 18:01
source share
— -

I would go the way of partial classes. Just because it is more natural for dividing one object into many parts. Even Linq2Sql uses partial ones. Fighting complexity with patterns and related conventions. And it’s easier to do a “partial update” (if the generated code is well structured) on partial classes.

If you do not perform code generation, proceed to class inheritance (in fact, you would be forced, because partial files would not work in different assemblies).

+2
Jun 26 '09 at 18:01
source share

This is exactly the problem that partial classes exist to solve. I think you can break them too far for practical purposes. My gut reaction, not knowing the finer details of your application, would be to use a partial class file for all the generated class code and a second file for all manually created code. Create a naming convention for generated partial classes and stick to it.

+2
Jun 26 '09 at 18:01
source share

If you have a type that needs to be serialized (in xml) and you want to add some functionality, you will have to use an incomplete class, not inheritance. Thanks /// M

0
Aug 12 2018-10-12
source share

You speak:

similar to (but not exactly) ORM

I would draw some forecats figures for the maintenance costs for the current solution (which I assume is important), then select ORM, write a proof of the conceptual solution and compile comparative data to maintain it. I'm not sure if this custom package stands in the way of maintenance, rampup and the developer to buy, but I will never go anywhere near a “regular” product such as this. Knowledge is not transferable, the risk of error is high (always in case of code generation), and you are tied to the nodes of the base database.

0
Jul 02 '12 at 23:45
source share



All Articles