对于不同的数据类型,完全相同的代码重复多次;任何可以处理所有可能类型的函数的方法? [英] Exact same code repeated multiple times for different data types; Any way to make one function that can handle all possible types?

查看:124
本文介绍了对于不同的数据类型,完全相同的代码重复多次;任何可以处理所有可能类型的函数的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图优化别人编写的代码。在一节中,它有很多重复的代码;有四个'if'语句,在第一行之后是完全相同的代码。所有不同'if'语句的原因在于,取决于用户所在页面的类型,数据反序列化的方式不同;然而,在数据反序列化之后,每次使用完全相同。

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

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

if(item.Link!= null&& item.Link.Count()> 0)
{
foreach(item.Link中的var child)
{
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)作为DoctorProfilePage);
var sections = pageData.LeftContent.AdditionalSection;
title = pageData.LeftContent.ParentBreadcrumbTitle;

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

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




$ b其他if(smartFormId == EktronSmartForms.StoryPage)
{
var pageData =(EkXml.Deserialize(typeof(StoryPage),ContentData.Html)作为StoryPage);
var sections = pageData.LeftContent.AdditionalSection;
title = pageData.LeftContent.ParentBreadcrumbTitle;

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

if(item.Link!= null&& item.Link.Length> 0)
{
foreach(item.Link中的var child)
{
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)作为MaintainedPage);
var sections = pageData.LeftContent.AdditionalSection;
title = pageData.LeftContent.ParentBreadcrumbTitle;

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

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

但是,由于pageData的创建方式不同,每个if语句都有不同的类型;第一个是StandardPage,第一个是StandardPageLeftContentAdditionalSections;在第二页数据是DoctorPage,部分是DoctorPageLeftContentAdditionalSections;等等...



我想创建一个函数,我可以传递所有重复的代码,并在每个'if'语句内调用该函数(或更好(1)我不能在if语句之前声明pageData和section,因为如果我声明

  var sections = new StandardPageLeftContentAdditionalSections(); 

如果我尝试操作,会出现转换错误

  pageData =(EkXml.Deserialize(typeof(DoctorProfilePage),ContentData.Html)作为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;
}
等...

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

________________________________________________________________________________

私人列表< Link> ProcessSections(type ???部分,List< LinkData> autoData){
if(sections!= null)
{
foreach(var部分项)
{
tempLD = new LinkData();
tempLD.Text = item.SectionTitle;
tempLD.Class =class = \sub-parent \;
autoData.Add(tempLD);

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





$ b

有没有什么办法可以简化这个问题,每次都有不同的数据类型?

解决方案

使用接口:

<$
对象LeftContent {get;} //在这里使用正确的返回类型而不是'object'
}

然后当你声明你的页面类型时,确保你实现了这个接口:

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

然后,您可以反序列化为一个通用对象:

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

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

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

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


I'm trying to optimize some code that was written by somebody else. In one section, it has a lot of repeated code; there are four 'if' statements, and inside one, after the first line is the exact same code. The reason for all the different 'if' statements is that depending on the type of page the user is on, the data is deserialized differently; however, after the data is deserialized, it is used exactly the same each time.

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);
                }
            }
        }
    }
}

However, because of how pageData is created, it has a different type for each if statement; in the first it's StandardPage and sections is StandardPageLeftContentAdditionalSections; in the second pageData is DoctorPage and sections is DoctorPageLeftContentAdditionalSections; etc...

I'd like to make one function where I can transfer all the repeated code, and just call that function inside each 'if' statement (or better, at the end of all the 'if' statements) but (1) I can't declare pageData and sections before the if statements, because if I declare

var sections = new StandardPageLeftContentAdditionalSections();

I'll get a conversion error if I try to do

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

What I would like to do is this:

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;
}

Is there any way that I can simplify this, given the problem of sections being a different data type every time?

解决方案

Use an interface:

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

Then when you declare your page types make sure you implement the interface:

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

Then you can deserialise into a common object:

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);
            }
        }
    }
}

这篇关于对于不同的数据类型,完全相同的代码重复多次;任何可以处理所有可能类型的函数的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆