Thursday, January 14, 2010

NHibernate – Creating the BaseRepository (Part 8)


Before creating repositories for each domain entities in our model, we will work on a base repository to define the operations that can be performed on all the entities in our model. It will be from this base repository the domain specific entity repositories inherit from.
My IRepositoryinterface is defined as

public interface IRepository where TEntity : BaseEntity
{
    void Add(TEntity entity);
    void Save(TEntity entity);
    void Remove(TEntity entity);
    TEntity GetById(KId id);
    IList GetAll();
    IList FindAllWithMultipleConditions(IList<Expression<Funcbool>>> searchCriterias);
    TEntity First(Expression<Funcbool>> searchCriteria);
    IList FindAll(Expression<Funcbool>> searchCriteria);
}

The repository class implements the IRepository interface.

public class Repository : IRepository where TEntity : BaseEntity

public virtual void Add(TEntity entity)
{
    using (ITransaction __transaction = ___Session.BeginTransaction())
    {
        entity.CreatedDate = DateTime.Now;
        entity.ChangedDate = DateTime.Now;
        ___Session.Save(entity);
        __transaction.Commit();
    }
}
public IList FindAll(Expression<Funcbool>> searchCriteria)
{
    return ParseQuery(___Session)
           .Where(searchCriteria)
           .ToList();           
}
public TEntity GetById(TId id)
{
    return ___Session.Get(id);
}

As mentioned earlier, I have used Linq to NHibernate in my repository classes. Once the base repository is ready, we can start writing some test cases and see how this base repository satisfies our requirements.

[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
    ___sessionFactory = new Configuration()
                       .Configure()
                       .AddAssembly(typeof(BaseEntity<long>).Assembly)
                       .BuildSessionFactory();
}

[TestMethod]
public void can_add_new_customer()
{
    var __customer = new Customer { FirstName = "Rahul", LastName = "P" };
    using (ISession __session = SessionManager.CurrentInstance.Session)
    {
        IRepository<Customer, long> __repository = new Repository<Customer, long>(__session);
        __repository.Add(__customer);
        var __customerFromDb = __repository.First(x => x.FirstName == "Rahul");
        Assert.IsNotNull(__customerFromDb);
        __repository.Remove(__customerFromDb);
    }
}

[TestMethod]
public void can_find_customer_by_search_query()
{
    using (ISession __session = SessionManager.CurrentInstance.Session)
    {
        IRepository<Customer, long> __repository = new Repository<Customer, long>(__session);
        var __customer = __repository.First(x => x.FirstName == "Prajeesh");
        Assert.IsNotNull(__customer);
        Assert.IsTrue(__customer.FirstName == "Prajeesh");
    }
}
Next we’ll work on the CustomerRepository class which inherits from the base repository that we have created here.

1 comment:

Anonymous said...

Is it possible to put it together as a Visual Studio project. It will be very useful for beginners like me.