I'm building a small timing application using the MVVM pattern, using entity framework for persistence. At this stage, my logic is pretty thin, as I only need to perform a few calculations and aggregations on related data. At the moment, I have implemented these by writing them in a partial class of the entity class.
For example:
// entity framework generated
partial class Lap {
int Id { /* boilerplate */ }
DateTime StartTime { /* etc */ }
DateTime EndTime { /* etc */ }
}
// in my partial class (written by me)
partial class Lap {
TimeSpan Duration {
get { return EndTime - StartTime; }
}
}
Is it bad practice to drop extra logic straight onto the entity-generated classes? Should I make another domain layer for this logic?
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.
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.
Are partial classes a code smell? But of you are writing all the code by hand, splitting the code into partial classes is a code smell, an indication that your class is growing too big/having too many responsibilities.
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.
You are doing the very thing for which partial classes were designed; adding relevant logic to a code-generated class without bogging down the inheritance tree. Keep it up.
Addition:
From a page in the font of all tribal knowledge, Wikipedia (emphasis added):
The purpose of partial classes is to allow a class's definition to span across multiple files. It is especially useful for:
- Very large classes (where it is cumbersome to navigate with an editor through a single file)
- Separation of concerns, in a way similar to aspect-oriented programming but without using any extra tools. An example is shown below.
- Allowing multiple developers to work on a single class at the same time without the need for later merging files in source control.
- Allowing a separation between the class interface and the implementation-related definitions (Separate definitions of the public and private parts)
- Easing the writing of code generators, such as visual designers. This is perhaps the most useful reason. It is a challenge to develop code generators that can manage the generated code when it is placed in the human-written code:
- Requires a lot of parsing of unnecessary code, just to find a place to insert the generated code. Altering the code is also a problem. Poorly written generators hold the potential risk of damaging the entire file.
Using partial classes, the code generator processes a separate file, and is thus alleviated from all the above mentioned problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With