IRepository interface.

 First of all I'd like to remark that I'm not following PoEA Repository pattern. I just like this name! =)

Repository and Dao interfaces still remains hot topic today. While developing my last ASP.NET MVC application I've created my own interface, which I think suits best for me.

    public interface IRepository
    {
        IQueryable<T> CreateQuery<T>() where T : class;
        void Execute(Action<IPersister> action);
    }

    public interface IPersister
    {
        void Delete<T>();
        void SaveOrUpdate<T>();
    }

The most thing I like here is that I can hide transaction management in execute method implementation by wrapping Execute method delegate in "using transaction block" with try.

Sketch implementation of Execute method.

void Execute(Action<IPersister> action);

{

            using (var transaction = this.session.BeginTransaction())
            {
                try
                {
                    action(this.persister);
                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                }
            }

}

 What you think of that? Please leave a comment.

52 Comments

Comments have been disabled for this content.