.NET

Should I create an interface for my entity (i.e. an User object)?

I got the question above in my ADO.NET done right article. I hope that my answer can help others and therefore it got this dedicated post.

No. I create interfaces mostly for two reasons.

To create abstraction for behavior.

An interface is really a contract that offers some sort of service in your code. It can for instance be to store an entity in a data source (repository) or filter HTTP requests (action filter).

The great thing with an interface compared to a base class is that you remove all implementation details and just focus on the behavior. A class can also implement more than one interface, but just one base class.

When it comes to user objects and similar they do typically not contain behavior or offer some sort of service that you want to create an abstraction for.

To be able to unit test my code.

Entities are typically fully isolated, i.e. they do not depend on something with behavior that you must abstract away to be able to write isolated unit tests.

Therefore there is no reason to have interfaces to be able to test them or code that is dependent of them.

Conclusion

Hence I do not think there is a reason to create an interface for entities.

Related Articles

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button