|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Linq.Expressions;
using System.Data;
using System.Data.Objects.DataClasses;
using System.Data.Common;
using Demo.Framework;
namespace Demo.Data
{
public sealed class DemoRepository<T> : IRepository<T>
where T : EntityObject
{
static readonly DemoRepository<T> instance
= new DemoRepository<T>(new DemoCoreEntities());
private static readonly Object _lockObject = new Object();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static DemoRepository()
{
if (_lockObject == null)
_lockObject = new Object();
}
public static DemoRepository<T> Instance
{
get
{
return instance;
}
}
DemoRepository(ObjectContext repositoryContext)
{
_repositoryContext = repositoryContext ?? new DemoCoreEntities();
_objectSet = _repositoryContext.CreateObjectSet<T>();
}
private static ObjectContext _repositoryContext;
private ObjectSet<T> _objectSet;
public ObjectSet<T> ObjectSet
{
get
{
return _objectSet;
}
}
#region IRepository Members
public IRepository<T> GetRepository()
{
return Instance;
}
public void Add(T entity)
{
lock (_lockObject)
{
this._objectSet.AddObject(entity);
_repositoryContext.SaveChanges();
_repositoryContext.AcceptAllChanges();
}
}
public void Update(T entity)
{
lock (_lockObject)
{
_repositoryContext.ApplyOriginalValues(((IEntityWithKey)entity)
.EntityKey.EntitySetName, entity);
_repositoryContext.Refresh(RefreshMode.ClientWins, _objectSet);
_repositoryContext.SaveChanges();
_repositoryContext.AcceptAllChanges();
}
}
public void Delete(T entity)
{
lock (_lockObject)
{
this._objectSet.DeleteObject(entity);
_repositoryContext.Refresh(RefreshMode.ClientWins, _objectSet);
_repositoryContext.SaveChanges();
_repositoryContext.AcceptAllChanges();
}
}
public void DeleteAll()
{
_repositoryContext
.ExecuteStoreCommand("DELETE " + _objectSet.EntitySet.ElementType.Name);
}
public IList<T> GetAll()
{
lock (_lockObject)
{
return this._objectSet.ToList<T>();
}
}
public IList<T> GetAll(Expression<Func<T, bool>> whereCondition)
{
lock (_lockObject)
{
return this._objectSet.Where(whereCondition).ToList<T>();
}
}
public T GetSingle(Expression<Func<T, bool>> whereCondition)
{
lock (_lockObject)
{
return this._objectSet.Where(whereCondition).FirstOrDefault<T>();
}
}
public IQueryable<T> GetQueryable()
{
lock (_lockObject)
{
return this._objectSet.AsQueryable<T>();
}
}
public long Count()
{
lock (_lockObject)
{
return this._objectSet.LongCount<T>();
}
}
public long Count(Expression<Func<T, bool>> whereCondition)
{
lock (_lockObject)
{
return this._objectSet.Where(whereCondition).LongCount<T>();
}
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
In-depth coverage of Microsoft .Net, Cloud and many other cutting-edge Technologies.
-
The Mandelbrot set – someone has called it the thumb-print of God – is one of the most beautiful and remarkable discoveries in the entire history of mathematics. My profile picture is generated with that equation.
You may contact Nirosh for Consultations, Code Reviews and Architecture Guide Workshops via c_nir*o*sh@hotmail.com (Remove * to use)
View Nirosh L.W.C.'s profile on LinkedIn
Other Links