To take full advantage of the repository pattern create an interface that implements the IRepositoryBase interface. All methods from the RepositoryBase will become available to the repository implementing this new interface.

public interface IProductRepository : IRepositoryBase
{
    bool AddProduct(Product product);
    Product FindProductByName(string productName);
}

Implementing this interface will ensure that the repository implementing this interface will provide access to the repository base methods. The repository implementing this interface will also need to implement RepositoryBase. The RepositoryBase class expects a type parameter of DbContext type. If the DbContext is not yet intitialized the RepositoryBase will intialize the DbContext for you through the Activator class.

public class ProductRepository : RepositoryBase<RepositoryContext>, IProductRepository
    {
        public ProductRepository()
            : base(throwExceptions: true, useTransactions: true)
        {
            base.RepositoryBaseExceptionRaised += ProductRepository_RepositoryBaseExceptionRaised;
        }

        void ProductRepository_RepositoryBaseExceptionRaised(Exception exception)
        {
            throw new NotImplementedException();
        }

        public bool AddProduct(Product product)
        {
            bool result = false;

            result = Add<Product>(product);

            return result;
        }

        public bool AddProductWithoutTransaction(Product product)
        {
            SetUseTransaction(false);

            bool result = false;

            result = Add<Product>(product);

            return result;
        }

        public bool AddOrUpdateProduct(Product product)
        {
            bool result = false;

            result = AddOrUpdate<Product>(product);

            return result;
        }

        public bool DeleteProduct(Product product)
        {
            bool result = false;

            result = Delete<Product>(product);

            return result;
        }

        public bool UpdateProduct(Product product)
        {
            bool result = false;

            result = Update<Product>(product);

            return result;
        }
        }
    }

 

Last edited Sep 26, 2012 at 2:35 PM by MMMicha, version 6

Comments

AlphaCodemonkey Dec 9, 2014 at 6:51 PM 
"If the DbContext is not yet intitialized the RepositoryBase will intialize the DbContext for you through the Activator class."

How would you pre-initialize the DbContext?