Hi. Here is a simple class and interface I am using that will take care of most normal data access needs, using LightSpeed:
The class provides 5 methods:
GetAll, GetByPrimaryKey, Add, Update, Delete
To create a type-safe data access layer, you simply type this:
var dataAccessLayer = new LightSpeedDataAccess<YourClass, YourClassPrimaryKey>(connectionString);
Here you go:
using System;
using System.Collections.Generic;
public interface IDataAccess<T> : IDisposable
{
IEnumerable<T> GetAll();
T GetByPrimaryKey(object primaryKey);
void Delete(IEnumerable<T> data);
void Update(IEnumerable<T> data);
void Add(IEnumerable<T> data);
}
using System.Collections.Generic;
using Mindscape.LightSpeed.Linq;
using Mindscape.LightSpeed.Logging;
public class LightSpeedDataAccess<T1, T2> : IDataAccess<T1> where T1 : Mindscape.LightSpeed.Entity<T2>
{
Mindscape.LightSpeed.LightSpeedContext context;
Mindscape.LightSpeed.IUnitOfWork unitOfWork;
public LightSpeedDataAccess(string connectionString)
{
context = new Mindscape.LightSpeed.LightSpeedContext
{
ConnectionString = connectionString,
DataProvider = Mindscape.LightSpeed.DataProvider.MySql5
};
context.IdentityMethod = Mindscape.LightSpeed.IdentityMethod.IdentityColumn;
context.Logger = new ConsoleLogger();
unitOfWork = context.CreateUnitOfWork();
}
#region IDataAccess<T1> Members
public IEnumerable<T1> GetAll()
{
return unitOfWork.Query<T1>();
}
public T1 GetByPrimaryKey(object primaryKey)
{
return unitOfWork.FindOne<T1>(primaryKey);
}
public void Delete(IEnumerable<T1> data)
{
foreach (T1 sp in data)
{
unitOfWork.Remove(sp);
}
unitOfWork.SaveChanges();
}
public void Update(IEnumerable<T1> data)
{
unitOfWork.SaveChanges();
}
public void Add(IEnumerable<T1> data)
{
foreach (T1 o in data)
{
unitOfWork.Add(o);
}
unitOfWork.SaveChanges();
}
#endregion
#region IDisposable Members
public void Dispose()
{
if (unitOfWork != null)
unitOfWork.Dispose();
}
#endregion
}