コントローラーのスリム化に関する質問に対するDarinの回答をフォローしてきましたが、このIEntityChangeTracker
例外で問題が発生しています。
エンティティオブジェクトは、IEntityChangeTrackerの複数のインスタンスから参照することはできません。
これが私のモデルバインダーです:
public class PostModelBinder : IModelBinder
{
private readonly IPostRepository _repository;
public PostModelBinder(IPostRepository repository)
{
_repository = repository;
}
public object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext)
{
var postIDValue = controllerContext.Controller.ValueProvider.GetValue("postID");
int postID;
if (postIDValue == null || !int.TryParse(postIDValue.AttemptedValue, out postID))
return null;
return _repository.FindPost(postID, filterByPublished: true);
}
}
この後、私のカスタムアクションフィルターは、投稿に対して少しの検証を実行し、投稿が有効でない場合はリダイレクトします。この時点まで、すべてが順調です。ポストリードカウントを更新しようとすると、コントローラーでエラーが発生します。
public class PostsController : Controller
{
private IPostRepository postRepository;
// snip...
[AutoMap(typeof(Post), typeof(PostViewModel)), HttpGet]
[PostActionFilter]
public ActionResult ShowPost(Post model)
{
postRepository.PostVisited(model);
return View(model);
}
}
IEntityChangeTracker
私は事実上、同じオブジェクトを参照する2つのリポジトリになってしまい、それが起こるのを待っている悪いモジョのように見えるので、の不満を理解しています。PostVisited()
呼び出しをプッシュできることは知っていますPostModelBinder
が、モデルの更新はそこに属する動作のようには見えません。
これを回避する別の方法はありますか?
ありがとうございました。