Code Monkey home page Code Monkey logo

efcore-dto-demo's Introduction

Demo code for the "Stop using Entity Framework as a DTO provider!" talk

This repo contains the code used for the demos in my talk "Stop using Entity Framework as a DTO provider!".

The talk is about usingthe features in EF Core to properly map entities instead of just using it to read data from the database.

Running the code

The code is built around using tests to verify that everything works. This means, that to "run" the code, you just run the tests.

However, the tests use a database. So, you will need to set up a SQL Server instance and make sure that the connection string in the appSettings.json files in the different test projects are correct.

Generating SQL scripts to run during deployment

In my talk, I show how to generate idempotent scripts from the migrations. This makes it possible to run the migrations as part of the deployment, instead of during application startup.

Comment: Running them as part of the startup might seem like a good idea. But it often isn't... For example, in a load balanced environment, you might run into startup issues...

To generate the SQL scripts, just run

dotnet ef migrations script --idempotent --output ./migrations.sql

in the directory that contains the project that has the context to be migrated, and the migrations.

Note. In this solution, that means the /src/EFCore.Domain.

Migrations

In this demo, I use multiple DbContext's to allow me to map the same table in different ways. This unfortunately means that I cannot use automated migration creation. However, that is not a big problem, as it is quite easy to create migrations manually.

Comment: I have included a snippet to simplify this. It is available in ./snippets/ef-core-snippets.snippet. Just put it in C:\Users<USERNAME>\Documents\Visual Studio 2022\Code Snippets\Visual C#\My Code Snippets and it should pop up in Visual Studio and be available as efmigration.

However, because I use multiple DbContext, there isn't really a DbContext to "attach" my migrations to. So, instead, I use a separate dummy context for the migrations, as it is only there to allow the EF Core tools to find the migrations.

One common problem that people tend to face, is that the EF Core tools want a runnable application to be able to locate the context. It also tends to require a database connection, which isn't generally available in a build pipeline.

To solve this, I am using an IDesignTimeDbContextFactory<T>. This allows us to configure the context for "design time" actions. In this case, I configure it for SQL Server, but set the connection string to null. This means that the EF Core tools will not attempt to communicate with the database when used.

Generating migration bundles

In the talk, I also cover the new migration bundle feature in EF Core. This allows us to create an executable that is able to migrate the database without us having to connect to it, and run the scripts manually. Something that can be a bit cumbersome in a deployment pipeline.

To generate the bundles, you just run

dotnet ef migrations bundle --output ./migrations.exe

in the directory that contains the project that has the context to be migrated, and the migrations.

And to execute the migrations, you just run

.\migrations.exe --connection "<CONNECTIONSTRING>"

More information

A bit of the content of the talk can be found as a blog post at https://www.fearofoblivion.com/dont-let-ef-call-the-shots

Contact

If you have any questions, feel free to ping me on Twitter @ZeroKoll. Or wherever you might find me online or IRL...

efcore-dto-demo's People

Contributors

chrisklug avatar

Stargazers

 avatar Alok Sharma avatar Coding Crisis avatar Panagiotis Onasis avatar Armando Jr avatar Ivan Pavlov avatar Matt O'Donnell avatar Matteo Zamparini avatar Alexander Marek avatar DOM avatar  avatar  avatar Adriano Kerber avatar Davor Pihač avatar Carlos Ryckeley avatar Václav Bílý avatar Kirill Lebedinov avatar  avatar Andriy Bezkorovayny avatar Michael Prattinger avatar Eser SAHIN avatar Christophe Petitjean avatar Zaranitos avatar IT Solutions Grzegorz Kępiński avatar GregH avatar Nikita Melnik avatar Thomas De Craemer avatar Gordon Kenaston avatar Rodrigo Correa Barro avatar Jakub Zalas avatar Media Explorer avatar Bilgehan Zeki ÖZAYTAÇ avatar Ireneusz Patalas avatar Thomas Jespersen avatar Osvaldo avatar Joseph Izang avatar Fabio Lima avatar Aaron Schendel avatar Vince Renders avatar  avatar  avatar Ciaran Crocker avatar  avatar Kieran Coldron avatar Virma avatar Max Lancaster avatar Gabriela Hermenegildo avatar Roland Papdi avatar stef avatar Mykhailo avatar  avatar erkan avatar bpy avatar Tyler Gibbons avatar  avatar Simon avatar Steffen avatar Tomás López Rodríguez avatar Rasmus Strunge avatar Konrad Pietralik avatar Jakub Mirota avatar αяτιsαη avatar Piotr Solarczyk avatar Tomáš Pouzar avatar Mohammed Abdelhalim avatar  avatar Alex Tjahjana avatar Jimi Friis avatar  avatar  avatar 고형호 avatar

Watchers

Osvaldo avatar António avatar  avatar Alex Tjahjana avatar Olaf Haase avatar Anders Rundgren avatar  avatar

efcore-dto-demo's Issues

Feedback from a long time EF user and experienced software developer

I saw a talk promoted on Jetbrains, controversially titled: "Stop using Entity Framework as a DTO provider!"

I googled the content and found this. I have matched this based on the author and speaker name.

Here's my take on what this sample demonstrates:

The sample code demonstrates the pitfalls of why people think Entity Framework is slow and champions downloading a lot more from your database server than is required in almost all scenarios. It encourages a lot of code to be written for very little gain. This sample code tries to bypass Entity Framework so much so that it starts replicating EF concepts in extension methods, and other domain. Change tracking being one example.

The consumer should not be able to pass in one bool and change the behaviour of the resulting entity. This is effectively building a repository and leaking Entity Framework outside.

All of the mappings require the "full fat" Entity Model be downloaded from your database. There is no partial hydration or projection, which is where the time and performance can come from. You are stopping Entity Framework from being what it is, over reaching and not allowing the database to do what it's good at: data processing.

Further to this, the sample code demonstrates tight coupling between DTOs and Entities. There is no way for me to reliably transport DTOs between layers without these having knowledge of domain entities that shouldn't leave my domain layer.

This codebase demonstrates a non-scalable, non-ideal usage of Entity Framework.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.