我目前正在处理一个项目,因为该项目构建成了许多行代码,所以我尝试采用琐碎的方法,并开始使用一些更智能的方法来使代码可重用。例如,我需要连接到SqlServer:
(这可能是我在编码时学习的任何其他任务,我知道所有可用的.Net内置专用类)
我真正需要的只是:SqlCommand
、SqlDataReader
(&SqlConnection
)和适当的容器对象来返回数据通常我使用List<dictionary<string, object>>
进行数据库测试所以到目前为止,我认为这是非常微小的。但是,我说啊,这是连接到sql并获取一些行集的好方法然后我决定这还不够,因为还有其他类型的返回数据,所以让我们做一些替代方案来涵盖更多的选项,实际上让我们涵盖所有可用的场景,然后我提出了一个充满"模型"的类,涵盖所有可用数据格式、单记录多行数据集等。但这还不是全部。还有两个主要选项可以从sql server中从.NET中读取如CCD_ 5和CCD_。所以我也会介绍这些选项,但等等,还有一些神奇的词我可以使用,比如存储过程和纯文本命令,所以让我们创建一个类,它将包含一些定义存储过程的语句,然后还有存储过程参数,让我们以一种简单的方式将存储过程作为对象调用,所以稍后一行调用,我们就有了一个存储过程对象。。。
现在,如果我就停在这里,因为这只是开始,兔子洞里还有更多。。这甚至不是最好的例子,因为创建的对象有很好的理由存在,除非你想更有效地编码它,否则你不会创建那些额外的类和对象有了这种行为和更多的"日常"任务,我开始看到有一种模式导致了一个巨大的.Cs文件,该文件包含大约10个名称空间,每个名称空间上有几十个类和函数。问题是,你如何决定什么时候使用最小代码,什么时候让它过去,而不太在乎,因为嘿,工作站正在运行。。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);
}
}
您的问题是,有太多的选项可以做同样的事情。我的建议是使用最容易编程的技术,只有在出于性能原因绝对必要的情况下才切换到较低级别的抽象。不要比较简单情况下的性能,而是要考虑整个系统的性能和维护。我在实体框架之前的时代做过数据库编程,正确地完成这项工作需要更多的专业知识和精力,所以我今天不建议这样做。
因此,我建议按以下顺序使用技术:
- 纯实体框架
- 具有特殊情况存储过程的实体框架
- 数据集和手工制作的逻辑
- 具有特殊情况存储过程的数据集
- 用于对整张表进行只读顺序访问的SqlDatareader