Do you define an interface for every public class

2019-04-10 11:14发布

Do you implement an interface for every public class in your domain model? Pros and Cons?

Update: If Repositories interfaces and domain model classes are defined in separate assemblies, wouldn't there be circular dependency if we do not define interfaces for every domain class.

9条回答
甜甜的少女心
2楼-- · 2019-04-10 12:00

From my point of view, it is overkilled. Just my 2 cents...

查看更多
乱世女痞
3楼-- · 2019-04-10 12:04

No.

Cons.

  1. Noise code.
  2. More to write.
  3. YAGNI.
查看更多
【Aperson】
4楼-- · 2019-04-10 12:07

From the perspective of immutability you might consider putting interfaces on your domain objects:

In most places in your code you'll want your objects to be immutable, so that you can guarantee they won't be changed - in this case you'd work with a data access object of some kind that returns the interface, ensuring that your domain object can't be altered.

If you are writing an admin page of some sort where the user will end up editing the domain object, you'll need to expose setters - your data access object will need to return 'MutableDomainObject' instanaces (either a class or a sub-interface).

Having said that, I agree with the YAGNI philosophy expressed above - if you don't need to guarantee immutability at the moment, it may not be worth investing in this at the moment. It shouldn't be too difficult to factor out interfaces at a later date.

查看更多
登录 后发表回答