Note that there are some explanatory texts on larger screens.

plurals
  1. POData Mapper question in C# .NET
    primarykey
    data
    text
    <p>There seems to be a lot of "noise" going on around DDD, repositories, data mappers, etc... and very little "real world" implementation code to show newbies like myself what is "good", and what is "bad".</p> <p>I've just finished reading the book <a href="http://oreilly.com/catalog/9780735626096/" rel="nofollow">Architecting Applications for the Enterprise</a>, and it's been a huge eye-opener. I currently use the Active Record pattern on a project at work, and have been working on changing it to use Domain Model. I've used a lot of the examples of architecture in the book, as well as the Northwind Starter Kit code download that is a companion to the book.</p> <p>Everything has been going great, but now I've hit my first "real world" data mapper problem... aka, instead of my mapper just being responsible for taking one Entity object and persisting it to the database, I now have an Entity object that has an IList&lt;> collection that also needs to be mapped.</p> <p>The main Entity object is Expert, here is the code:</p> <pre><code> public class Expert { public int ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public virtual IList&lt;Case&gt; Cases { get; protected set; } } </code></pre> <p>Here is the implementation of the collection from Expert, the Case object:</p> <pre><code> public class Case { public int ID { get; set; } public string Name { get; set; } } </code></pre> <p>Can't get much simpler than that.</p> <p>Now, I have a DataMapper for each Entity Item, but my question is, when I go to map the Case collection in my ExpertDataMapper code, what is considered the "right" way to do that?</p> <p>In the book, there is actual SQL code that is embedded in the ExpertDataMapper that makes a call out to ADO.NET code that takes all Case items in the IList collection and calls that code once per item. Here is some pseudo-code:</p> <pre><code> public virtual void Create(Expert expert) { // Insert expert in the Expert table SqlHelper.ExecuteNonQuery(ProviderHelper.ConnectionString, CommandType.Text, "SQL TO INSERT EXPERT", this.BuildParamsFromEntity(expert)); // Insert individual Case items in the Case table foreach (Case c in expert.Cases) { SqlHelper.ExecuteNonQuery(ProviderHelper.ConnectionString, CommandType.Text, "SQL TO INSERT CASE", this.BuildParamsFromEntity(order)); } } </code></pre> <p>So two things wrong with this jump out at me immediately:</p> <ol> <li>Why does the ExpertDataMapper have the SQL call (or stored proc call) to insert a Case embedded in itself? To me, this breaks the idea of encapsulation... the ExpertDataMapper should know nothing about how a Case gets inserted into the database, that is the job of the CaseDataMapper.</li> </ol> <p>I would assume the ExpertDatMapper should DELEGATE the task of inserting a Case to the CaseDataMapper... but I don't know if it's "right" or "wrong" for one DataMapper to instantiate another. Is that considered a regular problem with DataMappers? I can find no guidance on this problem which I assume is fairly common.</p> <ol> <li>If I have 100 Cases associated with this Expert, that's 100 Case objects created, and 100 insert statements against the database? Something about this feels "wrong" to me, and goes against my better judgment. Now, if I were lucky enough to be able to use SQL Server 2008, I could use Table Valued Parameters, and it would not be as bad, but we're on 2005 right now.</li> </ol> <p>So, when it comes to DataMappers, I have not seen any concrete implementations of a simple create, update, etc.. on an entity object's DataMaper with a simple collection association. The book I'm reading has no code to support it (and the code that is there looks suspect to me).</p> <p>I have read, and own Martin Fowler's P of EAA book, so please don't tell me to look there. I am also aware of the ORM tools that are available to me, so I don't need to take care of implementing a DAL myself. I've played with EF 4.0, and I like it. But for this project, I don't have the option of using an ORM tool.</p> <p>Most books/examples seem to stop at the same basic premise that as long as I have a one-to-one association between my Entity object and the table it gets persisted to via the DataMapper, the world is just rose-colored using a Domain Model approach...</p> <p>If all my entities were one-to-one, I might as well just use Active Record and be done with it.</p> <p>Any guidance, suggestions, insight? Sorry, It's a long post, but I've read other posts here with similar problems, and no really concrete answers or suggestions of how to deal with the problem presented here.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload