[{"data":1,"prerenderedAt":59},["ShallowReactive",2],{"blog-post-removing-infrastructure-information-from-domain-code-3":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-3","Removing infrastructure information from domain code - Part 3","Getting rid of the private setters","Eliminating the need of adding private setters to bind read-only properties by using IL weaving and the Fody plugin SpatialFocus.AddSetter.Fody.",[10,11,12,13],"DDD","C#","EF","Clean code","2021-04-09T00: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\nMany of the properties in our entities are being set via the constructor and should be purely read-only. So we could just remove the setters, but Entity Framework requires them to work properly.\n\nIf you check the [EF Core documentation](https:\u002F\u002Fdocs.microsoft.com\u002Fen-us\u002Fef\u002Fcore\u002Fmodeling\u002Fconstructors#read-only-properties), it says:\n\n> Properties without setters are not mapped by convention. (Doing so tends to map properties that should not be mapped, such as computed properties.)\n\nThe documentation recommends to add private setters, or to add a more explicit mapping in OnModelCreating in the infrastructure code. The second would suit our DDD approach and keep infrastructure code separated, however it will be quite labor intensive in bigger projects.\n\nThe first option will not only make our code less self-explaining, the code will also display compiler warnings, indicating that the setters are never used.\n\n# The solution\n\nWith the help of `IL Weaving` we can keep stick to the first option from above but still keep the the domain model clean at design-time. The private setters will be weaved into our entities at build-time. We haven't found any existing Fody plugin, so we created one for this purpose. We named it [SpatialFocus.AddSetter.Fody](https:\u002F\u002Fgithub.com\u002FSpatialFocus\u002FAddSetter.Fody). There are only two steps necessary:\n\n- Install the SpatialFocus.AddSetter.Fody NuGet package and update the Fody NuGet package\n- Add `\u003CSpatialFocus.AddSetter\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    \u003CSpatialFocus.AddSetter>\n        \u003CIncludeNamespaces>\n            OurProjectTitle.Domain.Model\n            OurProjectTitle.Domain.Model.*\n        \u003C\u002FIncludeNamespaces>\n    \u003C\u002FSpatialFocus.AddSetter>\n\u003C\u002FWeavers>\n```\n\n# The result\n\nInstead of polluting our domain entities with properties that contain unused and potentially misleading setters, we end up with a clean domain entity class and real read-only properties:\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; }\n\n    public string Name { get; }\n\n    public Specs Specs { get; }\n}\n```\n\nThanks to the Fody plugin, after the build, EF Core will find the private setters and will map them properly.\n\nThe [part 4](\u002Fblog\u002Fremoving-infrastructure-information-from-domain-code-4) of our series will take a closer look at some lazy-loading specifics in our properties.",[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",1783525744127]