Reputation: 1341
This seems like a bunch of extra work for data access. I currently use a generic repository for very basic CRUD, as well I have some specific repository's where I may need to eager load some entities to get around the lovely N+1 anti pattern. Obviously not having N+1 issues makes the database happy. So here is an example of what I have come up with. I would like some feedback and maybe a better way to go about things.
As you can see from the code below my custom repository derives from Repository and I have the interface for the custom repository as well. This all works just seems wrong to me.
public class EmailQueueRepository:Repository,IEmailQueueRepository
{
private readonly ISessionWrapper<ISession> _sessionWrapper;
public EmailQueueRepository(ISessionWrapper<ISession> sessionWrapper) : base(sessionWrapper)
{
_sessionWrapper = sessionWrapper;
}
public void AddEmailQueueItem(IEmailQueueItem queueItem)
{
_sessionWrapper.Session.CreateCriteria(typeof (EmailQueue)).SetFetchMode("User", FetchMode.Eager).SetFetchMode("EmailDeliveryStatus",FetchMode.Eager);
var emailQueueEntity = new EmailQueue
{
User = _sessionWrapper.Session.QueryOver<User>().Where(u=>u.UserId == queueItem.UserId).SingleOrDefault(),
ToAddress = queueItem.ToAddress,
Subject = queueItem.Subject,
MessageBody = queueItem.MessageBody,
EmailDeliveryStatus = _sessionWrapper.Session.QueryOver<EmailDeliveryStatus>().Where(x=>x.DeliveryStatus=="Pending").SingleOrDefault(),
Active = 1,
DateCreated = DateTime.Now,
LastUpdated = DateTime.Now
};
Insert(emailQueueEntity);
}
}
Upvotes: 0
Views: 119
Reputation: 49251
What seems wrong to you? This is basically what I do except:
This has worked very well for me and is easily understandable by other developers. In my opinion, the biggest pitfalls to avoid with the repository pattern is having a generic repository and having one repository per class.
Upvotes: 2