0

I have been scouring forums and repository pattern blogs for some clear direction on how I should be coding my project and I'm stuck. Any help or guidance from you guys would be much approciated :)

I started my project as EF5 MVC4 Razor Code First and decided to use MVCScaffolding to generate all my controllers, views and repositories. This was my first project with these technologies, I was just told that this was how the team was doing it now (but the previous developers did model first and hand coded their contexts).

SO, all is great, we're coding a bunch of screens, but one of our screens is a complex one that involves many models/sub modlels (ie/ Object model has FKs to Responses, Attachments, Reviewers, etc...). The user adds a bunch of data, selects one or more reviewers, adds 0 or more attachments. Then they hit Save!

Now my big problem is that I want to save all this data as one transaction, and if something fails on one of the children models (ie/ attachments) the transaction will roll back. However, the way the MVCScaffolding repositories are created, each model has it's own instance of DB Context and it's own Save. And the controllers accept each unique repository as parameters for loading the screen data. Another thing to note is for this screen we are using a ViewModel to load the data, and then wrote custom mappers to map back to the different models for saving. We can save each piece separately, and possibly the solution is just to wrap TransactionScope around my save, but I also want to reduce the number of calls to the db, as each repository save does a call.

I thought I could add code to the parent repository for a UnitsOfWork type save that would add/edit all the child obejcts in one context object, but that seems like a hack more than anything, and I want to code this properly.

One of the other projects here just made a custom DB context and all Save methods were in that class, is that the best way to do it? Another dev did code first but hand coded all his Save methods. None of them are in a standard place and he is using TransactionScope with a DBContext inside (is that overkill or does DBContext not handle transactions)?

Since I'm so new to this, I need help and no one I work with seems to agree on a proper method. I'm not sure if my model is wrong for an "MVC App" since I'm such a database heavy thinker.

Below is a sample of my models, any guidance is appreciated. Thanks :)

public class Anomaly 
{
    [Key]
    public int AnomalyId { get; set; }

    public string Subject { get; set; }

    public int PlantId { get; set; }
    [ForeignKey("PlantId")]
    public virtual Plant Plant { get; set; }

    public DateTime? ReviewByDate { get; set; }

    public virtual ICollection<AnomalyReviewer> AnomolyReviewers { get; set; }
    public virtual ICollection<AnomalyAttachment> AnomalyAttachments { get; set; }

}

    public class AnomalyAttachment 
{
    [Key]
    public int AnomalyAttachmentId { get; set; }

    public int AnomalyId { get; set; }
    [ForeignKey("AnomalyId")]
    public virtual Anomaly Anomaly { get; set; }

    public string FilePath { get; set; }
    public string FileName { get; set; }
    public string FileExtension { get; set; }
    public string Notes { get; set; }           

}

ps. that is just a sample... thanks!

4

1 に答える 1

1

Controllerから継承するクラス「Master」を作成するだけです。次に、 public User GetUserById(Int32 id) のようにすべてのクエリをそこに記述します。最後に、プライベートへの呼び出しを行う関数を作成します!! DbContext の実装。

私は通常、その関数に SystemEvents の列挙型を与えるので、何かが失敗した場合に何が起こるかの参照を取得します...もちろん、個人的なテストのためにデータベースに独自のエラーを記録するために、独自の通知機能またはモデルを作成する必要があります。 ...

私はすべてのコードを書くことができ、実際に考えてみると、リポジトリパターンはほとんどの場合やり過ぎであることがわかったので、これをすべて行いました。

于 2013-04-09T09:07:01.737 に答える