DEV Community

Discussion on: Clean Architecture | Creating Maintainable Software using .NET Core

Collapse
 
jpeg729 profile image
jpeg729

I like your GenericRepository that exposes a nice non-db-specific IQueryable interface, but I think it has a subtle bug...

Let's say I am writing an app that lets users add posts and tag them for each others review. I might have a Post class with a TaggedFor property of type ICollection<User>.

So when Alice adds a post and tags it for Bob's review the code might do this..

var post = new Post 
{ 
    Contents = "some text", 
    TaggedFor = new List<User> { loadUser(bob) } 
};
postRepository.Create(post);

and that's where the bug comes in to play...

The User object for Bob is loaded with AsNoTracking(), so when you run Create(post) EF thinks he is a new user, and adds him to the Users table again. Now suppose that your users are identified by their Email field loaded using .Where(u => u.Email == email).Single() then when Alice tags Bob, Bob gets locked out of the site/app. Oops!

I could be wrong since I only have experience with EF 6, but I don't think EF Core is all that different on this point.