S
shapper
Hello,
Lately I have been trying Entity Framework 3.5 but it does not support
Lazy Loading in a nice way.
There are a few hacks but they only gave me problems.
Is there a way to create a UnitOfWork, a BaseRepository and a
BaseService with LinqToSql?
I have done the following for EF:
public interface ISession {
void Commit();
void Dispose();
} // ISession
public partial class AppEntities : ISession {
public void Commit() {
SaveChanges();
} // Commit
public void Rollback() {
Dispose();
} // Rollback
} // AppEntities
public interface IRepository<T> {
void Create(T t);
void Delete(T t);
ObjectQuery<T> GetAll();
T GetById(Int32 id);
void Update(T t);
} // IRepository
public class Repository<T> : IRepository<T> {
private AppEntities _context;
public Repository(ISession session) {
if (session == null)
throw new ArgumentNullException("session");
_context = (AppEntities)session;
} // Repository
public void Create(T t) {
String table = typeof(T).Name;
_context.AddObject(table, t);
} // Create
public void Delete(T t) {
_context.DeleteObject(t);
} // Delete
public ObjectQuery<T> GetAll() {
String table = typeof(T).Name;
return _context.CreateQuery<T>(table);
} // GetAll
public T GetById(Int32 id) {
String table = typeof(T).Name;
EntityKey key = new EntityKey(_context.DefaultContainerName +
"." + table, "Id", id);
return (T)_context.GetObjectByKey(key);
} // GetById
public void Update(T t) {
String table = typeof(T).Name;
Int32 Id = GetPrimaryKeyValue(t);
T entity = GetById(Id);
entity = t;
_context.ApplyPropertyChanges(table, entity);
} // Update
private int GetPrimaryKeyValue(T t) {
Int32 id = 0;
PropertyInfo[] properties = typeof(T).GetProperties();
foreach (PropertyInfo p in properties) {
if (String.Compare(p.Name, "Id", true) == 0) {
String r = p.GetValue(t, null).ToString();
id = Int32.Parse(r);
break;
}
}
return id;
} // GetPrimaryKeyValue
} // Repository
public abstract class Service<TModel, T> where TModel : new() where
T : new() {
public Repository<T> repository;
protected Service(ISession session) {
repository = new Repository<T>(session);
TypeMap map = Mapper.FindTypeMapFor<T, TModel>();
if (map == null) {
Mapper.CreateMap<T, TModel>();
}
map = Mapper.FindTypeMapFor<TModel, T>();
if (map == null) {
Mapper.CreateMap<TModel, T>();
}
}
public virtual void Create(TModel tm) {
T t = Map(tm);
repository.Create(t);
} // Create
public virtual void Delete(T t) {
repository.Delete(t);
} // Delete
public virtual IEnumerable<TModel> GetAll() {
IQueryable<T> ts = repository.GetAll();
return Map(ts);
} // GetAll
public virtual TModel GetById(Int32 id) {
T t = repository.GetById(id);
return Map(t);
} // GetById
public virtual void Update(TModel tm) {
T t = Map(tm);
repository.Update(t);
} // Update
public virtual TModel Map(T t) {
return Mapper.Map<T, TModel>(t);
} // Map
public virtual T Map(TModel tm) {
return Mapper.Map<TModel, T>(tm);
} // Map
public virtual IEnumerable<TModel> Map(IQueryable<T> t) {
return Mapper.Map<IQueryable<T>, IEnumerable<TModel>>(t);
} // Map
public virtual IEnumerable<T> Map(IQueryable<TModel> tm) {
return Mapper.Map<IQueryable<TModel>, IEnumerable<T>>(tm);
} // Map
} // Service
public class ArticleService : Service<ArticleUI, Article> {
public ArticleService(ISession session) : base(session) {
} // ArticleService
public IEnumerable<ArticleUI> GetPublished() {
IQueryable<Article> articles = repository.GetAll().Where(a =>
a.Published == true);
return Map(articles);
IDictionary<Int32, String> b = new Dictionary<Int32, String>();
} // GetPublished
} // ArticleServices
Thanks,
Miguel
Lately I have been trying Entity Framework 3.5 but it does not support
Lazy Loading in a nice way.
There are a few hacks but they only gave me problems.
Is there a way to create a UnitOfWork, a BaseRepository and a
BaseService with LinqToSql?
I have done the following for EF:
public interface ISession {
void Commit();
void Dispose();
} // ISession
public partial class AppEntities : ISession {
public void Commit() {
SaveChanges();
} // Commit
public void Rollback() {
Dispose();
} // Rollback
} // AppEntities
public interface IRepository<T> {
void Create(T t);
void Delete(T t);
ObjectQuery<T> GetAll();
T GetById(Int32 id);
void Update(T t);
} // IRepository
public class Repository<T> : IRepository<T> {
private AppEntities _context;
public Repository(ISession session) {
if (session == null)
throw new ArgumentNullException("session");
_context = (AppEntities)session;
} // Repository
public void Create(T t) {
String table = typeof(T).Name;
_context.AddObject(table, t);
} // Create
public void Delete(T t) {
_context.DeleteObject(t);
} // Delete
public ObjectQuery<T> GetAll() {
String table = typeof(T).Name;
return _context.CreateQuery<T>(table);
} // GetAll
public T GetById(Int32 id) {
String table = typeof(T).Name;
EntityKey key = new EntityKey(_context.DefaultContainerName +
"." + table, "Id", id);
return (T)_context.GetObjectByKey(key);
} // GetById
public void Update(T t) {
String table = typeof(T).Name;
Int32 Id = GetPrimaryKeyValue(t);
T entity = GetById(Id);
entity = t;
_context.ApplyPropertyChanges(table, entity);
} // Update
private int GetPrimaryKeyValue(T t) {
Int32 id = 0;
PropertyInfo[] properties = typeof(T).GetProperties();
foreach (PropertyInfo p in properties) {
if (String.Compare(p.Name, "Id", true) == 0) {
String r = p.GetValue(t, null).ToString();
id = Int32.Parse(r);
break;
}
}
return id;
} // GetPrimaryKeyValue
} // Repository
public abstract class Service<TModel, T> where TModel : new() where
T : new() {
public Repository<T> repository;
protected Service(ISession session) {
repository = new Repository<T>(session);
TypeMap map = Mapper.FindTypeMapFor<T, TModel>();
if (map == null) {
Mapper.CreateMap<T, TModel>();
}
map = Mapper.FindTypeMapFor<TModel, T>();
if (map == null) {
Mapper.CreateMap<TModel, T>();
}
}
public virtual void Create(TModel tm) {
T t = Map(tm);
repository.Create(t);
} // Create
public virtual void Delete(T t) {
repository.Delete(t);
} // Delete
public virtual IEnumerable<TModel> GetAll() {
IQueryable<T> ts = repository.GetAll();
return Map(ts);
} // GetAll
public virtual TModel GetById(Int32 id) {
T t = repository.GetById(id);
return Map(t);
} // GetById
public virtual void Update(TModel tm) {
T t = Map(tm);
repository.Update(t);
} // Update
public virtual TModel Map(T t) {
return Mapper.Map<T, TModel>(t);
} // Map
public virtual T Map(TModel tm) {
return Mapper.Map<TModel, T>(tm);
} // Map
public virtual IEnumerable<TModel> Map(IQueryable<T> t) {
return Mapper.Map<IQueryable<T>, IEnumerable<TModel>>(t);
} // Map
public virtual IEnumerable<T> Map(IQueryable<TModel> tm) {
return Mapper.Map<IQueryable<TModel>, IEnumerable<T>>(tm);
} // Map
} // Service
public class ArticleService : Service<ArticleUI, Article> {
public ArticleService(ISession session) : base(session) {
} // ArticleService
public IEnumerable<ArticleUI> GetPublished() {
IQueryable<Article> articles = repository.GetAll().Where(a =>
a.Published == true);
return Map(articles);
IDictionary<Int32, String> b = new Dictionary<Int32, String>();
} // GetPublished
} // ArticleServices
Thanks,
Miguel