Is using one-to-one interfaces with domain entities a good or bad practice? Why?

后端 未结 6 1141
梦谈多话
梦谈多话 2021-02-01 17:16

One thing I see in some DDD enterprise apps that I work on, is the use of interfaces that are identical to the domain entities, with a one-to-one mapping of properties and funct

6条回答
  •  一整个雨季
    2021-02-01 17:26

    The biggest reason for always specifying the domain objects as interfaces instead of directly as classes is to give you a degree of freedom on the implementation. In your example you only have one kind of IAccount, so it's a little redunant.

    But what if you had, for example:

    public class Account : IAccount { ... }       // Usual account, persistent
    public class MockAccount : IAccount { ... }   // Test mock object
    public class TransAccount : IAccount { ... }  // Account, not persistent
    public class SimAccount : IAccount { ... }    // Account in a performance sim
    

    and so on?

    By defining the domain objects as interfaces, you can replace the implementations without disturbing your domain definition.

提交回复
热议问题