I'm worried I'm adding too many interfaces

前端 未结 3 1004
别跟我提以往
别跟我提以往 2021-02-12 09:30

I am building out my domain model and continuing to refactor it. As I do, I am finding that I like interfaces as it allows me to create reusable methods/controllers/views for c

相关标签:
3条回答
  • 2021-02-12 10:11

    Don't create interfaces that you don't foresee an imminent need for. Observe the YAGNI (you ain't gonna need it) principle. Otherwise you'll wind up with needlessly complicated code.

    0 讨论(0)
  • 2021-02-12 10:16

    The fact that you are using interfaces is a good thing. However, you should ask yourself, if I create an IEnabled interface, will I ever reference my class by that interface alone? i.e. will there be contexts where I interact with my class purely via the single property that interface exposes?

    Also, can you consider contexts where you will interact with multiple implementation of this IEnabled interface?

    If the answer to both of these question is "no", then the interface serves very little purpose.

    Having said that, please don't worry too much about this! it does very little harm.

    0 讨论(0)
  • 2021-02-12 10:19

    I think your problem is that you are trying to shoe-horn your domain model into whatever gui that you're displaying data in.

    Instead, consider your domain object things that have behaviour close to data and in its c'tor, give it an Action<DomainEvent>. Now, make sure that you ONLY EVER pass data OUT from a domain object through this action.

    Now, you listen. Whenever you actually want to make a change to your domain, call a method on it. Let your GUI be updated through the Action<DomainEvent> by taking these events and saving them to whatever read model that you are interested in.

    Have a look at http://www.infoq.com/presentations/ffffd-eric-evans and consider his points about domain events.

    Now you don't have to add strange interfaces related to a technical domain into your business domain anymore. And remember; if you are doing CRUD like your examples show, then you are NOT doing domain driven design. You have an anemic domain.

    Final point: use interfaces for things that actually need to be interchangeable. Are you carrying around a lot of INamed things in your application that can be interchanged with one another?

    Let me also link this, for you to consider:

    • http://lostechies.com/jimmybogard/2011/10/11/event-sourcing-as-a-strategic-advantage/
    • http://lostechies.com/jimmybogard/2010/04/08/strengthening-your-domain-domain-events/
    0 讨论(0)
提交回复
热议问题