Wednesday, January 30, 2013

MCSD: Web Applications Solutions Developer

Exam 70-480: Programming in HTML5 with JavaScript and CSS3 (Publication Date: April 22, 2013)

Exam 70-486: Developing ASP.NET MVC 4 Web Applications (Publication Date: May 22, 2013)

Exam Ref 70-487: Developing Windows Azure and Web Services (Publication Date: June 22, 2013)

Saturday, January 5, 2013

Twitter Bootstrap for MVC

See https://github.com/erichexter/twitter.bootstrap.mvc

Ninject Binary vs. Ninject Nuget Package

See Setting up an MVC3 application

EF Unit of Work and Repository Pattern

GenericRepository.cs

namespace SCNData
{
    public class GenericRepository<TEntity> where TEntity : class
    {
        internal NhacContext context;
        internal DbSet<TEntity> dbSet;
        public GenericRepository(NhacContext context)
        {
            this.context = context;
            this.dbSet = context.Set<TEntity>();
        }
        public virtual IEnumerable<TEntity> Get(
            Expression<Func<TEntity, bool>> filter = null,
            Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
            string includeProperties = "")
        {
            IQueryable<TEntity> query = dbSet;
            if (filter != null)
            {
                query = query.Where(filter);
            }
            foreach (var includeProperty in includeProperties.Split
                (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProperty);
            }
            if (orderBy != null)
            {
                return orderBy(query).ToList();
            }
            else
            {
                return query.ToList();
            }
        }
        public virtual TEntity GetByID(object id)
        {
            return dbSet.Find(id);
        }
        public virtual void Insert(TEntity entity)
        {
            dbSet.Add(entity);
        }
        public virtual void Delete(object id)
        {
            TEntity entityToDelete = dbSet.Find(id);
            Delete(entityToDelete);
        }
        public virtual void Delete(TEntity entityToDelete)
        {
            if (context.Entry(entityToDelete).State == EntityState.Detached)
            {
                dbSet.Attach(entityToDelete);
            }
            dbSet.Remove(entityToDelete);
        }
        public virtual void Update(TEntity entityToUpdate)
        {
            dbSet.Attach(entityToUpdate);
            context.Entry(entityToUpdate).State = EntityState.Modified;
        }
    }
}

UnitOfWork.cs

namespace SCNData
{
    public class UnitOfWork : IDisposable
    {
        private NhacContext context = new NhacContext();
        private GenericRepository<Performer> performerRepository;
        public GenericRepository<Performer> PerformerRepository
        {
            get
            {
                if (this.performerRepository == null)
                {
                    this.performerRepository = new GenericRepository<Performer>(context);
                }
                return performerRepository;
            }
        }
        public void Save()
        {
            context.SaveChanges();
        }
        private bool disposed = false;
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}

EF Repository Pattern

IPerformerRepository.cs

namespace SCNData
{
    public interface IPerformerRepository : IDisposable
    {
        IEnumerable<Performer> GetPerformers();
        Performer GetPerformerByID(int performerId);
        void InsertPerformer(Performer performer);
        void DeletePerformer(int performerID);
        void UpdatePerformer(Performer performer);
        void Save();
    }
}


PerformerRepository.cs

namespace SCNData
{
    public class PerformerRepository : IPerformerRepository, IDisposable
    {
        private NhacContext context;
        public PerformerRepository(NhacContext context)
        {
            this.context = context;
        }
        public IEnumerable<Performer> GetPerformers()
        {
            return context.Performers.ToList();
        }
        public Performer GetPerformerByID(int id)
        {
            return context.Performers.Find(id);
        }
        public void InsertPerformer(Performer performer)
        {
            context.Performers.Add(performer);
        }
        public void DeletePerformer(int performerId)
        {
            Performer performer = context.Performers.Find(performerId);
            context.Performers.Remove(performer);
        }
        public void UpdatePerformer(Performer performer)
        {
            context.Entry(performer).State = EntityState.Modified;
        }
        public void Save()
        {
            context.SaveChanges();
        }
        private bool disposed = false;
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}