main-vinayFork / Source / WebService / Web.Admin.DataLayer / RepositoryBase.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Data.Objects;

namespace DataUp.Web.Admin.DataLayer
{
    /// <summary>
    /// Abstract base repository class having the implementation for methods which are
    /// common to all the db entities.
    /// </summary>
    /// <typeparam name="T">Template object</typeparam>
    public class RepositoryBase<T> : IRepositoryBase<T> where T : class
    {
        /// <summary>
        /// Data Up database context
        /// </summary>
        private DCXLAdminEntities dcxlAdminDbContext;

        /// <summary>
        /// Initializes a new instance of the RepositoryBase class.
        /// </summary>
        /// <param name="dcxlDbContext">Database context of DCXL DB</param>
        public RepositoryBase(DCXLAdminEntities dcxlDbContext)
        {
            this.dcxlAdminDbContext = dcxlDbContext;
        }

        /// <summary>
        /// Gets or sets the DCXL database context
        /// </summary>
        public DCXLAdminEntities DCXLDbContext
        {
            get
            {
                return dcxlAdminDbContext;
            }
            set
            {
                dcxlAdminDbContext = value;
            }
        }

        /// <summary>
        /// Gets the DbSet instance from the DCXL database context
        /// </summary>
        protected ObjectSet<T> ObjectSet
        {
            get
            {
                return DCXLDbContext.CreateObjectSet<T>();
            }
        }

        /// <summary>
        /// Adds the given entity to the DCXL database.
        /// </summary>
        /// <param name="entity">Entity to be added</param>
        public void Add(T entity)
        {
            ObjectSet.AddObject(entity);
        }

        /// <summary>
        /// Updates the given Entity in the DCXL database.
        /// </summary>
        /// <param name="entity">Entity to be updated</param>
        public void Update(T entity)
        {
            // ObjectSet.Attach(entity);
            // ObjectSet.Context.DetectChanges();

            // EarthOnlineDbContext.Entry(entity).State = EntityState.Modified;
        }

        /// <summary>
        /// Deletes the given entity from the DCXL database.
        /// </summary>
        /// <param name="entity">Entity to be deleted</param>
        public void Delete(T entity)
        {
            ObjectSet.DeleteObject(entity);
        }

        ///// <summary>
        ///// Gets the entity from the DbSet for the given object template which is having the given id.
        ///// </summary>
        ///// <param name="id">Id of the entity</param>
        ///// <returns>Entity with the given id</returns>
        //public T GetById(Guid id)
        //{
        //    return DbSet.Where(item => item.)
        //}

        /// <summary>
        /// Gets the Entity which satisfies the given condition from the collection of entities.
        /// </summary>
        /// <param name="condition">Condition to be satisfied</param>
        /// <returns>Entity which satisfies the condition</returns>
        public T GetItem(Expression<Func<T, bool>> condition)
        {
            return ObjectSet.Where(condition).FirstOrDefault<T>();
        }

        /// <summary>
        /// Gets the Entity which satisfies the given condition from the collection of entities and also load the navigation property
        /// mentioned in Include.
        /// </summary>
        /// <param name="condition">Condition to be satisfied</param>
        /// <param name="include">Navigation property to be included</param>
        /// <returns>Entity which satisfies the condition</returns>
        public T GetItem(Expression<Func<T, bool>> condition, string include)
        {
            return ObjectSet.Where(condition).Include(include).FirstOrDefault<T>();
        }

        /// <summary>
        /// Gets the items count satisfying the given condition in the current database table.
        /// </summary>
        /// <param name="condition">Condition to be applied</param>
        /// <returns>Count of items satisfying the condition</returns>
        public int GetItemsCount(Expression<Func<T, bool>> condition)
        {
            return ObjectSet.Count(condition);
        }

        /// <summary>
        /// Gets multiple Entities which satisfies the given condition from the collection of entities.
        /// </summary>
        /// <param name="condition">Condition to be satisfied</param>
        /// <param name="orderBy">Order by clause</param>
        /// <param name="descending">Order by descending?</param>
        /// <returns>Collection of Entities</returns>
        public IEnumerable<T> GetItems(Expression<Func<T, bool>> condition, Func<T, object> orderBy, bool descending)
        {
            IEnumerable<T> result = null;

            if (orderBy == null && condition == null)
            {
                // When condition and order by are not passed, return all the items.
                result = ObjectSet.ToList();
            }
            else if (orderBy != null && condition == null)
            {
                // When condition is not passed, only order by is passed, return data for given order by.
                if (descending)
                {
                    result = ObjectSet.OrderByDescending(orderBy).ToList();
                }
                else
                {
                    result = ObjectSet.OrderBy(orderBy).ToList();
                }
            }
            else if (orderBy == null && condition != null)
            {
                // When order by is not passed, only condition is passed, return data for given condition.
                result = ObjectSet.Where(condition).ToList();
            }
            else
            {
                // When both order by and condition are passed, return data for given condition and order by.
                if (descending)
                {
                    result = ObjectSet.Where(condition).OrderByDescending(orderBy).ToList();
                }
                else
                {
                    result = ObjectSet.Where(condition).OrderBy(orderBy).ToList();
                }
            }

            return result;
        }

        /// <summary>
        /// Gets multiple Entities which satisfies the given condition from the collection of entities.
        /// </summary>
        /// <param name="condition">Condition to be satisfied</param>
        /// <param name="orderBy">Order by clause</param>
        /// <param name="descending">Order by descending?</param>
        /// <param name="skipCount">Number of items to be skipped.</param>
        /// <param name="takeCount">Number of items to be picked up.</param>
        /// <returns>Collection of Entities</returns>
        public IEnumerable<T> GetItems(Expression<Func<T, bool>> condition, Func<T, object> orderBy, bool descending, int skipCount, int takeCount)
        {
            IEnumerable<T> result = null;

            if (orderBy == null && condition == null)
            {
                // When condition and order by are not passed, return all the items.
                result = ObjectSet
                    .Skip(skipCount)
                    .Take(takeCount)
                    .ToList();
            }
            else if (orderBy != null && condition == null)
            {
                // When condition is not passed, only order by is passed, return data for given order by.
                if (descending)
                {
                    result = ObjectSet
                        .OrderByDescending(orderBy)
                        .Skip(skipCount)
                        .Take(takeCount)
                        .ToList();
                }
                else
                {
                    result = ObjectSet
                        .OrderBy(orderBy)
                        .Skip(skipCount)
                        .Take(takeCount)
                        .ToList();
                }
            }
            else if (orderBy == null && condition != null)
            {
                // When order by is not passed, only condition is passed, return data for given condition.
                result = ObjectSet
                    .Where(condition)
                    .Skip(skipCount)
                    .Take(takeCount)
                    .ToList();
            }
            else
            {
                // When both order by and condition are passed, return data for given condition and order by.
                if (descending)
                {
                    result = ObjectSet.Where(condition)
                        .OrderByDescending(orderBy)
                        .Skip(skipCount)
                        .Take(takeCount)
                        .ToList();
                }
                else
                {
                    result = ObjectSet
                        .Where(condition).OrderBy(orderBy)
                        .Skip(skipCount)
                        .Take(takeCount)
                        .ToList();
                }
            }

            return result;
        }

        /// <summary>
        /// Gets all the Entities from the entities collection.
        /// </summary>
        /// <param name="orderBy">Order by clause</param>
        /// <returns>Collection of Entities</returns>
        public IEnumerable<T> GetAll(Func<T, object> orderBy)
        {
            if (orderBy == null)
            {
                return ObjectSet.ToList();
            }
            else
            {
                return ObjectSet.OrderBy(orderBy).ToList();
            }
        }

        /// <summary>
        /// Saves the changes made in the data models to the DCXL database.
        /// </summary>
        public void SaveChanges()
        {
            this.DCXLDbContext.SaveChanges();
        }
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.