Replies: 2 comments 2 replies
-
Hi @yarus - you're welcome. 😀
You can accomplish this using the unit of work and repository patterns for both reads and writes. Store the interfaces within Domain or Application, and the implementations within Infrastructure. A simple and flexible approach would be to add LINQ support to the repositories, for example: public virtual IEnumerable<T> List(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
return _dbContext.Set<T>()
.Where(predicate)
.AsEnumerable();
} See: https://deviq.com/design-patterns/repository-pattern With the above approach, the goal is accomplished, the Application layer is independent of EF Core. Perhaps there is some value in demonstrating this approach with a blog post. However, bypassing the domain model for reads takes more work.
True, but probably not a strength of EF Core. I feel there is too much work creating and configuring keyless entity types. See: https://docs.microsoft.com/en-us/ef/core/modeling/keyless-entity-types.
Yes, something like Dapper could work well. It is great at mapping from raw SQL to a specified type: var sql = "select * from customers";
var customers = connection.Query<Customer>(sql); See: https://www.learndapper.com/selecting-multiple-rows Clearly, to remain independent of database, the raw SQL should not be in the Application layer, it needs to be in the Infrastructure layer. So a possible solution would be to use Dapper for reads, and EF Core for writes. You could create combined, or separate, read / write repositories leveraging both technologies. In this scenario you might find Specification pattern to be helpful. See: https://deviq.com/design-patterns/specification-pattern. This approach increases the overall complexity of the solution so be sure to access the benefits gained against the increased complexity. |
Beta Was this translation helpful? Give feedback.
-
Another approach would be to create factories for your models. This is probably more inline with what you would like to accomplish. Within the Application layer, create a new Within the Application layer, create a new public interface IProductModelFactory
{
ProductModel GetModel(int productId);
} Within the Infrastructure layer create a new 'ProductModelFactory' and implement as follows: public class ProductModelFactory : IProductModelFactory
{
public ProductModel GetModel(int productId)
{
using (var connection = new SqlConnection(connectionString))
{
var sql = $"select * from products where productid = {productId}";
return connection.QuerySingle<ProductModel>(sql);
}
}
} The factory is responsible for querying the database and building a |
Beta Was this translation helpful? Give feedback.
-
Hi, thanks for a great template!
I have a question regarding the way of implementing Queries and Query handlers with EF Core following Clean Architecture approach.
The ultimate goal is to exclude EF Core dependency from Application layer (Application project).
For Commands this could be easily achieved using Repository pattern. We can isolate AggregateRoot retrieval to repositories. Repository interfaces can be defined in Domain or Application projects while actual implementations in Infrastructure project.
But when we talking about Queries - we don't really need to have Domain business rules in place - we just want to query data. Ideally, we could switch to completely different library like Dapper. But it is also possible to still use EF Core but bypassing full scale domain objects retrieval. The only issue I have is how to not add EF Core dependency into Application project and still allow to query necessary data using simple interface like DbContext?
Beta Was this translation helpful? Give feedback.
All reactions