[{"data":1,"prerenderedAt":59},["ShallowReactive",2],{"blog-post-removing-infrastructure-information-from-domain-code-1":3,"blog-sidebar-tags":43,"blog-sidebar-popular":46},{"overview":4,"post":32},{"slug":5,"title":6,"subtitle":7,"opener":8,"tags":9,"date":14,"headerImage":15,"author":20,"images":30,"__typename":31},"removing-infrastructure-information-from-domain-code-1","Removing infrastructure information from domain code - Part 1","Getting rid of the virtual keyword","Eliminating the need of using the virtual keyword in lazy loaded navigation properties by using IL weaving and the Fody plugin Virtuosity.",[10,11,12,13],"DDD","C#","EF","Clean code","2021-03-11T00:00:00Z",[16],{"fileName":17,"url":18,"__typename":19},"DDD.png","https:\u002F\u002Fcms.spatial-focus.net\u002Fapi\u002Fassets\u002Fspatialfocus\u002Fe23416f4-3d80-404b-bf82-b5c79d9dee2e\u002F","Asset",[21],{"id":22,"flatData":23,"__typename":29},"247edfce-2ecf-40b8-87e1-58e549d0c243",{"name":24,"image":25,"__typename":28},"Christoph Perger",[26],{"url":27,"__typename":19},"https:\u002F\u002Fcms.spatial-focus.net\u002Fapi\u002Fassets\u002Fspatialfocus\u002F967c15d7-bd4a-4d6e-9e34-b891dccb9906\u002F","AuthorFlatDataDto","Author",null,"PostsFlatDataDto",{"slug":5,"title":6,"subtitle":7,"headerImage":33,"opener":8,"text":35,"tags":36,"date":14,"repo":37,"author":38,"images":30,"__typename":31},[34],{"fileName":17,"url":18,"__typename":19},"# About this series\n\nRecently we've had the opportunity to work on a greenfield project to develop a modern and extensible business application. We invested quite a bit of time in the architecture of the app and agreed on following the DDD software development principles. If you're following the articles of the great [Vladimir Khorikov](https:\u002F\u002Fenterprisecraftsmanship.com\u002F), you probably know about the numerous drawbacks in Entity Framework (EF) when designing entities and value objects. Many times you need to pollute your domain with infrastructure code in order to get EF working. This blog series will look at some of these issues and will show you how to create a clean DDD solution.\n\n# The issue\n\nIn Entity Framework you can load related data by using lazy loading proxies. When accessing the navigation properties, lazy-loading will fetch the related entity in an extra database roundtrip. If you check the [EF Core documentation](https:\u002F\u002Fdocs.microsoft.com\u002Fen-us\u002Fef\u002Fcore\u002Fquerying\u002Frelated-data\u002Flazy), it says:\n\n> EF Core will enable lazy loading for any navigation property that can be overridden -- that is, it must be `virtual` and on a class that can be inherited from.\n\nIn the following example, the `Posts` navigation property will be lazy loaded:\n\n```csharp\npublic class Blog\n{\n    public int Id { get; set; }\n    public string Name { get; set; }\n\n    public virtual ICollection\u003CPost> Posts { get; set; }\n}\n```\n\nHowever, the `virtual` keyword is something we would like to get rid of, when designing a clean DDD domain entity. It is infrastructure code that doesn't belong in the domain.\n\n\n# The solution\n\nWith the help of `IL Weaving` we can keep the domain model clean at design-time and weave the virtual keyword into our entities at build-time. After some quick research we came across the very helpful Fody plugin [Virtuosity](https:\u002F\u002Fgithub.com\u002FFody\u002FVirtuosity), which does exactly that. The are only two steps necessary:\n\n- Install the Virtuosity.Fody NuGet package and update the Fody NuGet package\n- Add `\u003CVirtuosity\u002F>` to `FodyWeavers.xml`\n\nIn our case we configured the `FodyWeavers.xml` to include only our domain entities in the Domain.Model namespace and below:\n\n```xml\n\u003CWeavers xmlns:xsi=\"http:\u002F\u002Fwww.w3.org\u002F2001\u002FXMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"FodyWeavers.xsd\">\n    \u003CVirtuosity>\n        \u003CIncludeNamespaces>\n            OurProjectTitle.Domain.Model\n            OurProjectTitle.Domain.Model.*\n        \u003C\u002FIncludeNamespaces>\n    \u003C\u002FVirtuosity>\n\u003C\u002FWeavers>\n```\n\n# The result\n\nInstead of polluting our domain entities with \"unneccessary\" (i.e. irrelevant for our business domain) virtual references we end up with a clean domain entity class, not carrying any infrastructure code:\n\n```csharp\npublic class Car\n{\n    public Car(string name, Specs specs)\n    {\n        Name = name;\n        Specs = specs;\n    }\n\n    protected Car()\n    {\n    }\n\n    public CarHolder? CarHolder { get; protected set; }\n\n    public int Id { get; protected set; }\n\n    public string Name { get; protected set; }\n\n    public Specs Specs { get; protected set; }\n}\n```\n\nAnd thanks to the Fody plugin, after the build we get this weaved result, so that EF Core can lazy-load the `CarHolder` on demand:\n\n```csharp\npublic class Car\n{\n    public Car(string name, Specs specs)\n    {\n        Name = name;\n        Specs = specs;\n    }\n\n    protected Car()\n    {\n    }\n\n    public virtual CarHolder? CarHolder { get; protected set; }\n\n    public int Id { get; protected set; }\n\n    public string Name { get; protected set; }\n\n    public Specs Specs { get; protected set; }\n}\n```\n\nThe [part 2](\u002Fblog\u002Fremoving-infrastructure-information-from-domain-code-2) of our series will take a closer look at the empty constructor we need to enable EF Core to instantiate the object and set the `Specs` value object.",[10,11,12,13],"https:\u002F\u002Fgithub.com\u002FSpatialFocus\u002Fsample-ef-removing-infrastructure-code-from-domain",[39],{"id":22,"flatData":40,"__typename":29},{"name":24,"image":41,"__typename":28},[42],{"url":27,"__typename":19},[11,10,12,13,44,45],"Geo","web",[47,53],{"slug":48,"title":49,"headerImage":50,"__typename":31},"radial-progress-css-animation","Radial progress with CSS",[51],{"url":52,"__typename":19},"https:\u002F\u002Fcms.spatial-focus.net\u002Fapi\u002Fassets\u002Fspatialfocus\u002Fb7cdc0ef-364e-4444-8dd3-b85f19e87820\u002F",{"slug":54,"title":55,"headerImage":56,"__typename":31},"load-testing-put-endpoints-with-apache-jmeter","Load testing PUT endpoints",[57],{"url":58,"__typename":19},"https:\u002F\u002Fcms.spatial-focus.net\u002Fapi\u002Fassets\u002Fspatialfocus\u002Fad44fc84-935c-4ab4-ab0e-9fbcb84fb2c1\u002F",1783525744869]