If you have functionality that you often use in classes representing very different things, in my experience, which should fall into several categories:
- Utilities (e.g. line formatting, parsing, ...)
- General issues (logging, security, ...)
For functions such as utilities, you should consider creating separate classes and links to utility classes, if necessary in a business class.
public class Validator { public bool IsValidName(string name); } class Patient { private Validator validator = new Validator(); public string FirstName { set { if (validator.IsValidName(value)) ... else ... } } }
For cross-cutting tasks such as logging or security, I suggest you study Aspect Oriented Programming .
Regarding the example of PrintA vs. PrintB, discussed in other comments, this looks like a great example for the Factory pattern. You define an interface, for example. IPrint, the PrintA and PrintB classes that implement IPrint, and assign an IPrint instance based on a specific page.
// Simplified example to explain: public interface IPrint { public void Print(string); } public class PrintA : IPrint { public void Print(string input) { ... format as desired for A ... } } public class PrintB : IPrint { public void Print(string input) { ... format as desired for B ... } } class MyPage { IPrint printer; public class MyPage(bool usePrintA) { if (usePrintA) printer = new PrintA(); else printer = new PrintB(); } public PrintThePage() { printer.Print(thePageText); } }
source share