2015-11-16 53 views
3

我目前正在一個項目上工作,因爲項目建立了許多代碼行,所以我試圖採取微不足道的方法,並開始使用一些更聰明的方法來使代碼可重複使用的。 所以比如我需要連接到SQL Server:性能和代碼resue之間的平衡

(它可以是任何其他的任務,我正在學習,而我的代碼我所知availble的所有的.NET內置專用類)

真的所有我需要的是: SqlCommandSqlDataReader(& SqlConnection)及相應的容器對象與數據庫 所以我的測試中返回 通常我使用List<dictionary<string, object>>數據到現在爲止它是很小的,我應該想到。 但是,然後我說啊這是一個很好的方式來連接到SQL並獲取一些rowsets 然後我認爲這是不夠的,因爲還有一些其他種類的返回的數據,所以讓我們做一些替代方案來覆蓋更多的選項,實際上讓我們介紹所有可用的場景,然後我想出了一個包含所有可用數據格式,單記錄多行數據集等的「模型」類。 但這並不是全部還有兩個主要選項可從.NET 從sql server讀取,如SqlDataReaderSqlDataAdapter。所以我會覆蓋這些選項,但是等等,還有我可以使用的魔術詞,比如存儲過程和純文本命令,所以讓我們創建一個類,它將存儲一些破壞來定義存儲過程,然後存儲過程參數,讓做一個簡單的方法來調用存儲過程作爲一個對象,所以後來1行的電話,我們有一個存儲過程的對象......

現在如果我將停止在這裏原因這又是唯一的從那個兔子洞開始,還有很多...甚至不是最好的例子,因爲創建的對象有一個很好的理由來存在,除非你想更有效地編碼它,否則你不會創建那些額外的類和對象 這種行爲和更多的任務,以「每日」爲基礎,我開始看到有一個該模式導致一個巨大的.Cs文件,其中包含大約10個命名空間,每個命名空間都有數十個類和函數。 問題是你如何決定什麼時候使用最少的代碼,什麼時候讓它去,不關心很多原因,嘿,工作站是說.. 8核心I7機器不會有太大的區別,所以我們只需要擔心編碼的簡便性。

你在哪裏放紅線?

編輯下面的代碼是不是一個代碼審查的目的,它只是顯示的應用和性能的影響很可能導致規模。**

這只是的核心數據對象工廠,這是約三分之一的類其中一些在其他任務中的少數幾個。

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

我認爲這將更適合「代碼審查」堆棧交換站點。 – Eterm

回答

1

你們的問題是,有太多的選擇來做同樣的事情。 我的建議是使用最容易編程的技術,如果出於性能原因絕對需要,只能切換到較低級別的抽象。 不要比較簡單案例的性能,而是考慮整個系統的性能和維護。 我在前實體框架時間做了數據庫編程,它需要更多的專業知識和努力才能正確實現,所以我不會推薦這樣做。

  1. 實體框架純
  2. 實體框架與特殊情況
  3. 數據集和手工製作的邏輯
  4. 數據集與特殊的存儲過程的存儲過程:

    所以我按以下順序reccommend技術案例

  5. SqlDatareader只讀順序訪問整個表
相關問題