1

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

What structure would you suggest? I thought of deriving both from Package and making the server and client have a one-to-one foreign key with the top level table rather then the base class. But that seems unnatural. How would you handle this?
Once you find one to one relationship in the database you should always ask yourselves: do I really need it? There are scenarios when this relation is useful but in most cases it is just overhead. I would probably use just single table / entity because from your description I don't see any reason to split it.
Thanks. I was hoping you would say something like that. It sounds very logical from a DB perspective but not from a class design one. Is there a way I can keep my class hierarchy and still package the table as you suggest?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.