博客
关于我
linq to sql 三层架构中使用CRUD操作
阅读量:802 次
发布时间: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/

    你可能感兴趣的文章
    Python:如何使用 plotly 制作阴影区域或交替背景颜色?
    查看>>
    Python:如何根据日期时间获取值的计数
    查看>>
    python:如何检测串行 COM 上的设备名称/ID
    查看>>
    Python:如何模拟 datetime.utcnow()?
    查看>>
    Python:如果DataFrames之间的其他值匹配,则对DataFrame中的值求和
    查看>>
    Python:字典思维
    查看>>
    Python:将 HEX 字符串转换为字节
    查看>>
    Python:将列表与范围列表合并
    查看>>
    Python:将列表列表初始化为一定大小,
    查看>>
    Python:带有生成器的给定集合的幂集
    查看>>
    Python:按字符的位置拆分字符串
    查看>>
    Python:替换嵌套字典中的值
    查看>>
    Python:爬取数据出现response.status_code为403解决方法
    查看>>
    Python:爬取数据出现response.status_code为418解决方法
    查看>>
    Python:生成“元组集“;来自“元组列表“;不考虑顺序
    查看>>
    Python:生成具有趋势的随机时间序列数据(例如周期性、指数衰减等)
    查看>>
    Python:用渐进式数字重命名列表中的重复项而不对列表进行排序
    查看>>
    Python:矩形参数无效
    查看>>
    Python:语法进阶
    查看>>
    Python:调用复制函数时调用 Python 对象时超出了最大递归深度
    查看>>