Sırası gelmişken şurada da
http://www.codeproject.com/Articles/363501/Repository-pattern-with-Entity-Framework-Code-Firs
böyle bir yapı var.
Emre Mert Asyalıoğlu
Selam Arkadaşlar,Daha önce katıldığım ama bir türlü (nedense) içine girmediğim DI, IoC, falan fişmekan teknolojilerin içine tekrar girdikten sonra Doğa'nın çaprısı üzerine tekrar döndüm. Zira hem sizden öğreneceğim çok şey var hem de soracaklarım :)Mesela ilk sorum acaba Unit of Work generic olabilir mi?Ben bir Generic Repository yapısı kurdum EF 5.0 Code First ile. Ayrıca Unit Of Work patternini de uyguladım.Acaba Unit of Work'ü generic yapabilir miyim yapamaz mıyım onu merak ediyorum.Eğer yapmazsam UoW içerisinde her bir repository için ayrı ayrı property'ler tanımlayacağım.Şayet UoW'u generic yaparsam (ki aslında yaptım) verdiğim generic entity'e uygun olan repo üzerinden yürüyebilirim.Bu konuda yorumunuz nedir?--
You received this message because you are subscribed to the Google Groups "altdotnetturkiye" group.
To post to this group, send email to altdotne...@googlegroups.com.
To unsubscribe from this group, send email to altdotnetturki...@googlegroups.com.
Visit this group at http://groups.google.com/group/altdotnetturkiye?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
IUnitOfWork.cs
namespace DbCore {
public interface IUnitOfWork {
void Dispose();
GenericRepository<Test> TestRepository {
get;
}
void Save();
}
}UnitOfWork.cs
namespace DbCore {
public class UnitOfWork : IDisposable, DbCore.IUnitOfWork {
private MyDbContext context = new MyDbContext ();
private GenericRepository<Test> testRepository;
public GenericRepository<Test> TestRepository {
get {
if(this.testRepository == null) {
this.testRepository = new GenericRepository<Test>(context);
}
return testRepository;
}
}
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);
}
}
}IGenericRepository.cs
namespace DbCore {
public interface IGenericRepository<TEntity>
where TEntity : class {
void Delete(object id);
void Delete(TEntity entityToDelete);
System.Collections.Generic.IEnumerable<TEntity> Get(System.Linq.Expressions.Expression<Func<TEntity, bool>> filter = null, Func<System.Linq.IQueryable<TEntity>, System.Linq.IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "");
System.Collections.Generic.IEnumerable<TEntity> GetAll();
TEntity GetByID(object id);
System.Collections.Generic.IEnumerable<TEntity> GetWithRawSql(string query, params object[] parameters);
void Insert(TEntity entity);
void Update(TEntity entityToUpdate);
}
}GenericRepository.cs
namespace DbCore {
public class SortExpression<TEntity, TType> {
Expression<Func<TEntity, TType>> SortProperty;
}
public class GenericRepository<TEntity> : DbCore.IGenericRepository<TEntity> where TEntity : class {
internal MyDbContext context;
internal DbSet<TEntity> dbSet;
public GenericRepository(MyDbContext 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 IEnumerable<TEntity> GetAll() {
return dbSet.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;
}
public virtual IEnumerable<TEntity> GetWithRawSql(string query, params object[] parameters) {
return dbSet.SqlQuery(query, parameters).ToList();
}
}
}e GenericRepository<Test> var ya, ben acaba UoW'u generic yapsam ve bunu da Repository olarak tanımlasam.Böylece UoW içerisine eklemem gereken her repo için ayrı ayrı prop yapmak
--
You received this message because you are subscribed to the Google Groups "altdotnetturkiye" group.
To post to this group, send email to altdotne...@googlegroups.com.
To unsubscribe from this group, send email to altdotnetturki...@googlegroups.com.
Visit this group at http://groups.google.com/group/altdotnetturkiye?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
Selam Arkadaşlar,Daha önce katıldığım ama bir türlü (nedense) içine girmediğim DI, IoC, falan fişmekan teknolojilerin içine tekrar girdikten sonra Doğa'nın çaprısı üzerine tekrar döndüm. Zira hem sizden öğreneceğim çok şey var hem de soracaklarım :)Mesela ilk sorum acaba Unit of Work generic olabilir mi?Ben bir Generic Repository yapısı kurdum EF 5.0 Code First ile. Ayrıca Unit Of Work patternini de uyguladım.Acaba Unit of Work'ü generic yapabilir miyim yapamaz mıyım onu merak ediyorum.Eğer yapmazsam UoW içerisinde her bir repository için ayrı ayrı property'ler tanımlayacağım.Şayet UoW'u generic yaparsam (ki aslında yaptım) verdiğim generic entity'e uygun olan repo üzerinden yürüyebilirim.Bu konuda yorumunuz nedir?
--
Alt. Net Doga'ya odaklanmis durumda.. :)
Insansiz hava araci ornegi gelebilir :)
--
You received this message because you are subscribed to the Google Groups "altdotnetturkiye" group.
To unsubscribe from this group and stop receiving emails from it, send an email to altdotnetturki...@googlegroups.com.
:)) Doga unuttuk sandin dimi ?
Bir Alt.Net ci asla unutmaz..
Senden biraz daha ses cikmasaydi ofise cukartma yapabikirdik zaten ;)
Emre Mert Asyalıoğlu