How can I prevent methods from being added to the class?

I’m trying to find out if there is a way to stop adding functions / methods ( EDIT: by other developers ) to the class for the case when the object is a model or DTO that should not contain methods (to prevent the Model / DTO from being "misused" by others who might try to add " auxiliary "methods, etc.).

Is there any way to achieve this?

+5
source share
5 answers

Use reflection and write unit test, which fails if the model class has methods.

Mark all model classes with a custom attribute. Then create a unit test that uses reflection to load this assembly, repeat all the classes in this assembly and verify that the classes marked with the model attribute have no methods. This should be fairly straightforward using reflection.

+5
source

I believe that you are trying to solve procedural problems with code where you should use communication.

Your colleagues (I suppose) work with code files with "full trust" privileges. If they violate this privilege, you must open a dialog. Use this change as an opportunity to train them in their intended design. Perhaps they are true, and you will be educated!

I suggest simply making the intended design obvious in the class name and with a comment indicating the intended character. Perhaps indicate the project document that informed the class.

+3
source

You cannot stop anyone who has full write access to your code base to do this. The only thing you can do to avoid is to create a CodeAnalysis rule for FXCop, as pointed out by Christian.K in the comments, or write your DTO class so that it is undoubtedly a DTO that should not have any methods using a unique name for a class, and if that’s not enough, provide some code comments that notify the encoder that this is not the case.

However, you may need some method if you use collections, for example. where you need some kind of comparison if the two instances of your DTO are equal, so you must provide at least the Equals - and GetHashCode method.

+2
source

You do not need to use a structure to prevent adding a class. You can use the keyword sealed

 public sealed class MyDTOObject { ... } 

Now you can’t be inherent in the class and also prevent inheritance (which is essentially what you ask for). The mere fact of inheritance of MyDTOObject is the creation of a new class that is based on , not equal to or limited to, or defined in some way by the implementation of MyDTOObject .

You can use the abstract class to force derived classes to implement specific methods, but not vice versa.

If you want others to not leave your class and not use helper methods, you should use the sealed or mark the inner class.

0
source

You can prevent the extension or inheritance of a class by marking it final in such a way that no one can extend your class and therefore cannot add any behavior. But stop and ask yourself if you want to do this or not, because then you sign an invisible contract that everything that is required for the class is written to the class, and this class does not need to be added.

To be clear, I spoke in the context of Java.

0
source

All Articles