Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework 4 - Where to put "ApplyCurrentValues" Logic?
    primarykey
    data
    text
    <p>I'm using the "<strong><a href="https://stackoverflow.com/questions/1343530/entityframework-net-4-update-entity-with-a-simple-method/1347775#1347775">stub technique</a></strong>" to <strong>update</strong> my POCO's (used in a detached context, ASP.NET MVC).</p> <p>This is the code i currently have in my controller (which works):</p> <pre><code>[HttpPost] public ActionResult Edit(Review review) { Review originalReview = _userContentService.FindById(review.PostId) as Review; var ctx = _unitOfWork as MySqlServerObjectContext; ctx.ApplyCurrentValues("MyEntities.Posts", review); _unitOfWork.Commit(); // ..snip - MVC stuff.. } </code></pre> <p>As you can see, there is code smell everywhere. :)</p> <p>A few points:</p> <ol> <li>I use Dependency Injection (interface-based) for basically everything</li> <li>I use the Unit of Work pattern to abstract ObjectContext and provide persistence across multiple repositories</li> <li>Currently my <strong>IUnitOfWork</strong> interface has only 1 method: <code>void Commit();</code></li> <li>Controller have <code>IUserContentService</code> and <code>IUnitOfWork</code> inject by DI</li> <li><code>IUserContentService</code> calls <code>Find</code> in Repositories, which use the <code>ObjectContext</code>.</li> </ol> <p>These are two things i don't like with my above code:</p> <ol> <li>I don't want to cast the <strong>IUnitOfWork</strong> as <code>MySqlServerObjectContext</code>.</li> <li>I don't want the Controller to have to care about <code>ApplyCurrentValues</code></li> </ol> <p>I basically want my code to look like this:</p> <pre><code>[HttpPost] public ActionResult Edit(Review review) { _userContentService.Update(review); _unitOfWork.Commit(); // ..snip - MVC stuff.. } </code></pre> <p>Any ideas how i can do that? (or something similar).</p> <p>I already have smarts to work out the entity set name based on the type (combination of generics, pluralization), so don't worry too much about that.</p> <p>But <strong>i'm wondering where the best place to put <code>ApplyCurrentValues</code> is</strong>? It doesn't seem appropriate to put it in the <code>IUnitOfWork</code> interface, as this is a persistence (EF) concern. For the same reason it doesn't belong in the Service. If i put it in my <code>MySqlServerObjectContext</code> class (makes sense), where would i call this from, as nothing directly has access to this class - it is injected via DI when something requests <code>IUnitOfWork</code>.</p> <p>Any thoughts?</p> <p><strong>EDIT</strong></p> <p>I have a solution below using the stub technique, but the problem is if i had retrieved the entity i am updating beforehand, it throws an exception, stating an entity with that key already exists.</p> <p>Which makes sense, although i'm not sure how can resolve this?</p> <p>Do i need to "check if the entity is already attached, if not, attach it?"</p> <p>Can any EF4 experts out there help?</p> <p><strong>EDIT</strong></p> <p>Nevermind - found the solution, see answer below.</p>
    singulars
    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.
 

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