通用知识库是一个好主意,如果你只是使用CRUD操作,(创建,读取,更新和删除) 对我来说,我不喜欢通过DBML生成数据库实体,我通常写实体手动的映射,在这里下的一个项目我就
第一模式工作的一些例子
public class Operator
{
public virtual string OperatorName { get; set; }
public virtual string LoginName { get; set; }
public virtual string Email { get; set; }
public virtual string PhoneNo { get; set; }
public virtual short Status { get; set; }
public virtual byte[] Password { get; set; }
}
UPDATE: 使库的独立供应商,你应该定义自定义背景的该存储库首先使用 定义i覆盖整个院落
public interface IGenericContext
{
void Add<T>(T entity)
where T : class;
void Update<T>(T entity)
where T : class;
void Delete<T>(T entity)
where T : class;
IQueryable<T> GetIQueryable<T>()
where T : class;
}
这里实现了IGenericContext接口EF上下文的样本
public class EFContext : DbContext, IGenericContext
{
public EFContext(string connectionName)
: base(connectionName)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.AddFromAssembly(this.GetType().Assembly);
base.OnModelCreating(modelBuilder);
}
public void Add<T>(T entity) where T : class
{
this.Set<T>().Add(entity);
this.SaveChanges();
}
public void Update<T>(T entity) where T : class
{
this.Set<T>().Attach(entity);
this.Entry(entity).State = EntityState.Modified;
this.SaveChanges();
}
public void Delete<T>(T entity) where T : class
{
this.Set<T>().Attach(entity);
this.Set<T>().Remove(entity);
this.SaveChanges();
}
public IQueryable<T> GetIQueryable<T>() where T : class
{
return this.Set<T>();
}
}
为EF,你需要自己做的映射,比如下面
public class OperatorMapping : EntityTypeConfiguration<Operator>
{
public OperatorMapping()
{
this.ToTable("Operators");
this.Property(t => t.OperatorName).IsRequired().HasColumnName("OperatorName");
this.HasKey(t => t.LoginName).Property(t => t.LoginName).HasColumnName("LoginName");
this.Property(t => t.Email).IsRequired().HasColumnName("Email");
this.Property(t => t.PhoneNo).HasColumnName("PhoneNo");
this.Property(t => t.Status).IsRequired().HasColumnName("Status");
this.Property(t => t.Password).IsRequired().HasColumnName("Password");
}
}
然后一个通用存储库,此存储库将使用IGenericContext而不是在EF DBContext上工作
public class Repository<TEntity>
where TEntity : class, new()
{
protected IGenericContext Context { get; set; }
public Repository(IGenericContext context)
{
Context = context;
}
public void Add(TEntity entity)
{
Context.Add(entity);
}
public void Update(TEntity entity)
{
Context.Update(entity);
}
public void Delete(TEntity entity)
{
Context.Delete(entity);
}
public List<TEntity> ToList()
{
return Context.GetIQueryable<TEntity>().ToList();
}
}
所有你需要做的是创造的DbContext,它传递到通用存储库,做你的作业
IGenericContext context = new EFContext("Infrastructure");
Repository<Operator> repository = new Repository<Operator>(context);
var operators = repository.ToList();
然后你可以实现另一个背景下,做基本的CRUD操作,仍然可以使用同一个版本库
我会创建一个通用的存储库。如果一些实体不需要超过基本CRUD,那么在我看来,为每个实体创建一个独立的课程是毫无意义的。并且 - reppository是模式,没有EF - 数据库只是保持数据的一种方式。你也可以创建在XML上运行的repo,在别的东西上运行JSON。 – 2014-11-03 07:22:17
是的,这是我最初的想法......但我一直在努力通过创建合适的类来定义这些操作。我发现的所有示例都只使用EF来实现存储库模式。您能否提供一个实现通用存储库的示例,我可以相应地进行复制和定制。 – pulkitgulati 2014-11-03 07:31:58
Generic Repository是一种反模式,它只有在你序列化对象时才有效。在您的具体情况下,我会直接使用EF Code First,它完全符合您的要求。如果你真的讨厌EF,可以试试NHibernate或者微型ORM。 – MikeSW 2014-11-04 18:42:34