“设计pattern"用于ADO中的CRUD.. NET实体框架

本文关键字:CRUD 中的 NET 实体 框架 ADO 用于 pattern 设计 quot | 更新日期: 2023-09-27 18:12:29

我想问你是否存在一些"设计模式"的CRUD在ADO。. NET实体框架4.1.

例如这个方法从DB加载对象。

private static Project GetObjects(int id)
{
    var connSetting = ConfigurationManager.ConnectionStrings["ProjectEntities"];
    var conn = new EntityConnection(connSetting.ConnectionString);
    conn.Open();
    using(var contex= new ProjectEntities(conn))
    {
        var project = (from p in contex.Projects
                       where p.ProjectId == id
                       select p).FirstOrDefault();
        contex.Detach(project);
        return project;
    }
}

this method do update:

private static void UpdateObject(Project obj)
{
    var connSetting = ConfigurationManager.ConnectionStrings["ProjectEntities"];
    var conn = new EntityConnection(connSetting.ConnectionString);
    conn.Open();
    using (var contex = new ProjectEntities(conn))
    {
        var entity = (Project)contex.GetObjectByKey(obj.EntityKey);
        contex.ApplyCurrentValues(entity.EntityKey.EntitySetName, obj);
        contex.SaveChanges();
    }
}

和这个方法删除对象

private static void DeleteObject(Project obj)
{
    var connSettings = ConfigurationManager.ConnectionStrings["ProjectEntities"];
    var conn = new EntityConnection(connSettings.ConnectionString);
    conn.Open();
    using(var ctx = new ProjectEntities())
    {
        var entity = (Project)ctx.GetObjectByKey(obj.EntityKey);
        ctx.DeleteObject(entity);
        ctx.SaveChanges();
    }
}

我想有一个很好的通用类的CRUD与ADO。NET EF in DB…你能帮我吗?获得链接与良好的样本?

“设计pattern"用于ADO中的CRUD.. NET实体框架

如果你让它们泛型,你的方法有什么问题?

你的问题的答案是主观的,因为第一个问题是你的方法是否真的有用?简单CRUD通常不与EF一起使用。EF提供了更多的功能,而CRUD则违背了它的附加功能。另一个问题是你使用的实体类型。EntityKey是基于EntityObject实体的特性——这些实体目前肯定被认为是不推荐的,因为整个EF都在转向DbContext API和poco。

那么如何为poco制作一些可重用的CRUD方法呢?

让我们为你的实体定义接口:

public interface IEntity 
{
    int Id { get; set; }
}

在你的实体中实现这个接口,现在你可以定义简单的可重用方法:

private static T GetObject<T>(int id) where T : class, IEntity     
{
    using(var context = GetContext())
    {
        // What if you want to load relations as well?
        var entity = (from x in context.CreateObjectSet<T>()
                      where x.Id == id
                      select p).FirstOrDefault();
        // Typical issue - you cannot use neither eager, explicit or lazy 
        // loading
        context.Detach(entity);
        return entity;
    }
}
private static void UpdateObject<T>(T entity) where T : class, IEntity
{
    using (var contex = GetContext())
    {
        // This works for POCOs but for EntityObject based entities you will have
        // to use your approach (combine it with GetObject method to load entity)
        context.Attach(entity);
        context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
        context.SaveChanges();
    }
}
private static void DeleteObject<T>(int id) where T : class, IEntity, new()
{
    using(var context = GetContext())
    {
        // You need only dummy entity with key to perform delete
        T entity = new T { Id = id };
        context.Attach(entity);
        context.DeleteObject(entity);
        context.SaveChanges();
    }
}

注意GetContext -你正在寻找可重用性,所以也开始重构你的可重用方法。如果您需要使用基于EntityObject的实体,将EntityObject添加到通用类型T的约束中,而不是class