After reading on the benefits (and using) the Repository Design pattern within an ASP.NET MVC 3.0 project, I’ve come across a certain issue which got me puzzled and started to question the benefits of the pattern. Perhaps someone could help me clarify this.
I have the following 3 TABLES:
- -User (
parent
) - -UserLead (
child of User
) - -UserLeadNotes (
child of UserLead
)
Roughly, the tables and their relationships are designed like this:
- User [Id]
- UserLead [Id, UserId (Foreign Key)]
- UserLeadNotes [Id, UserLeadId (Foreign Key)]
I have a BaseRepository which include the basic CRUD methods.
(IEnumerable<TEntity> Get(), TEntity GetByID(), Insert(), Delete(), etc…)
With that in place, I also have a Service Layer
which calls the appropriate repositories when and if needed.
In a nutshell, the application displays a UserLead and allows a user to add/remove notes to that UserLead (which are stored/deleted in the UserLeadNotes table). Adding/removing notes are done via Ajax calls. The removing of a note passes along the “id”
of the note I wish to delete.
Now…before I actually delete the note, I need to make sure that the note truly belongs to the current logged in user (User.Identity.Name aka UserId
)
Considering my method deleteUserNote(int noteId)
only receives a “noteId”
parameter AND considering that my UserLeadNotes TABLE does not have a foreign key on UserId
, what I need to do, is add an
.Include(“UserLead’)
inside my _userLeadNoteRepository
to have something like:
UserLeadNote userLeadNote = _userLeadNoteRepository
.AllIncluding(p => p.UserLead)
.FirstOrDefault(p => p.UserLeadNoteID.Equals(noteId)
&& p.UserLead.UserID.Equals(User.Identity.Name));
Then I would delete the UserLeadNote object that I just found:
_userLeadNoteRepository.Delete(userLeadNote);
Questions:
Following the above approach, this would force me to a)
create a Repository for my UserLeadNotes and b)
because of the .Include() the userLeadNote object found would have a property called UserLead
holding all the fields defined in the UserLead table.
If my UserLead table happens to have 14 fields and 10 of these fields are ntext, that means I would be loading/holding too much information for nothing!
If I was to use, instead of a Lambda expression, a Linq statement bypassing the repository and directly use my Context:
using(MyContext db = new MyContext ())
{
var query =
from uln in db.UserLeadNote
join ul in db.UserLead on uln.UserLeadID equals ul.UserLeadID
where uln.UserLeadNoteID == userLeadNoteID
&& ul.UserID == User.Identity.Name
select uln;
var userLeadNote = query.FirstOrDefault();
db.UserLeadNote.Remove(userLeadNote);
db.SaveChanges();
}
Wouldn’t this approach be more efficient as it would not bring back those unnecessary fields from the UserLead table? (Those 10 ntext field for example).
Unless I’m not using the Repository Pattern correctly, or perhaps this is an Entity Framework issue, I’d like to know what anyone thinks!
Keep in mind, that I am aware of the benefits of having a Repository Pattern in place but curious if this is some kind of issue people are having or not. And if so, have they decided to drop the Pattern and directly use their Context using Linq statements.
What’s the lesser of two evils?