Source

Camino / src / Empi.Camino / Data / RepositoryBase.cs

Full commit
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Linq.Expressions;
using Empi.Camino.Web.Data;
using Empi.Camino.Configuration;

namespace Empi.Camino.Data
{

    //MP: make it testable use Unity to inject the Repository or the DataContext
    internal abstract class RepositoryBase<TEntity> //IRepository<T> 
        where TEntity : class
    {
        protected RepositoryBase(IDataContextFactory dataContextFactory)
        {
            this.dataContextFactory = dataContextFactory;
        }

        private IDataContextFactory dataContextFactory;

        ///// <summary>
        ///// Return all instances of type T.
        ///// </summary>
        ///// <returns></returns>
        //internal IQueryable<TEntity> All()
        //{
        //    return GetTable();
        //}

        ///// <summary>
        ///// Return all instances of type T that match the expression exp.
        ///// </summary>
        ///// <param name="exp"></param>
        ///// <returns></returns>
        //internal IEnumerable<TEntity> FindAll(Expression<Func<TEntity, bool>> exp)
        //{
        //    return GetTable().Where<TEntity>(exp);
        //}

        internal TEntity Single(Expression<Func<TEntity, bool>> exp)
        {
            return GetTable().Single(exp);
        }

        //internal TEntity Single(Expression<Func<TEntity, bool>> exp, Type type)
        //{
        //    return GetTable(type).Single(exp);
        //}

        internal TEntity SingleOrDefault(Expression<Func<TEntity, bool>> exp, Type type)
        {
            return GetTable(type).Cast<TEntity>().SingleOrDefault(exp); //MP: cast or oftype, not sure, test it
        }

        internal TEntity First(Expression<Func<TEntity, bool>> exp)
        {
            return GetTable().First(exp);
        }

        

        //internal TEntity First(Expression<Func<TEntity, bool>> exp, Type type)
        //{
        //    return GetTable(type).First(exp);
        //}

        //internal virtual void MarkForDeletion(TEntity entity)
        //{
        //    GetTable().DeleteOnSubmit(entity);
        //}

        /// <summary>
        /// Create a new instance of type T.
        /// </summary>
        /// <returns></returns>
        //internal virtual TEntity CreateInstance()
        //{
        //    TEntity entity = Activator.CreateInstance<TEntity>();
        //    GetTable().InsertOnSubmit(entity);
        //    return entity;
        //}

        internal virtual TEntity CreateInstance(Type type)
        {
            TEntity entity = (TEntity) Activator.CreateInstance(type);
            GetTable().InsertOnSubmit(entity);
            return entity;
        }

        internal void SaveAll()
        {
            dataContextFactory.DataContext.SubmitChanges();
        }
       
        private string PrimaryKeyName
        {
            get { return TableMetadata.RowType.IdentityMembers[0].Name; }
        }

        private Table<TEntity> GetTable()
        {
            return dataContextFactory.DataContext.GetTable<TEntity>();
        }

        // give here back an ITable because Executiontime should be the latest possible 
        // if you make a cast  or oftype its getting executed
        private ITable GetTable(Type type) 
        {
            return dataContextFactory.DataContext.GetTable(type); 
        }

        private System.Data.Linq.Mapping.MetaTable TableMetadata
        {
            get { return dataContextFactory.DataContext.Mapping.GetTable(typeof(TEntity)); }
        }

        private System.Data.Linq.Mapping.MetaType ClassMetadata
        {
            get { return dataContextFactory.DataContext.Mapping.GetMetaType(typeof(TEntity)); }
        }
    }
}