惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Exploit Database - CXSecurity.com
A
Arctic Wolf
K
Kaspersky official blog
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy & Cybersecurity Law Blog
O
OpenAI News
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cisco Blogs
AWS News Blog
AWS News Blog
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
美团技术团队
T
Threatpost
S
Schneier on Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Cyber Attacks, Cyber Crime and Cyber Security
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes
Blog — PlanetScale
Blog — PlanetScale
C
Cybersecurity and Infrastructure Security Agency CISA
F
Full Disclosure
博客园_首页
N
Netflix TechBlog - Medium
Security Latest
Security Latest
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Register - Security
The Register - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Recent Announcements
Recent Announcements
博客园 - Franky
P
Palo Alto Networks Blog
Project Zero
Project Zero
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Cisco Talos Blog
Cisco Talos Blog
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 【当耐特】
GbyAI
GbyAI

博客园 - 学习.NET

听说微软要收购 winrar 用guid类型的字段作主键,用char(36)好还是用uniqueidentifier好? 批量插入与更新 批判工厂方法模式 李斯与赵高 MSDN Magazine 微软首席架构师Anders Hejlsberg谈C#的一些东西。 迷惘中 实现简单多层表头的办法 如何实现数据转移?不知这个叫法对不对。 什么是多套帐?如何实现? 代码复用 下载VS.NET2005 Beta1 人就是人 何时需要非规范化 复习基础知识:数据规范化 连接本机时,localhost的意思 正在出差,很长时间没来。 whidbey
请教泛型方法重载的一个小问题
学习.NET · 2006-05-29 · via 博客园 - 学习.NET

请教泛型方法重载的一个小问题

我用类似PetShop的方式实现三层架构,数据访问层有一个基类DataAccessObjectBase,实现思路如下:

1、两个实体类OrderInfo和OrderDatailInfo。(Model,作为在各层之间传递的数据对象)
2、数据访问层的基类:
public abstract class DataAccessObjectBase<TDomainObject> where TDomainObject : new()
{
    public string ObjectId;
    public string ParentObjectId;
    public string ChildObjectId;

    #region Common StoredProcedure Name List

    public string SelectByIdStoredProcedureName;

    public string SelectDetailObjectStoredProcedureName;
    public string SelectMasterObjectStoredProcedureName;
    #endregion

public abstract void LoadDomainObjectInfo(TDomainObject domainObject, System.Data.IDataReader dr);
public TDomainObject SelectById(System.Guid id)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand cmd = db.GetStoredProcCommand(SelectByIdStoredProcedureName);
            DataAccessHelper.PopulateIdParamter(db, cmd, id);

            TDomainObject domainObject = new TDomainObject();

            try
            {
                using (IDataReader dr = db.ExecuteReader(cmd))
                {
                    if (dr.Read())
                    {
                        LoadDomainObjectInfo(domainObject, dr);
                    }
                }
            }
            catch (InvalidOperationException ex)
            {
                //if (ExceptionPolicy.HandleException(ex, SR.ExceptionReplacePolicy)) throw;
            }
            catch (DataException ex)
            {
                //if (ExceptionPolicy.HandleException(ex, SR.ExceptionWrapPolicy)) throw;
            }
            return domainObject;
        }

public virtual void LoadDetailObjectInfo<TDetailObject>(TDetailObject detailObject, System.Data.IDataReader dr)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// 返回当前实体类的子对象,如订单类,则返回的是订单的订单明细
        /// </summary>
        /// <typeparam name="TDetailObject"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<TDetailObject> SelectDetailObject<TDetailObject>(System.Guid id) where TDetailObject : new()
        {
            if( String.IsNullOrEmpty(SelectDetailObjectStoredProcedureName ) ) return null;

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand cmd = db.GetStoredProcCommand(SelectDetailObjectStoredProcedureName);

            DataAccessHelper.PopulateIdParamter(db, cmd, id, ObjectId);

            List<TDetailObject> domainObjectList = new List<TDetailObject>();
            try
            {
                using (IDataReader dr = db.ExecuteReader(cmd))
                {
                    while (dr.Read())
                    {
                        TDetailObject detailObject = new TDetailObject();
                        LoadDetailObjectInfo<TDetailObject>((TDetailObject)detailObject, dr);

                        domainObjectList.Add(detailObject);
                        
                    }
                }
            }
            catch (InvalidOperationException ex)
            {
                //if (ExceptionPolicy.HandleException(ex, SR.ExceptionReplacePolicy)) throw;
            }
            catch (DataException ex)
            {
                //if (ExceptionPolicy.HandleException(ex, SR.ExceptionWrapPolicy)) throw;
            }
            return domainObjectList;
        }

订单数据访问类:继承于DataAccessObjectBase,
重载了抽象的和虚拟的方法,分别如下:
//////////////////////////////////
public override void LoadDomainObjectInfo(OrderInfo order, System.Data.IDataReader dr)
        {
            LoadAdminObjectInfo.LoadOrderInfo(order, dr);
        }

// 请教:这个方法应该如何重载?<=====================
public override void LoadDetailObjectInfo<TDetailObject>(TDetailObject detailObject, IDataReader dr)
        {
           
            LoadAdminObjectInfo.LoadOrderItemInfo(detailObject, dr);
        }

其中LoadAdminObjectInfo,是一个辅助类,有两个方法如下:
public static void LoadOrderInfo(OrderInfo order, System.Data.IDataReader dr)
        {
            order.OrderId = (System.Guid)dr["OrderId"];
            order.SequenceNumber = (System.Int32)dr["SequenceNumber"];
            order.Name = (System.String)dr["Name"];
           
        }
 public static void LoadOrderItemInfo(OrderItemInfo orderItem, System.Data.IDataReader dr)
        {
            orderItem.OrderItemId = (System.Guid)dr["OrderItemId"];
            orderItem.OrderId = (System.Guid)dr["OrderId"];
            orderItem.LocalId = (System.String)dr["LocalId"];
        }

对于具体的数据访问对象,LoadDetailObjectInfo应该如何重载?

posted on 2006-05-29 20:55  学习.NET  阅读(1386)  评论()    收藏  举报