[{"data":1,"prerenderedAt":59},["ShallowReactive",2],{"blog-post-removing-infrastructure-information-from-domain-code-2":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-2","Removing infrastructure information from domain code - Part 2","Getting rid of the empty constructor","Eliminating the need of adding an empty constructor to bind navigation properties by using IL weaving and the Fody plugin EmptyConstructor.Fody.",[10,11,12,13],"DDD","C#","EF","Clean code","2021-04-03T00: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\nWhen designing a clean DDD solution, many times you need to pollute your domain with infrastructure code in order to get Entity Framework (EF) working. This blog series will look at some of these issues and how to resolve them.\n\n# The issue\n\nWhen constructor parameters cannot be bound to properties, the Entity Framework requires an empty constructor. This happens for example when your constructor parameter is a navigation property. In this case EF will go via the emtpy constructor and bind the property directly.\n\nIf you check the [EF Core documentation](https:\u002F\u002Fdocs.microsoft.com\u002Fen-us\u002Fef\u002Fcore\u002Fmodeling\u002Fconstructors), it says:\n\n> EF Core cannot set navigation properties (such as Blog or Posts above) using a constructor.\n\nIn our example we will have to define a protected empty constructor, since EF cannot bind the Specs property from the constructor:\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\nHowever, this protected empty constructor is something we would like to get rid of, when designing a clean DDD domain entity. It doesn't belong to our domain logic.\n\n# The solution\n\nWith the help of `IL Weaving` we can keep the domain model clean at design-time and weave the empty constructor into our entities at build-time. After some quick research we came across the very helpful Fody plugin [EmptyConstructor.Fody](https:\u002F\u002Fgithub.com\u002FFody\u002FEmptyConstructor), which does exactly that. The are only the following steps necessary:\n\n- Install the EmptyConstructor.Fody NuGet package and update the Fody NuGet package\n- Add `\u003CEmptyConstructor\u002F>` to `FodyWeavers.xml`\n- Define the visibility of the constructors as `family`, which is the IL equivalent of `protected`\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    \u003CEmptyConstructor Visibility='family'>\n        \u003CIncludeNamespaces>\n            OurProjectTitle.Domain.Model\n            OurProjectTitle.Domain.Model.*\n        \u003C\u002FIncludeNamespaces>\n    \u003C\u002FEmptyConstructor>\n\u003C\u002FWeavers>\n```\n\n# The result\n\nInstead of polluting our domain entities with \"unneccessary\" (i.e. irrelevant for our business domain) protected empty constructor 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    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, EF Core will find an empty constructor, which allows it to set the `Specs` value object.\n\nThe [part 3](\u002Fblog\u002Fremoving-infrastructure-information-from-domain-code-3)  of our series will take a closer look at the setters in our properties, which we are not using at the moment.",[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",1783525744860]