Need advice on complex insert scenario using LinqToSql with repository pattern?

You can use System.Transactions. TransactionScope to perform this all in an atomic transaction, but if you are using different DataContext instances, it will result in a distributed transaction, which is probably not what you really want By the sounds of it, you're not really implementing the repository pattern correctly. A repository should not create its own DataContext (or connection object, or anything else) - these dependencies should be passed in via a constructor or public property.

If you do this, you'll have no problem sharing the DataContext : public class UserRepository { private MyDataContext context; public UserRepository(MyDataContext context) { if (context == null) throw new ArgumentNullException("context"); this. Context = context; } public void Save(User user) { ... } } Use the same pattern for ClubMemberRepository (or whatever you call it), and this becomes trivial: using (MyDataContext context = new MyDataContext()) { UserRepository userRep = new UserRepository(context); userRep. Save(user); ClubMemberRepository memberRep = new ClubMemberRepository(context); memberRep.

Save(member); context.SubmitChanges(); } Of course, even this is a little bit iffy. If you have a foreign key in your database, then you shouldn't even need two repositories, because Linq to SQL manages the relationship. The code to create should simply look like this: using (MyDataContext context = new MyDataContext()) { User user = new User(); user.Name = "Bob"; user.

ClubMember = new ClubMember(); user.ClubMember. Club = "Studio 54"; UserRepository userRep = new UserRepository(context); userRep. Save(user); context.SubmitChanges(); } Don't fiddle with multiple repositories - let Linq to SQL handle the relationship for you, that's what ORMs are for.

You can use System.Transactions. TransactionScope to perform this all in an atomic transaction, but if you are using different DataContext instances, it will result in a distributed transaction, which is probably not what you really want. By the sounds of it, you're not really implementing the repository pattern correctly.

A repository should not create its own DataContext (or connection object, or anything else) - these dependencies should be passed in via a constructor or public property. If you do this, you'll have no problem sharing the DataContext: public class UserRepository { private MyDataContext context; public UserRepository(MyDataContext context) { if (context == null) throw new ArgumentNullException("context"); this. Context = context; } public void Save(User user) { ... } } Use the same pattern for ClubMemberRepository (or whatever you call it), and this becomes trivial: using (MyDataContext context = new MyDataContext()) { UserRepository userRep = new UserRepository(context); userRep.

Save(user); ClubMemberRepository memberRep = new ClubMemberRepository(context); memberRep. Save(member); context.SubmitChanges(); } Of course, even this is a little bit iffy. If you have a foreign key in your database, then you shouldn't even need two repositories, because Linq to SQL manages the relationship.

The code to create should simply look like this: using (MyDataContext context = new MyDataContext()) { User user = new User(); user. Name = "Bob"; user. ClubMember = new ClubMember(); user.ClubMember.

Club = "Studio 54"; UserRepository userRep = new UserRepository(context); userRep. Save(user); context.SubmitChanges(); } Don't fiddle with multiple repositories - let Linq to SQL handle the relationship for you, that's what ORMs are for.

Aaron, great answer, thanks. I'm kicking myself because I originally had something similar to your third code snippet, but in the course of debugging, I came to the (incorrect) conclusion that it wasn't going to work. Trying it again now, it works great.

Second, my repository classes are based on NerdDinner, so I'm blaming Scott Gu for my less than stellar implementation :). I actually have a "todo" to start wrapping my DataContext instances with using () statements, though, and your example gives me a good idea of how to go about it. – DanM Feb 21 '10 at 21:17.

Yes, you can do it in a single transaction. Use the TransactionScope object to begin and commit the transaction (and rollback if there is an error of course).

There is a one-to-one mapping between users and club members, so every time I insert a ClubMember, I need to insert a User first. This is implemented with a foreign key on ClubMember (UserId REFERENCES User (Id)). Over in my ASP.NET MVC app, I'm using LinqToSql and the Repository Pattern to handle my persistence logic.

The way I currently have this implemented, my User and ClubMember transactions are handled by separate repository classes, each of which uses its own DataContext instance. This works fine if there are no database errors, but I'm concerned that I'll be left with orphaned User records if any ClubMember insertions fail. To solve this, I'm considering switching to a single DataContext, which I could load up with both inserts then call DataContext.SubmitChanges() only once.

The problem with this, however, is that the Id for User is not assigned until the User is inserted into the database, and I can't insert a ClubMember until I know the UserId. Is it possible to insert the User into the database, obtain the Id, then insert the ClubMember, all as a single transaction (which can be rolled back if anything goes wrong with any part of the transaction)? If yes, how?

If not, is my only recourse to manually delete any orphaned User records that get created?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions