using System.Linq;
using Organization.Project.Common.Dal;
using System.Data.Linq;
namespace Organization.Project.Dal
{
///
/// Base L2S repository for different types of entities
///
/// entity interface
/// entity type
public class BaseLinqRepository : IRepository where TEntity : class, IEntity, new()
{
#region User Defined Variables
private readonly DataContext context;
#endregion
#region Constructor
///
/// Constructor
///
/// L2S context
public BaseLinqRepository(DataContext context)
{
this.context = context;
}
#endregion
#region Public Methods
///
/// Gets IQueryable of all the entities of some type from the database (with possibility
/// to add some additional filters, sortings, etc. before the query really executes)
///
/// IQueryable of all the entities of some type
public IQueryable GetAll()
{
return context.GetTable().Select(e => (IEntity)e);
}
///
/// Adds a new entity to the context so it will be added to the database on submit
///
/// entity object
/// if result of the operation is successful
public bool Add(IEntity entity)
{
var cEnt = entity as TEntity;
if (cEnt != null)
{
context.GetTable().InsertOnSubmit(cEnt);
return true;
}
return false;
}
///
/// Deletes a new entity from the context so it will be removed from the database on submit
///
/// entity object
/// if result of the operation is successful
public bool Delete(IEntity entity)
{
var cEnt = entity as TEntity;
if (cEnt != null)
{
context.GetTable().DeleteOnSubmit(cEnt);
return true;
}
return false;
}
///
/// Creates an empty entity
///
/// an entity through its common interface
public IEntity CreateEmpty()
{
return new TEntity();
}
///
/// Refresh an entity by values from database
///
/// an entity to refresh
public void Refresh(IEntity entity)
{
context.Refresh(RefreshMode.OverwriteCurrentValues, entity);
}
#endregion
}
}