簡體   English   中英

相同的代碼針對不同的數據類型重復多次; 有沒有辦法使一個函數可以處理所有可能的類型?

[英]Exact same code repeated multiple times for different data types; Any way to make one function that can handle all possible types?

我正在嘗試優化其他人編寫的代碼。 一節中,有很多重復的代碼。 在第一行之后,有四個'if'語句,並且在其中,第一行是完全相同的代碼。 使用所有不同“ if”語句的原因是,根據用戶所在頁面的類型,對數據進行反序列化的方式有所不同。 但是,將數據反序列化后,每次使用的方式都完全相同。

if (smartFormId == EktronSmartForms.StandardPage)
{
    var pageData = (EkXml.Deserialize(typeof(StandardPage), ContentData.Html) as StandardPage);
    var sections = pageData.LeftContent.AdditionalSection;
    title = pageData.LeftContent.ParentBreadcrumbTitle;

    if (sections != null)
    {
        foreach (var item in sections)
        {
            tempLD = new LinkData();
            tempLD.Text = item.SectionTitle;
            tempLD.Class = "class=\"sub-parent\"";
            autoData.Add(tempLD);

            if (item.Link != null && item.Link.Count() > 0)
            {
                foreach (var child in item.Link)
                {
                    tempLD = new LinkData();
                    tempLD.Text = child.a.OuterXML;
                    tempLD.Link = child.a.href;
                    tempLD.Class = "class=\"\"";
                    autoData.Add(tempLD);
                }
            }
        }
    }

}
else if (smartFormId == EktronSmartForms.DoctorPage)
{
    var pageData =
        (EkXml.Deserialize(typeof(DoctorProfilePage), ContentData.Html) as DoctorProfilePage);
    var sections = pageData.LeftContent.AdditionalSection;
    title = pageData.LeftContent.ParentBreadcrumbTitle;

    if (sections != null)
    {
        foreach (var item in sections)
        {
            tempLD = new LinkData();
            tempLD.Text = item.SectionTitle;
            tempLD.Class = "class=\"sub-parent\"";
            autoData.Add(tempLD);

            if (item.Link != null && item.Link.Length > 0)
            {
                foreach (var child in item.Link)
                {
                    tempLD = new LinkData
                    {
                        Text = child.a.OuterXML,
                        Link = child.a.href,
                        Class = "class=\"\""
                    };
                    autoData.Add(tempLD);
                }
            }
        }
    }
}
else if (smartFormId == EktronSmartForms.StoryPage)
{
    var pageData = (EkXml.Deserialize(typeof(StoryPage), ContentData.Html) as StoryPage);
    var sections = pageData.LeftContent.AdditionalSection;
    title = pageData.LeftContent.ParentBreadcrumbTitle;

    if (sections != null)
    {
        foreach (var item in sections)
        {
            tempLD = new LinkData();
            tempLD.Text = item.SectionTitle;
            tempLD.Class = "class=\"sub-parent\"";
            autoData.Add(tempLD);

            if (item.Link != null && item.Link.Length > 0)
            {
                foreach (var child in item.Link)
                {
                    tempLD = new LinkData
                    {
                        Text = child.a.OuterXML,
                        Link = child.a.href,
                        Class = "class=\"\""
                    };
                    autoData.Add(tempLD);
                }
            }
        }
    }
}
else if (smartFormId == EktronSmartForms.MaintainedPage)
{
    var pageData =
        (EkXml.Deserialize(typeof(MaintainedPage), ContentData.Html) as MaintainedPage);
    var sections = pageData.LeftContent.AdditionalSection;
    title = pageData.LeftContent.ParentBreadcrumbTitle;

    if (sections != null)
    {
        foreach (var item in sections)
        {
            tempLD = new LinkData();
            tempLD.Text = item.SectionTitle;
            tempLD.Class = "class=\"sub-parent\"";
            autoData.Add(tempLD);

            if (item.Link != null && item.Link.Length > 0)
            {
                foreach (var child in item.Link)
                {
                    tempLD = new LinkData
                    {
                        Text = child.a.OuterXML,
                        Link = child.a.href,
                        Class = "class=\"\""
                    };
                    autoData.Add(tempLD);
                }
            }
        }
    }
}

但是,由於pageData的創建方式,每個if語句的類型不同。 首先是StandardPage,各部分是StandardPageLeftContentAdditionalSections; 在第二個頁面中,Data是DoctorPage,部分是DoctorPageLeftContentAdditionalSections; 等等...

我想制作一個函數,在其中可以傳輸所有重復的代碼,並只在每個“ if”語句(或更好的是,在所有“ if”語句的末尾)中調用該函數,但是(1)我可以” t在if語句之前聲明pageData和sections,因為如果我聲明

var sections = new StandardPageLeftContentAdditionalSections();

如果我嘗試這樣做會收到轉換錯誤

pageData = (EkXml.Deserialize(typeof(DoctorProfilePage), ContentData.Html) as DoctorProfilePage);
sections = pageData.LeftContent.AdditionalSection; 

我想做的是這樣的:

var sections = ????
if (smartFormId == EktronSmartForms.StandardPage){
    var pageData = (EkXml.Deserialize(typeof(StandardPage), ContentData.Html) as StandardPage);
    sections = pageData.LeftContent.AdditionalSection;
}
else if (smartFormId == EktronSmartForms.DoctorPage)
{
    var pageData =
        (EkXml.Deserialize(typeof(DoctorProfilePage), ContentData.Html) as DoctorProfilePage);
    sections = pageData.LeftContent.AdditionalSection;
}
etc...

autoData = ProcessSections(type??? sections, List<LinkData> autoData);

________________________________________________________________________________

private List<Link> ProcessSections(type??? sections, List<LinkData> autoData){
    if (sections != null)
    {
        foreach (var item in sections)
        {
            tempLD = new LinkData();
            tempLD.Text = item.SectionTitle;
            tempLD.Class = "class=\"sub-parent\"";
            autoData.Add(tempLD);

            if (item.Link != null && item.Link.Length > 0)
            {
                foreach (var child in item.Link)
                {
                    tempLD = new LinkData
                    {
                        Text = child.a.OuterXML,
                        Link = child.a.href,
                        Class = "class=\"\""
                    };
                    autoData.Add(tempLD);
                }
            }
        }
    }
    return autoData;
}

鑒於節每次都是不同的數據類型的問題,有什么方法可以簡化這一點?

使用界面:

public interface IPageData{
    object LeftContent{get;} //Use the correct return type rather than 'object' here
}

然后,在聲明頁面類型時,請確保實現接口:

public class StandardPage:IPageData {
    ...
}
public class DoctorPage:IPageData{
    ...
}

然后,您可以反序列化為一個通用對象:

IPageData pageData;
switch(smartFormId) {
    case EktronSmartForms.StandardPage:
        pageData = (EkXml.Deserialize(typeof(StandardPage), ContentData.Html) as StandardPage);
        break;
    ...
}

var sections = pageData.LeftContent.AdditionalSection;
title = pageData.LeftContent.ParentBreadcrumbTitle;

if (sections != null)
{
    foreach (var item in sections)
    {
        tempLD = new LinkData();
        tempLD.Text = item.SectionTitle;
        tempLD.Class = "class=\"sub-parent\"";
        autoData.Add(tempLD);

        if (item.Link != null && item.Link.Count() > 0)
        {
            foreach (var child in item.Link)
            {
                tempLD = new LinkData();
                tempLD.Text = child.a.OuterXML;
                tempLD.Link = child.a.href;
                tempLD.Class = "class=\"\"";
                autoData.Add(tempLD);
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM