Creating Multiple Related Rows in One Operation

58 views Asked by At

I have a class Document and a class Home. I also have made a relation Document_Home.

What I want to do is to select some photos and create them as Documents once the button "Create Home" is pressed.

How should I do this?

Document has its own controller with a create method and the same for Home and Document_Home.

Is there something like a transaction?

First, create the Home. Second, create the Documents Third, create the relation between Documents and Home.

1

There are 1 answers

2
user1477388 On

Contrary to my previous comment, you may want to consider using a "repository pattern." You don't need any DB context for this, I just mean create a project in your solution adjacent to your MVC project called "Repositories" and then create a new class that has functionality like this:

public class MyCustomRepo
{
    public void MyCreateMethod(List<MyPhotoObject> photos)
    {
        foreach (var photo in photos)
        {
            // create home
            // create documents
            // create relation
        }
    }
}

This would keep your number of controllers down and would be "SOC" separation of concerns being as this is more data layer than logic layer. I think it would keep your code a lot cleaner.

You'll have to reference the DLL from your repositories project in your MVC project.