CrazyCoderz
CrazyCoderz

Reputation: 1341

Looking for an easier way, Nhibernate and Repositories?

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

Answers (1)

Jamie Ide
Jamie Ide

Reputation: 49251

What seems wrong to you? This is basically what I do except:

  • I don't wrap the ISession and I let my code use the ISession directly for Save, Update, Get and Load operations and to control transactions. My repositories hold predefined queries but the application is free to use the ISession directly.
  • I expose the ISession publicly as a read-only property. This makes it possible to new up a second repository from an existing one.
  • I don't have a generic repository. I group together related queries and place them in appropriate repositories. For example, queries to retrieve Customer and CustomerStatus both go into a CustomerRepository instead of having one repository per class.

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

Related Questions