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

后端 未结 6 1150
梦谈多话
梦谈多话 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:50

    Why has this one fizzled out?

    I find having an interface for a domain object, as Charlie Martin said, allows me to choose my implementation.

    A fundamental example is the identifier (object.Id) for an object, this will be different depending on where you store that object and the responsibility for creating it may or may not rest in the implementation of the data later. In SQL Server you might go for an autonumber, but in Azure table storage you might go for a Guid, but you don't want to have to change the business logic of you app because you change where you store your data.

    I may or may not choose to have my domain object persisted, or even use it in the presentation layer - it depends on the scope of my app which is best. But adding a set of common domain interfaces in a common layer allows me to write services against them and reuse them again and again.

    We would go over the same argument about what should be an address if we hadn't got IAddress, new programmers would be rewriting stuff for credit cards if it weren't for ICreditCard.

    The anti-pattern label is a bad use of language, it's an over simplification for describing the worth of solutions to complex and varied tasks.

    There is a place for most patterns even the maligned Singleton, which means its not an "anti-pattern", at least as far as the term suggests.

提交回复
热议问题