博客
关于我
linq to sql 三层架构中使用CRUD操作
阅读量:789 次
发布时间:2023-01-31

本文共 4359 字,大约阅读时间需要 14 分钟。

DbContext用于管理数据连接与操作,代码中使用了Entity Framework来实现数据增删改查。 GasBottles类暴露了基本的CRUD操作,并通过DbContext获取数据上下文,执行数据库操作。

以下是详细说明:


Class Structure

GasBottles 类实现了IGasBottles 接口,内部通过DbContext 操作数据库。主要方法包括:

  • GetModel

    根据ID查询特定记录,返回模型对象。

  • Add

    将模型对象插入数据库。

  • Update

    根据变化数据更新数据库记录。

  • Delete

    标记指定记录为已删除。

  • Save

    或新增或更新记录,返回新的ID。


  • Configuration

    DbContext 配置了数据上下文,支持两种运行环境:Winfrom方式Web方式。 在访问数据库时,لاح $"{contextClassName}DataContext" 会被缓存,以提升性能。

    具体实现如下:

    public static class DbContext {    private static string connectionstring = SqlHelper.SQLConnString;     public static WLMQGasBottlesDataContext GetWLMQDataContext()     {         var context = HttpContext.Current.Items["WLMQGasBottlesDataContext"] as WLMQGasBottlesDataContext;         if (context == null)         {             context = new WLMQGasBottlesDataContext(connectionstring);             HttpContext.Current.Items["WLMQGasBottlesDataContext"] = context;         }         return context;     }    public static LGSCMSDataContext GetLGSCMSDataContext()     {         var context = HttpContext.Current.Items["LGSCMSDataContext"] as LGSCMSDataContext;         if(context == null)         {             context = new LGSCMSDataContext(connectionstring);             HttpContext.Current.Items["LGSCMSDataContext"] = context;         }         return context;     }}

    Helper Method

    CopyProperties 作为辅助方法,用于将源对象的属性复制到目标对象中。这对于批量更新或部分属性修改非常有用。

    private void CopyProperties
    (ref T Target, T Source) { foreach (PropertyInfo prop in Target.GetType().GetProperties()) { if (prop.CanWrite && prop.CanRead) { prop.SetValue(Target, prop.GetValue(Source, null), null); } }}

    Implementation Details

    GetModel Method

    查询数据库中指定ID的记录。若存在,返回对应的模型对象;否则返回 null。

    public Model.GasBottles GetModel(int gasBottlesID) {    var db = DbContext.LGSCMSDataContext;     try     {         var gs = db.GasBottles.FirstOrDefault(s => s.ID == gasBottlesID);         if (gs != null)         {             Model.GasBottles gasBottlesInfo = gs.ConvertToEntity();             return gasBottlesInfo;         }     }     catch (Exception ex)     {         throw ex;     }     return null; }

    Add Method

    将模型对象转换为实体,插入数据库。

    public bool Add(Model.GasBottles gasBottles) {    try     {         var db = DbContext.LGSCMSDataContext;         var gs = gasBottles.ConvertToEntity();         db.GasBottles.InsertOnSubmit(gs);         db.SubmitChanges();         return true;     }     catch (Exception ex)     {         return false;         throw ex;     } }

    Update Method

    更新指定ID的记录。通过CopyProperties 方法复制源对象的属性到目标对象中。

    public bool Update(Model.GasBottles gasBottles) {    var changedData = gasBottles.ConvertToEntity();     var db = DbContext.LGSCMSDataContext;     try     {         var updateTarget = db.GasBottles.SingleOrDefault(i => i.ID == gasBottles.ID);         if (updateTarget != null)         {             CopyProperties(ref updateTarget, changedData);             db.SubmitChanges();         }         return true;     }     catch (Exception ex)     {         return false;         throw ex;     } }

    Delete Method

    标记记录为已删除。

    public bool Delete(int gasBottlesID) {    try     {         var db = DbContext.LGSCMSDataContext;         var gs = db.GasBottles.FirstOrDefault(s => s.ID == gasBottlesID);         if (gs != null)         {             gs.DeleteFlag = 1;             db.SubmitChanges();         }         return true;     }     catch (Exception ex)     {         return false;         throw ex;     } }

    Save Method

    用于新增或更新记录。 支持增删两种模式:新增(ID为 0)或更新(已有 ID)。

    public bool Save(Model.GasBottles gasBottles, out int gasBottlesID) {    var changedData = gasBottles.ConvertToEntity();     var db = DbContext.LGSCMSDataContext;     try     {         var updateTarget = db.GasBottles.SingleOrDefault(i => i.ID == gasBottles.ID);         if (updateTarget == null)         {             db.GasBottles.InsertOnSubmit(changedData);             db.SubmitChanges();             gasBottlesID = changedData.ID.ToInt();             return true;         }         else         {             CopyProperties(ref updateTarget, changedData);             db.SubmitChanges();             gasBottlesID = updateTarget.ID.ToInt();             return true;         }     }     catch (Exception ex)     {         return false;         throw ex;     } }

    总结

    本文展示了一个使用 Entity Framework 的数据层实现,包含了与数据库交互的各项操作方法。代码设计清晰,注重Type-Safe记忆 enjoyable阅读。

    转载地址:http://zuwfk.baihongyu.com/

    你可能感兴趣的文章
    leetcode题解98-验证二叉搜索树
    查看>>
    LeetCode题解【打家劫舍】(中等难度)
    查看>>
    Leetcode题解(二)
    查看>>
    left join on、where后面的条件的区别
    查看>>
    left join right inner join 区别
    查看>>
    leftjoin多个on条件_MySQL:left join 避坑指南
    查看>>
    legend2---开发日志3(thinkphp的入口目录是public的体现是什么)
    查看>>
    legoblock秀上限
    查看>>
    LeNet介绍-ChatGPT4o作答
    查看>>
    LeNet剪枝
    查看>>
    Length of Last Word
    查看>>
    Lenovo E47A Ubuntu闪屏解决办法
    查看>>
    Leopard系统装好后不能从硬盘引导的朋友看过来
    查看>>
    Lepus搭建企业级数据库全方位监控系统
    查看>>
    LESS 中的变量有什么作用?如何声明和使用变量?
    查看>>
    Less 日常用法
    查看>>
    Lettuce 移动框架 for Romantic
    查看>>
    let、const、var的四点区别( 代码示例 )
    查看>>
    LexPredict法律词典项目教程
    查看>>
    LF.73.Combinations Of Coins
    查看>>