c# - 性能和代码重用之间的平衡

标签 c# class oop functional-programming desktop-application

我目前正在做一个项目,因为该项目构建成很多行代码,所以我尝试采用简单的方法并开始使用一些更智能的方法来使代码可重用。 例如,我需要连接到 Sql Server:

(这可能是我在编码时正在学习的任何其他任务,我知道所有可用的 .Net 内置专用类)

我真正需要的是: SqlCommand , SqlDataReader (& SqlConnection ) 和适当的容器对象返回数据 通常我使用 List<dictionary<string, object>>对于我的数据库测试 所以到目前为止,我认为这是非常小的。 但是,然后我说啊,这是连接到 sql 并获取一些行集的好方法 然后我决定这还不够,因为还有一些其他类型的返回数据,所以让我们做一些替代方案来覆盖更多选项,实际上让我们覆盖所有可用的场景,然后我想出了一个充满“模型”的类所有可用的数据格式、单记录多行数据集等。 但这还不是全部 还有 2 个主要选项可以从 .NET 从 sql server 读取 喜欢SqlDataReaderSqlDataAdapter .所以我也会介绍这些选项,但是等等,还有我可以使用的魔法词,例如存储过程和纯文本命令,所以让我们创建一个类来包含一些结构来定义存储过程,然后是存储过程参数,让我们创建一种将存储过程作为对象调用的简单方法,因此稍后只需一行调用,我们就有了一个存储过程对象...

现在如果我要在这里停下来 因为这只是开始还有更多的东西在那个兔子洞里..而且这甚至不是最好的例子因为创建的对象有一个很好的理由存在,除非你想更有效地编码,否则你不会创建那些额外的类和对象 有了这种行为和“每天”要完成的更多任务,我开始看到有一种模式会导致一个巨大的 .Cs 文件,其中包含大约 10 个 namespace ,每个 namespace 上有数十个类和函数。 问题是你如何决定什么时候使用最少的代码,什么时候放手并且不太关心因为嘿,工作站正在说.. 8 核 I7 机器不会有太大区别,所以让我们只担心编码的难易程度。

你把红线放在哪里?

编辑下面的代码不是为了代码审查目的,它只是为了显示应用程序的规模和它可能导致的性能影响。**

这只是数据对象工厂的核心,大约占该类的 1/3,是其他实际任务中的少数几个之一。

public class Inventory
{
    public class DataCariers
    {
        public class DbDataCar
        {
            public Prototypes.DataCarPrototypes.CarTypeMeta CarierSpec { get; set; }
            public AlsDataCariers.Inventory.Compartments.DataCarCompartment Trunk { get; set; }
            public string CarierName { get; private set; }
            public DbDataCar(AlsTransMods.Direction bsDir, AlsTransMods.CarSize bsCs, AlsTransMods.useCar bsFamType, Compartments.DataCarCompartment bsTrunk)
            {
                this.CarierSpec = new Prototypes.DataCarPrototypes.CarTypeMeta()
                {
                    CarDirection = bsDir,
                    carOfSize = bsCs,
                    CarFamilySelected = bsFamType
                };
                this.Trunk = bsTrunk;
                this.Trunk.Value = new object();
                this.Trunk.compTypeMeta.CompartmentParent = this.GetType();

            }

            public DbDataCar(Prototypes.DataCarPrototypes.CarTypeMeta bsCarierSpec, Compartments.DataCarCompartment bsTrunk)
            {
                this.CarierSpec = bsCarierSpec;
                this.Trunk = bsTrunk;
                this.Trunk.Value = new object();
                this.Trunk.compTypeMeta.CompartmentParent = this.GetType();

            }
            internal void SetTunk(string CsvVal)
            {
                //this.Trunk = new AlsDataCariers.Inventory.Compartments.GenericsDataCarCompartment.Singles(CsvVal);
                if (this.CarierSpec.CarDirection == AlsTransMods.Direction.In)
                {
                    // new Compartments.MultipleRowsTabedMultipleRecordsCompartmnet(new prototypesFactory.DataCarPrototypes.selectionsTypeCreator(prototypesFactory.DataCarPrototypes.SelectionMode.selectionMultyRow).SelectedSelectionType);
                }
                else this.Trunk.Value = new Csv() { val = CsvVal };
            }
        }


        public class GenericDbDataCar : DbDataCar
        {
            public GenericDbDataCar(AlsTransMods.Direction bsDir, AlsTransMods.CarSize bsCs, AlsTransMods.useCar bsFamType, Compartments.DataCarCompartment bsTrunk)
                : base(bsDir, bsCs, bsFamType, bsTrunk)
            {
                this.CarierSpec = new Prototypes.DataCarPrototypes.CarTypeMeta();
                this.CarierSpec.CarDirection = bsDir;
                this.CarierSpec.carOfSize = bsCs;
                this.CarierSpec.CarFamilySelected = bsFamType;
                this.CarierSpec.CarOfType = this.GetType();

                base.CarierSpec = this.CarierSpec;

                //this.Trunk = this.TrunkCreate();
                //base.Trunk = this.Trunk;
            }

            public GenericDbDataCar(Prototypes.DataCarPrototypes.CarTypeMeta bsCarierSpec, Compartments.DataCarCompartment bsTrunk)
                : base(bsCarierSpec, bsTrunk)
            {
                this.CarierSpec.CarOfType = this.GetType();
                base.CarierSpec = this.CarierSpec;
            }

        }
        public class TabularDbDataCar : DbDataCar
        {
            public TabularDbDataCar(AlsTransMods.Direction bsDir, AlsTransMods.CarSize bsCs, AlsTransMods.useCar bsFamType, Compartments.DataCarCompartment bsTrunk)
                : base(bsDir,  bsCs, bsFamType, bsTrunk)
            {
                this.CarierSpec = new Prototypes.DataCarPrototypes.CarTypeMeta();
                //this.CarierName = string.Concat(bsCs, bsFamType);
                this.CarierSpec.CarDirection = bsDir;
                this.CarierSpec.carOfSize = bsCs;
                this.CarierSpec.CarFamilySelected = bsFamType;
                this.CarierSpec.CarOfType = this.GetType();
                base.CarierSpec = this.CarierSpec;
            }
            public TabularDbDataCar(Prototypes.DataCarPrototypes.CarTypeMeta bsCarierSpec, Compartments.DataCarCompartment bsTrunk):base(bsCarierSpec, bsTrunk)
            {
                this.CarierSpec.CarOfType = this.GetType();

            }

        }

    }
    public class Compartments
    {
        public class DataCarCompartment
        {
            //private Prototypes.DataCarPrototypes.selectionsType selectionsType;

            public RobCs509b.MyModels.Prototypes.DataCarPrototypes.CompartmentMeta compTypeMeta{get; private set;}
            public DataCarCompartment(RobCs509b.MyModels.Prototypes.DataCarPrototypes.CompartmentMeta Bs_CompTypeMeta)
            {
                this.compTypeMeta = Bs_CompTypeMeta;
                this.compTypeMeta.CompartmentSeed = this.GetType();

            }
            public virtual object Value { get; set; }

        }

        public class TabularDataCarCompartment : DataCarCompartment
        {
            public TabularDataCarCompartment(Prototypes.DataCarPrototypes.CompartmentMeta compartmentMeta)
                : base(compartmentMeta)
            {
                base.compTypeMeta.CompartmentFamilyType = this.GetType();
            }

        }
        public class TabedSingleRecordComartment : TabularDataCarCompartment
        {
            public TabedSingleRecordComartment(Prototypes.DataCarPrototypes.CompartmentMeta compartmentMeta)
                : base(compartmentMeta)
            {
                base.compTypeMeta.CompartmentDeliverBaseType = this.GetType();
                this.Value = new DataColumn();
                base.compTypeMeta.CompartmentDeliverType = this.Value.GetType();
            }
            public new DataColumn Value;

        }
        public class TabedMultipleRecordsCompartment : TabularDataCarCompartment
        {
            public TabedMultipleRecordsCompartment(Prototypes.DataCarPrototypes.CompartmentMeta compartmentMeta)
                : base(compartmentMeta)
            {
               base.compTypeMeta.CompartmentDeliverBaseType= this.GetType();
            }

        }
        public class TabedSingleRowMultipleRecordsCompartment : TabedMultipleRecordsCompartment
        {
            public TabedSingleRowMultipleRecordsCompartment(Prototypes.DataCarPrototypes.CompartmentMeta compartmentMeta)
                : base(compartmentMeta)
            {
                base.compTypeMeta.CompartmentDeliverBaseType = this.GetType();

                base.compTypeMeta.CompartmentDeliverType = this.Value.GetType();
                base.Value = this.Value;
            }
            public new DataRow Value;
        }
        public class MultipleRowsTabedMultipleRecordsCompartmnet : TabedMultipleRecordsCompartment
        {
            public MultipleRowsTabedMultipleRecordsCompartmnet(Prototypes.DataCarPrototypes.CompartmentMeta compartmentMeta)
                : base(compartmentMeta)
            {
                base.compTypeMeta.CompartmentDeliverBaseType = this.GetType();
                this.Value = new DataTable();
                base.compTypeMeta.CompartmentDeliverType = this.Value.GetType();
                base.Value = this.Value;
            }
            public new DataTable Value;
        }
        public class MultipleRowsClonedTabedMultipleRecordsCompartmnet : TabedMultipleRecordsCompartment
        {
            public MultipleRowsClonedTabedMultipleRecordsCompartmnet(Prototypes.DataCarPrototypes.CompartmentMeta compartmentMeta)
                : base(compartmentMeta)
            {
                base.compTypeMeta.CompartmentDeliverBaseType = this.GetType();
                this.Value = new ClonedSchemaDtt("NotSet");
                base.compTypeMeta.CompartmentDeliverType = this.Value.GetType();
            }
            public new ClonedSchemaDtt Value;
        }


        public class GenericDataCarCompartment : DataCarCompartment
        {
            public GenericDataCarCompartment(Prototypes.DataCarPrototypes.CompartmentMeta compartmentMeta)
                : base(compartmentMeta)
            {
                base.compTypeMeta.CompartmentFamilyType = this.GetType();
            }

        }
        public class GenericSingelRecordCompartment : GenericDataCarCompartment
        {
            public GenericSingelRecordCompartment(Prototypes.DataCarPrototypes.CompartmentMeta compartmentMeta)
                : base(compartmentMeta)
            {

            }
        }
        public class GenericSingleCsv :GenericSingelRecordCompartment
        {
            public GenericSingleCsv(string CsvVal,Prototypes.DataCarPrototypes.CompartmentMeta compartmentMeta)
                : base(compartmentMeta)
            {
                this.Value = new Csv();
                this.Value.val = CsvVal;
            }
            public new Csv Value;
        }
        public class GenericMultipleRecordsCompartment : GenericDataCarCompartment
        {
            public GenericMultipleRecordsCompartment(Prototypes.DataCarPrototypes.CompartmentMeta compartmentMeta)
                : base(compartmentMeta)
            {

            }
                //public RepetablevaluesCollections RepeatbleCollNameVal;
                //public UniqCollectionNameval UniqCollNameVal;
        }
        public class GenericSingleRowMultipleRecordsCompartment3s: GenericMultipleRecordsCompartment
        {
            public GenericSingleRowMultipleRecordsCompartment3s(Prototypes.DataCarPrototypes.CompartmentMeta compartmentMeta)
                : base(compartmentMeta)
            {
                base.compTypeMeta.CompartmentDeliverBaseType = this.GetType();
                this.Value = new MyGenericObject3<string, string, string>("", "", "");
                base.compTypeMeta.CompartmentDeliverType = this.Value.GetType();
            }
            /// <summary>
            /// MyGenericObject3 (string,string,string) values.
            /// </summary>
            public new MyGenericObject3<string, string, string> Value;
        }
        public class GenericMultipleRowsMyGenericObj3DataCarCompartment:GenericMultipleRecordsCompartment
        {
            public GenericMultipleRowsMyGenericObj3DataCarCompartment (Prototypes.DataCarPrototypes.CompartmentMeta compartmentMeta)
                : base(compartmentMeta)
            {
                base.compTypeMeta.CompartmentDeliverBaseType = this.GetType();

                this.Value = new List<MyGenericObject3<string, string, string>>();
                                                base.compTypeMeta.CompartmentDeliverType = this.Value.GetType();
                base.Value = this.Value;
            }
            /// <summary>
            /// DbMagaZine (List MyGenereicObject3) valueType
            /// </summary>
            public new List<MyGenericObject3<string, string, string>> Value;
        }

    }

    public class CompartmentTypes
    {
        private CompartmentTypes creator;
        public CompartmentTypes ()
        {
   }
    }
}



public class Compartments
{
    public class Tabular :Inventory.CompartmentTypes
    {
            Type CompartmentOfFamilyType = typeof(System.ComponentModel.MarshalByValueComponent);
            Type CompartmnetOfType = typeof(Tabular);
    }
    public class Generic : Inventory.CompartmentTypes
    {
            Type CompartmnetOfType = typeof(Generic);
    }
}

最佳答案

你的问题是,做同样的事情有太多的选择。 我的建议是使用最容易编程的技术,并且只有在出于性能原因绝对必要时才切换到较低级别的抽象。 并且不要比较简单情况下的性能,而要考虑整个系统的性能和维护。 我在 Entity Framework 之前的时代做过数据库编程,正确地做到这一点需要更多的专业知识和努力,所以我今天不推荐这个。

所以我按照以下顺序推荐技术:

  1. 纯 Entity Framework
  2. 带有特殊情况存储过程的 Entity Framework
  3. 数据集和手工逻辑
  4. 具有特殊情况存储过程的数据集
  5. 用于整个表的只读顺序访问的 SqlDatareader

关于c# - 性能和代码重用之间的平衡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33733401/

相关文章:

C# 获取泛型类的所有运行时构造类的列表

python - 在 Python 中,说我们 "override"内置运算符的行为准确吗?

c++ - 对父级的类引用

java - 如何使用 Class.getMethod 作为 getter?

javascript - javascript 中的 getter 和 setter

java - 当用户键入 'EXIT' 时,程序应关闭并显示所有先前的输入

c# - 创建txt文件无法访问其他进程正在使用的文件

c# - 如何限制我的通用接口(interface)使用的类型?

c# - Entity Framework - 如何过滤 EntityCollection 关系

c# - Asp.NET ListView数据控件动态Databind