Now that Iβve reviewed the sample code that you edited for publication, I would definitely reorganize your class to take advantage of LINQ-to-SQL built-in functionality. (I will not edit my previous comment because this is the best answer to the general question)
The fields of your class seem to be a fairly direct mapping of the columns in the Comments table in the database. Therefore, you do not need to do most of what you do manually in this class. Most functions could be handled simply by having a private member of type Madtastic.Entities.Comment (and simply matching properties with its properties if you need to keep this class interacting with the rest of the project). Then your constructor can simply initialize the private member Madtastic.DataContext and set your private member Madtastic.Entities.Comment to the result of the LINQ query on it. If the comment is null, create a new one and call InsertOnSubmit in the DataContext. (but it doesnβt make sense to post changes since you didnβt set any values ββfor this new object anyway)
In your SubmitChanges, all you have to do is call SubmitChanges in the DataContext. It saves its own track about whether to update the data, it will not get into the database if this does not happen, so you do not need _isDirty.
In your Delete (), all you have to do is call DeleteOnSubmit in the DataContext.
In fact, you may find with a small overview that you do not need the Madtastic.Comment class at all, and the Madtastic.Entities.Comment LINQ to SQL class can act directly as your data access level. It seems that the only practical differences are the constructor that accepts commentID, and the fact that Entities.Comment has a UserID property, where your Madtastic.Comment class has a whole user. (However, if the User is also a table in the database, and the UserID is the foreign key to his primary key, you will find that LINQ-to-SQL created the User object on the Entities.Comment object, which you can access directly with comment. User)
If you find that you can completely exclude this class, it may mean that you can further optimize the DataContext life cycle by creating it in accordance with the methods of your project that use a comment.
Edited to publish the following example of a reorganized code (apologies for any errors, since I typed it in a notebook in a couple of seconds, and did not open a visual studio, and I would not intellisense for your project):
namespace Madtastic { public class Comment { private Madtastic.DataContext mdc; private Madtastic.Entities.Comment comment; public Int32 ID { get { return comment.CommentsID; } } public Madtastic.User Owner { get { return comment.User; } } public Comment(Int32 commentID) { mdc = new Madtastic.DataContext(); comment = (from c in mdc.Comments where c.CommentsID == commentID select c).FirstOrDefault(); if (comment == null) { comment = new Madtastic.Entities.Comment(); mdc.Comments.InsertOnSubmit(comment); } } public void SubmitChanges() { mdc.SubmitChanges(); } public void Delete() { mdc.Comments.DeleteOnSubmit(comment); SubmitChanges(); } } }
You might also want to implement IDisposable / using, as suggested by a number of people.
source share