Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include closing brace in partial repository code #4327

Merged
merged 1 commit into from
Apr 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion entity-framework/core/testing/testing-without-the-database.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,20 @@ If you've decided to write tests without involving your production database syst

... and here's a partial sample implementation for production use:

[!code-csharp[Main](../../../samples/core/Testing/BusinessLogic/BloggingRepository.cs?name=BloggingRepository)]
```csharp
public class BloggingRepository : IBloggingRepository
{
private readonly BloggingContext _context;

public BloggingRepository(BloggingContext context)
=> _context = context;

public Blog GetBlogByName(string name)
=> _context.Blogs.FirstOrDefault(b => b.Name == name);

// Other code...
}
```

There's not much to it: the repository simply wraps an EF Core context, and exposes methods which execute the database queries and updates on it. A key point to note is that our `GetAllBlogs` method returns `IEnumerable<Blog>`, and not `IQueryable<Blog>`. Returning the latter would mean that query operators can still be composed over the result, requiring that EF Core still be involved in translating the query; this would defeat the purpose of having a repository in the first place. `IEnumerable<Blog>` allows us to easily stub or mock what the repository returns.

Expand Down