encapsulation - DDD - Manage Coupling between Domain and Repository -


my question simple: how class's private data repository save?

regardless of architectural style adopt, agrees business objects shouldn't know "how" save - shouldn't implement database or other persistence details. however, seems me business objects ones know "what" need save. repository knows how fetch database, if know how translate business object database terms, must know what translate.

consider may use database, i'm not marking classes hibernate annotations because might save flat text file.

assuming classes here named after specific business entities, wrong doing like

interface exporter {     public void export(string id, string value1, string value2); }  interface repository {     public entity findbyid(string id); }  class entity {     private string id;     private string value1;     private string value2;     private string derivedvalue;      public entity() {}      public entity(string id, string value1, string value2)     {         this.id = id;         this.value1 = value1;         this.value2 = value2;         this.derivedvalue = /* derived id, value1, , value2 */;     }      public void dobusiness()     {         // ...     }      public void export(exporter exporter)     {         exporter.export(this.id, this.value1, this.value2);     } } 

and using like

flatfilerepositoryandexporter flatfile = new flatfilerepositoryandexporter(...); entity entity = flatfile.findbyid(...);  // business logic entity.dobusiness();  entity.export(flatfile); 

i understand there frameworks can assist me, @ end of day, rely on reflection of sort. want know, without reflection, how can statically compose objects expose data while maintaining encapsulation. answer can come visitor pattern.

i tend agree @mikesw making external persistence tools able collect domain object state, of minor scope adjustments (or reflection orms do), simpler letting domain object control entirely persisted.

there's third way consists in making entity emit events describing happened instead of exposing state -- that's event sourcing. whoever wants can listen , persist changes in whatever form want derive.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -