我应该使用新类还是创建部分?

时间:2014-11-13 13:28:55

标签: c# oop

情况:我有一个生成XML的类,XML的内容非常大。现在我为每个节点都有一个函数,但我注意到这个类变得非常大。我想阻止这个,所以我想为每个节点创建一个新类。我的一个女主人有不同的想法,他建议创建部分课程。

你会建议什么以及为什么?

相关代码:

public class IbTaxReturnTagCreator : TagCreatorBase
{
    private readonly IMathLibQueryClient mathLibQueryClient;
    private readonly IUserIdentity userIdentity;

    public IbTaxReturnTagCreator(ILynxDataUnitOfWork unitOfWork,
        IMathLibQueryClient mathLibQueryClient,
        IUserIdentity userIdentity)
        : base(unitOfWork)
    {
        this.mathLibQueryClient = mathLibQueryClient;
        this.userIdentity = userIdentity;
    }

    protected override void WriteTag(Domain.TaxReturn taxReturn)
    {
        WriteStartingIbTag(taxReturn);

        WriteEmployeeTag(taxReturn);

        WriteRelationTag(taxReturn);

        WriteFeatures(taxReturn);

        WriteClosingIbTag();
    }

    private void WriteStartingIbTag(Domain.TaxReturn taxReturn)
    {
        XmlWriter.WriteStartElement("ib");
        WriteIbAttributes(taxReturn);
    }

    private void WriteIbAttributes(Domain.TaxReturn taxReturn)
    {
        // Write attributes in ib tag
        XmlWriter.WriteAttributeString("txrid", taxReturn.Id.ToString());
        XmlWriter.WriteAttributeString("guid", taxReturn.Guid.ToString());
        XmlWriter.WriteAttributeString("sofinr", taxReturn.FiscalNumber);
        XmlWriter.WriteAttributeString("belastingjaar", taxReturn.TaxYear.ToString());
        XmlWriter.WriteAttributeString("valuta", taxReturn.Currency);
        XmlWriter.WriteAttributeString("lastformview", taxReturn.LastFormOpen);
        if (taxReturn.InspectionOffice == null)
        {
            XmlWriter.WriteAttributeString("inspectie", string.Empty);
        }
        else
        {
            XmlWriter.WriteAttributeString("inspectie", taxReturn.InspectionOffice.Code);
        }
        XmlWriter.WriteAttributeString("beconnr", taxReturn.BeconNumber);
        if (taxReturn.TaxReturnPartner == null)
        {
            XmlWriter.WriteAttributeString("partnerguid", string.Empty);
        }
        else
        {
            XmlWriter.WriteAttributeString("partnerguid", taxReturn.TaxReturnPartner.Guid.ToString());
        }
        XmlWriter.WriteAttributeString("partnersofinr", "3.0");
        XmlWriter.WriteAttributeString("biljetsoort", "3.0");
    }

    private void WriteEmployeeTag(Domain.TaxReturn taxReturn)
    {
        XmlWriter.WriteStartElement("medewerker");
        XmlWriter.WriteAttributeString("code", taxReturn.Employee.Name);
        XmlWriter.WriteAttributeString("indienst", ToUSDateFormat(taxReturn.Employee.EmployedDateTime));
        XmlWriter.WriteAttributeString("uitdienst", ToUSDateFormat(taxReturn.Employee.DischargeDateTime));

        XmlWriter.WriteStartElement("relatie");
        XmlWriter.WriteAttributeString("sofinr", taxReturn.Employee.Customer.FiscalNumber);
        XmlWriter.WriteAttributeString("zoeknaam", taxReturn.Employee.Customer.SearchName);
        XmlWriter.WriteAttributeString("type", taxReturn.Employee.Customer.CustomerType);
        XmlWriter.WriteAttributeString("voorletters", taxReturn.Employee.Customer.Initials);
        XmlWriter.WriteAttributeString("tussenvoegsels", taxReturn.Employee.Customer.PrefixLastName);
        XmlWriter.WriteAttributeString("achternaam", taxReturn.Employee.Customer.LastName);
        XmlWriter.WriteAttributeString("geslacht", taxReturn.Employee.Customer.Gender.Id.ToString());
        XmlWriter.WriteAttributeString("gebdatum", ToUSDateFormat(taxReturn.Employee.Customer.BirthDateTime));
        XmlWriter.WriteAttributeString("ovldatum", ToUSDateFormat(taxReturn.Employee.Customer.DeceasedDateTime));

        XmlWriter.WriteStartElement("postadres");
        XmlWriter.WriteAttributeString("straat", taxReturn.Employee.Customer.PaStreet);
        XmlWriter.WriteAttributeString("straat2", taxReturn.Employee.Customer.PaStreet2);
        XmlWriter.WriteAttributeString("huisnr", taxReturn.Employee.Customer.PaHouseNumber);
        XmlWriter.WriteAttributeString("huisnrtoe", taxReturn.Employee.Customer.PaHouseNumberPostfix);
        XmlWriter.WriteAttributeString("postcode", taxReturn.Employee.Customer.PaZipCode);
        XmlWriter.WriteAttributeString("plaats", GetCity(taxReturn.Employee.Customer.PaCity));
        XmlWriter.WriteAttributeString("landcode", GetCountry(taxReturn.Employee.Customer.PaCountry));
        XmlWriter.WriteEndElement();

        XmlWriter.WriteStartElement("woonadres");
        XmlWriter.WriteAttributeString("WAstraat", taxReturn.Employee.Customer.HaStreet);
        XmlWriter.WriteAttributeString("WAstraat2", taxReturn.Employee.Customer.HaStreet2);
        XmlWriter.WriteAttributeString("WAhuisnr", taxReturn.Employee.Customer.HaHouseNumber);
        XmlWriter.WriteAttributeString("WAhuisnrtoe", taxReturn.Employee.Customer.HaHouseNumberPostfix);
        XmlWriter.WriteAttributeString("WApostcode", taxReturn.Employee.Customer.HaZipCode);
        XmlWriter.WriteAttributeString("plaats", GetCity(taxReturn.Employee.Customer.HaCity));
        XmlWriter.WriteAttributeString("landcode", GetCountry(taxReturn.Employee.Customer.HaCountry));
        XmlWriter.WriteEndElement();

        XmlWriter.WriteEndElement(); // End relation tag
        XmlWriter.WriteEndElement(); // End employee tag
    }

    private void WriteRelationTag(Domain.TaxReturn taxReturn)
    {
        XmlWriter.WriteStartElement("relatie");

        XmlWriter.WriteAttributeString("sofinr", taxReturn.Customer.FiscalNumber);
        XmlWriter.WriteAttributeString("zoeknaam", taxReturn.Customer.SearchName);
        XmlWriter.WriteAttributeString("type", taxReturn.Customer.CustomerType);
        XmlWriter.WriteAttributeString("voorletters", taxReturn.Customer.Initials);
        XmlWriter.WriteAttributeString("tussenvoegsels", taxReturn.Customer.PrefixLastName);
        XmlWriter.WriteAttributeString("achternaam", taxReturn.Customer.LastName);
        XmlWriter.WriteAttributeString("geslacht", taxReturn.Customer.Gender.Id.ToString());
        XmlWriter.WriteAttributeString("gebdatum", ToUSDateFormat(taxReturn.Customer.BirthDateTime));
        XmlWriter.WriteAttributeString("ovldatum", ToUSDateFormat(taxReturn.Customer.DeceasedDateTime));

        XmlWriter.WriteStartElement("postadres");
        XmlWriter.WriteAttributeString("straat", taxReturn.Customer.PaStreet);
        XmlWriter.WriteAttributeString("straat2", taxReturn.Customer.PaStreet2);
        XmlWriter.WriteAttributeString("huisnr", taxReturn.Customer.PaHouseNumber);
        XmlWriter.WriteAttributeString("huisnrtoe", taxReturn.Customer.PaHouseNumberPostfix);
        XmlWriter.WriteAttributeString("postcode", taxReturn.Customer.PaZipCode);
        XmlWriter.WriteAttributeString("plaats", GetCity(taxReturn.Customer.PaCity));
        XmlWriter.WriteAttributeString("landcode", GetCountry(taxReturn.Customer.PaCountry));
        XmlWriter.WriteEndElement();

        XmlWriter.WriteStartElement("woonadres");
        XmlWriter.WriteAttributeString("WAstraat", taxReturn.Customer.HaStreet);
        XmlWriter.WriteAttributeString("WAstraat2", taxReturn.Customer.HaStreet2);
        XmlWriter.WriteAttributeString("WAhuisnr", taxReturn.Customer.HaHouseNumber);
        XmlWriter.WriteAttributeString("WAhuisnrtoe", taxReturn.Customer.HaHouseNumberPostfix);
        XmlWriter.WriteAttributeString("WApostcode", taxReturn.Customer.HaZipCode);
        XmlWriter.WriteAttributeString("plaats", GetCity(taxReturn.Customer.HaCity));
        XmlWriter.WriteAttributeString("landcode", GetCountry(taxReturn.Customer.HaCountry));
        XmlWriter.WriteEndElement();

        XmlWriter.WriteEndElement(); // End relation tag
    }

    private void WriteFeatures(Domain.TaxReturn taxReturn)
    {
        XmlWriter.WriteStartElement("features");

        var featureValues = GetFeatureValues(taxReturn);
        foreach (var feature in featureValues)
        {
            XmlWriter.WriteStartElement("feature");

            XmlWriter.WriteAttributeString("id", feature.Name);
            XmlWriter.WriteCData(feature.Value);

            XmlWriter.WriteEndElement(); // Closing tag feature
        }

        var featureTables = GetFeatureTableValues(taxReturn).GroupBy(f => f.FeatureName);
        foreach (var feature in featureTables)
        {
            XmlWriter.WriteStartElement("table");
            XmlWriter.WriteAttributeString("id", feature.Key);

            var rows = feature.OrderBy(f => f.RowId).Select(f => f.RowId).Distinct();
            foreach (var row in rows)
            {
                XmlWriter.WriteStartElement("row");

                foreach (var subfeature in feature.Where(f => f.RowId == row))
                {
                    XmlWriter.WriteStartElement("subfeature");
                    XmlWriter.WriteAttributeString("id", subfeature.Name);
                    XmlWriter.WriteCData(subfeature.Value);
                    XmlWriter.WriteEndElement();
                }

                XmlWriter.WriteEndElement(); // Closing row tag
            }

            XmlWriter.WriteEndElement(); // Closing table feature
        }

        var featuresIafWoInvestment = GetIafSubFeaturesWithoutInvestment(taxReturn).GroupBy(f => f.FeatureName);
        foreach (var feature in featuresIafWoInvestment)
        {
            XmlWriter.WriteStartElement("table");
            XmlWriter.WriteAttributeString("id", feature.Key);

            var rows = feature.OrderBy(f => f.Row).Select(f => f.Row).Distinct();
            foreach (var row in rows)
            {
                XmlWriter.WriteStartElement("row");

                foreach (var subfeature in feature.Where(f => f.Row == row))
                {
                    XmlWriter.WriteStartElement("subfeature");
                    XmlWriter.WriteAttributeString("id", subfeature.SubFeatureName);
                    XmlWriter.WriteCData(subfeature.SubFeatureValue);
                    XmlWriter.WriteEndElement();
                }

                XmlWriter.WriteEndElement(); // Closing row tag
            }

            XmlWriter.WriteEndElement(); // Closing table feature
        }

        var featuresIafWithInvestments = GetIafSubFeaturesWithInvestment(taxReturn).GroupBy(f => f.FeatureName);
        foreach (var feature in featuresIafWithInvestments)
        {
            XmlWriter.WriteStartElement("table");
            XmlWriter.WriteAttributeString("id", feature.Key);
            XmlWriter.WriteAttributeString("iafguid", feature.First().InvestmentGuid.Value.ToString());
            XmlWriter.WriteAttributeString("iafzoeknaam", feature.First().InvestmentName);

            var rows = feature.OrderBy(f => f.Row).Select(f => f.Row).Distinct();
            foreach (var row in rows)
            {
                XmlWriter.WriteStartElement("row");

                foreach (var subfeature in feature.Where(f => f.Row == row))
                {
                    XmlWriter.WriteStartElement("subfeature");
                    XmlWriter.WriteAttributeString("id", subfeature.SubFeatureName);
                    XmlWriter.WriteCData(subfeature.SubFeatureValue);
                    XmlWriter.WriteEndElement();
                }

                XmlWriter.WriteEndElement(); // Closing row tag
            }

            XmlWriter.WriteEndElement(); // Closing table features
        }

        XmlWriter.WriteEndElement(); // Closing features features
    }

    private void WriteClosingIbTag()
    {
        XmlWriter.WriteEndElement();
    }

    private string GetCountry(Domain.Country country)
    {
        var countryString = string.Empty;

        if (country != null)
            countryString = country.Code;

        return countryString;
    }

    private string GetCity(Domain.City city)
    {
        var cityString = string.Empty;

        if (city != null)
            cityString = city.Name;

        return cityString;
    }

    private FeatureNameValueView[] GetFeatureValues(Domain.TaxReturn taxReturn)
    {
        var featureValues = this.mathLibQueryClient.GetAllFeatureValues(this.userIdentity.UserId, taxReturn.Id);
        return featureValues;
    }

    private SubFeatureDetailView[] GetFeatureTableValues(Domain.TaxReturn taxReturn)
    {
        var featureNames = this.mathLibQueryClient.GetAllSubFeatureValues(this.userIdentity.UserId, taxReturn.Id);
        return featureNames;
    }

    private SubFeatureInvestmentDetailView[] GetIafSubFeaturesWithoutInvestment(Domain.TaxReturn taxReturn)
    {
        var featureNames = this.mathLibQueryClient.GetInvestmentSubFeatures(this.userIdentity.UserId, taxReturn.Id, false);
        return featureNames;
    }

    private SubFeatureInvestmentDetailView[] GetIafSubFeaturesWithInvestment(Domain.TaxReturn taxReturn)
    {
        var featureNames = this.mathLibQueryClient.GetInvestmentSubFeatures(this.userIdentity.UserId, taxReturn.Id, true);
        return featureNames;
    }
}

2 个答案:

答案 0 :(得分:2)

实际上,文档中的每个节点都是具有属性的对象。因此,您可以为每个嵌套的对象类型实现一个类。这样你也可以使用简单的XML序列化技术,而不是自己编写所有东西。

答案 1 :(得分:0)

仅仅因为课程变大而使用部分课程就是黑客攻击。部分类可以使用工具来为您单独创建类的一部分。我认为目前你的班级有一个可管理的大小,你是唯一写作它的实体。

如果你想以面向对象的方式思考这个问题,那么任何一个类都应该能够序列化自己,并且应该有一个接口来声明这一点。如果您有其他方式来表示它们,您也可以稍后对其进行扩展。每次任何节点的表示发生变化时,您都不必更改一个主类,而是可以更改该特定节点的类(Employee,Relation等)。这有助于您的源代码控制和版本控制。