-2

Here's the classic example:

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public Blog Blog { get; set; }
}

This creates a circular (or cyclic) dependency. And as such, you get all the nasties that come with circular dependencies: Single Responsibility violation, JSON serialiser exceptions, and IoC container exceptions, to name a few.

It very much feels like a code-smell.

Is it?

Ash
  • 2,021
  • 2
  • 26
  • 59

1 Answers1

0

Are Fully Defined Relationships in Entity Framework a code-smell

Not at an abstract level. Meaning just because you define all your relationships does not mean there is a problem.

This creates a circular (or cyclic) dependency.

Your code has no such dependencies. With the navigation properties you have setup, even with valid EF Entity Builder setup, without changing these classes, you have no such dependencies. How you decide to use these classes, changes whether or not this problem exists.

you get all the nasties that come with circular dependencies

This has always existed. Simply using EF shouldn't magically make this disappear.

We design our classes around real world scenarios/entities. Those scenarios/entities have the exact same problems.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • My code does not have dependencies? Really? Would you like me to quote Martin Fowler or go into what it means fundamentally to be a dependency? – Ash Mar 04 '19 at 14:38
  • Secondly, sure, there may not be a problem as it stands but that’s precisely what a ‘code-smell’ is; it’s laying the foundations to be a major headache in the future, most likely for the person who ends up cleaning up the mess you’ve made. Also, the nasties only exist if there’s a cyclic dependency. I’m certainly not using EF to “make them disappear”, as you’re suggesting. Quite the opposite, as my question clearly states. Finally, yes, while the real world displays cyclic dependencies, we sure as hell don’t have to model our classes that way. – Ash Mar 04 '19 at 14:52