随笔 - 46  文章 - 36 评论 - 43 阅读 - 40357

我总结的三层结构之二:IDAL示例

 

简单字典表的C# IDAL接口示例

 

数据表tFolk定义:

create table tFolk (

Id int identity(1,1) primary key,

Title nvarchar(50) not null unique

);

go

 

这是个简单的字典表,Id是主键;Title不允许空值,有唯一性约束。其对应IDAL接口如下:

 

//============================================================

// Module:              C# IDAL interface of data table [tFolk]

// Auto generated by:     mySagasoft CodeAssistant for MSPetShop3Tiers of sagahu@163.com, at 2012-07-29 14:50:40

// Usage:                     

// Last modified by: sagahu@163.com

// Last modified at:  2012-07-29 14:50:40

//============================================================

 

using System;

using System.Collections.Generic;

using System.Text;

 

using System.Data;

using mySagasoft.CodeTemplates.Models;  // 命名空间需要根据实际项目修改

 

namespace mySagasoft.CodeTemplates.IDAL // 命名空间需要根据实际项目修改

{

    public partial interface IFolkService

    {

        // 4个基本方法

        void Add(Folk model);

        void Delete(int? pk);

        void Update(Folk model);

        Folk GetModel(int? pk);

 

        // 2个返回多条记录的方法

        DataTable GetTable(string filter);

        IList<Folk> GetList(string filter);

 

        // 2个库内分页查询的方法

        DataTable GetTablePage(

            out int rowsCount, out int pagesCount,

            string where, string orderBy, int pageSize, int pageIndex);

        IList<Folk> GetPage(

            out int rowsCount, out int pagesCount,

            string where, string orderBy, int pageSize, int pageIndex);

 

        // 根据唯一性字段查询实体:由于本数据表除了主键之外,还有其它唯一性值字段,所以有下面的方法

        Folk GetModelByTitle(string title);

 

        // 如果需要使用悲观sql,还需要下面两个方法

        void Delete(Folk model);

        void Update(Folk newModel, Folk oldModel);

    }

}

 

简单业务数据表的C# IDAL接口示例

 

数据表tStudent定义:

create table tStudent (

Id int identity(1,1) primary key,

Name nvarchar(50) not null,

       Birthday datetime,

       FolkId int,

       IdentityCode varchar(20) not null unique,

       Photo image

);

go

 

这是个比较简单的业务表,Id是主键;IdentityCode不允许空值,有唯一性约束;Photo是大对象(large object)类型的字段。C# IDAL接口代码如下:

 

//============================================================

// Module:              C# IDAL interface of data table [tStudent]

// Auto generated by:     mySagasoft CodeAssistant for MSPetShop3Tiers of sagahu@163.com, at 2012-07-29 14:50:40

// Usage:                     

// Last modified by: sagahu@163.com

// Last modified at:  2012-07-29 14:50:40

//============================================================

 

using System;

using System.Collections.Generic;

using System.Text;

 

using System.Data;

using mySagasoft.CodeTemplates.Models;  // 命名空间需要根据实际项目修改

 

namespace mySagasoft.CodeTemplates.IDAL // 命名空间需要根据实际项目修改

{

    public partial interface IStudentService

    {

        // 4个基本方法

        void Add(Student model);    // 不包含大对象(large object)字段和它的值

        void Delete(int? pk);

        void Update(Student model); // 不包含大对象(large object)字段和它的值

        Student GetModel(int? pk);

 

        // 2个返回多条记录的方法,不包含大对象(large object)字段和它的值

        DataTable GetTable(string filter);

        IList<Student> GetList(string filter);

 

        // 2个库内分页查询的方法,不包含大对象(large object)字段和它的值

        DataTable GetTablePage(

            out int rowsCount, out int pagesCount,

            string where, string orderBy, int pageSize, int pageIndex);

        IList<Student> GetPage(

            out int rowsCount, out int pagesCount,

            string where, string orderBy, int pageSize, int pageIndex);

 

        // 根据唯一性字段查询实体:由于本数据表除了主键之外,还有其它唯一性值字段,所以有下面的方法

        Student GetModelByIdentityCode(string identityCode);

 

        // 由于本数据表具有大对象(large object)字段,所有还需要以下方法

        void UpdatePhoto(int? pk, byte[] photo);    // 根据主键单独更新大对象(large object)字段

        // 2个返回多条记录的方法,包含大对象(large object)字段和它的值

        DataTable GetTableWithLob(string filter);

        IList<Student> GetListWithLob(string filter);

        // 2个库内分页查询的方法,包含大对象(large object)字段和它的值

        DataTable GetTablePageWithLob(

            out int rowsCount, out int pagesCount,

            string where, string orderBy, int pageSize, int pageIndex);

        IList<Student> GetPageWithLob(

            out int rowsCount, out int pagesCount,

            string where, string orderBy, int pageSize, int pageIndex);

 

        // 如果需要使用悲观sql,还需要下面两个方法

        void Delete(Student model);

        void Update(Student newModel, Student oldModel);

    }

}

 

简单视图的C# IDAL接口示例

 

视图vwStudentView定义:

create view vwStudentView

       as

       select s.Id,s.Name,s.Birthday,s.FolkId,f.Title,s.IdentityCode,s.Photo from tStudent as s

       left join tFolk as f on s.FolkId=f.Id;

go

 

在这个简单视图里,Id、IdentityCode是唯一性键值。C# IDAL接口代码如下:

 

//============================================================

// Module:              C# IDAL interface of view [vwStudentViewService]

// Auto generated by:     mySagasoft CodeAssistant for MSPetShop3Tiers of sagahu@163.com, at 2012-07-29 14:50:40

// Usage:                     

// Last modified by: sagahu@163.com

// Last modified at:  2012-07-29 14:50:40

//============================================================

 

using System;

using System.Collections.Generic;

using System.Text;

 

using System.Data;

using mySagasoft.CodeTemplates.Models;  // 命名空间需要根据实际项目修改

 

namespace mySagasoft.CodeTemplates.IDAL // 命名空间需要根据实际项目修改

{

    public partial interface IStudentViewService

    {

        // 2个返回多条记录的方法,不包含大对象(large object)字段和它的值

        DataTable GetTable(string filter);

        IList<StudentView> GetList(string filter);

 

        // 2个库内分页查询的方法,不包含大对象(large object)字段和它的值

        DataTable GetTablePage(

            out int rowsCount, out int pagesCount,

            string where, string orderBy, int pageSize, int pageIndex);

        IList<StudentView> GetPage(

            out int rowsCount, out int pagesCount,

            string where, string orderBy, int pageSize, int pageIndex);

 

        // 根据唯一性字段查询实体:本视图有2个唯一性值的字段,所有有以下方法

        StudentView GetModelById(int? id);

        StudentView GetModelByIdentityCode(string identityCode);

 

        // 由于本视图具有大对象(large object)字段,所有还需要以下方法

        // 2个返回多条记录的方法,包含大对象(large object)字段和它的值

        DataTable GetTableWithLob(string filter);

        IList<StudentView> GetListWithLob(string filter);

        // 2个库内分页查询的方法,包含大对象(large object)字段和它的值

        DataTable GetTablePageWithLob(

            out int rowsCount, out int pagesCount,

            string where, string orderBy, int pageSize, int pageIndex);

        IList<StudentView> GetPageWithLob(

            out int rowsCount, out int pagesCount,

            string where, string orderBy, int pageSize, int pageIndex);

    }

}

 

补充思考

 

上面的接口是在这样的假定条件下设计的:数据表主键字段是单一的,不是多字段组合的情况;并且主键值在插入记录后不会再修改。否则上面设计不完全合适。

 

posted on   萨迦狐  阅读(471)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示