问题描述:

Hierarchy:

  • abstract Package
  • ServerPackage: Package
  • ClientPackage: ServerPackage

In terms of EF CF, all three tables have a one-to-one relationship. Have I selected the right composition to cater to the following scenario?

Scenario:

  • Abstract Package is created by the server upon request with unique Guid.
  • Server creates ServerPackage and sends to client.
  • Client processes ServerPackage and creates a ClientPackage to send back to server.

In other words, as soon as a Package request comes in, a ServerPackage AND a ClientPackage should be created. I am assuming this will not work since I need both packages to be tied to the same abstract Package Id.

Should I be using composition instead of inheritance and force a one-to-one relationship?

Please advise.

网友答案:

It will work only if all your packages are same instance => when the server receives request it must immediately create ClientPackage. It cannot create Package because Package is abstract and if it only creates ServerPackage you will not be able to create ClientPackage with the same Id. So it looks like whole your inheritance will be redundant in such scenario.

If you want to follow your workflow you need composition.

相关阅读:
Top