簡體   English   中英

我是在類的內部還是外部編寫這些using語句?

[英]Do I write these using statements inside or outside the class?

我有一個編寫的類,其中包含對從數據庫中提取的數據執行計算的不同方法。 每個方法都從數據庫中提取信息並對其進行計算,然后將其保存到該類中的變量中,當我初始化該類時可以訪問該變量。 我下面有兩個偽代碼方法,並且我試圖找出以下內容:

  1. 如果每個方法都提取數據然后對其執行計算,那么應該使用using語句的哪種方式?
  2. 我是否使用最有效的方式存儲數據?

這是我初始化類時所做的

    public class Calculations
    {
    public string Symbol { get; set; }
    public string Market { get; set; }
    public List<decimal> stockPctReturns { get; set; }
    public List<decimal> marketPctReturns { get; set; }
    public enum returnType { Market, Stock };
    public decimal avgAnnualMarketGrowth { get; set; }
    public List<decimal> stockPctGains { get; set; }
    public List<decimal> stockPctLosses { get; set; }
    public List<decimal> positiveMoneyFlow { get; set; }
    public List<decimal> negativeMoneyFlow { get; set; }
    public decimal rsi { get; set; }
    public decimal mfi { get; set; }
    public decimal williamsR { get; set; }
    public decimal sma20 { get; set; }
    public decimal sma50 { get; set; }
    public decimal sma200 { get; set; }
    public const decimal riskFree10Yr = 2.58M;

    public Calculations(string symbol, string market)
    {
        // initialize everything in the class
        Symbol = symbol;
        Market = market;
    }
}

這樣做:

public void fillPctReturns()
    {
        stockPctReturns = new List<decimal>();
        marketPctReturns = new List<decimal>();

        using (DailyGlobalDataTableAdapter dailyGlobalAdapter = new DailyGlobalDataTableAdapter())
        using (DailyAmexDataTableAdapter dailyAmexAdapter = new DailyAmexDataTableAdapter())
        using (DailyGlobalDataTable dailyGlobalTable = new DailyGlobalDataTable())
        using (DailyAmexDataDataTable dailyAmexTable = new DailyAmexDataDataTable())
        {
            dailyAmexAdapter.Fill(dailyAmexTable);
            dailyGlobalAdapter.Fill(dailyGlobalTable);
                    var amexQuery = from c in dailyGlobalTable.AsEnumerable()
                                    where c.Date >= DateTime.Now.Subtract(TimeSpan.FromDays(60))
                                    orderby c.Date descending
                                    join d in dailyAmexTable.AsEnumerable() on c.Date equals d.Date
                                    select new StockMarketCompare { stockClose = d.AdjustedClose, marketClose = c.AdjustedClose };

                    List<StockMarketCompare> amexResult = amexQuery.ToList();
// perform calculations here and save to stockPctReturns and marketPctReturns
         }
  }

還是我應該這樣:

using (DailyGlobalDataTableAdapter dailyGlobalAdapter = new DailyGlobalDataTableAdapter())
        using (DailyAmexDataTableAdapter dailyAmexAdapter = new DailyAmexDataTableAdapter())
        {
            Calculations calc = new Calculations();
            calc.Symbol = "GOOG";
            calc.Market = "nyse";
            dailyAmexAdapter.Fill(calc.dailyAmexTable);
            dailyGlobalAdapter.Fill(calc.dailyGlobalTable);
            calc.fillPctReturns();
        }

//此示例將在數據表的計算類中具有公共屬性,如您所見,我像上面那樣設置它們

希望你能說出我要做什么。

看起來您顯示的第二種方法現在確實允許您在使用塊中包裝dailyAmexTabledailyGlobalTable ,因此,如果它們各自的類實現IDisposable (我假設它們基於您的其他代碼來實現),那么我更喜歡第一種方法。 這使您可以將所有IDisposable實例包裝在using塊中,您應該這樣做。

使用塊的主要目的是,您通常希望在不再需要它們時立即將其關閉以釋放資源。 這樣,您應該嘗試在using塊之外進行計算,除非執行計算后需要將某些內容保存回數據庫。 所以看起來像這樣:

public void fillPctReturns()
{
    stockPctReturns = new List<decimal>();
    marketPctReturns = new List<decimal>();

    // Declare amexResult here so it is accessible outside the using block
    List<StockMarketCompare> amexResult;

    using (DailyGlobalDataTableAdapter dailyGlobalAdapter = new DailyGlobalDataTableAdapter())
    using (DailyAmexDataTableAdapter dailyAmexAdapter = new DailyAmexDataTableAdapter())
    using (DailyGlobalDataTable dailyGlobalTable = new DailyGlobalDataTable())
    using (DailyAmexDataDataTable dailyAmexTable = new DailyAmexDataDataTable())
    {
        dailyAmexAdapter.Fill(dailyAmexTable);
        dailyGlobalAdapter.Fill(dailyGlobalTable);
        var amexQuery = from c in dailyGlobalTable.AsEnumerable()
                        where c.Date >= DateTime.Now.Subtract(TimeSpan.FromDays(60))
                        orderby c.Date descending
                        join d in dailyAmexTable.AsEnumerable() on c.Date equals d.Date
                        select new StockMarketCompare { stockClose = d.AdjustedClose, marketClose = c.AdjustedClose };

        amexResult = amexQuery.ToList();

     }
     // perform calculations here and save to stockPctReturns and marketPctReturns
}

編輯:

現在,我對您的工作有了更清晰的了解,我建議在類外使用using塊。 主要原因是要確保您的代碼是松散耦合的 ,在這種情況下,這意味着數據訪問代碼與業務邏輯 (又名計算)代碼是分開的。 一種方法是在計算類之外運行查詢,然后將查詢結果傳遞給計算類中的屬性。 例如:

public class Calculations
{
    public List<StockMarketCompare> Data { get; set; }
    // (Other properties and methods omitted.)
}

然后在您的調用函數中:

var calc = new Calculations();
using (DailyGlobalDataTableAdapter dailyGlobalAdapter = new DailyGlobalDataTableAdapter())
using (DailyAmexDataTableAdapter dailyAmexAdapter = new DailyAmexDataTableAdapter())
using (DailyGlobalDataTable dailyGlobalTable = new DailyGlobalDataTable())
using (DailyAmexDataDataTable dailyAmexTable = new DailyAmexDataDataTable())
{
    dailyAmexAdapter.Fill(dailyAmexTable);
    dailyGlobalAdapter.Fill(dailyGlobalTable);
    var amexQuery = from c in dailyGlobalTable.AsEnumerable()
                    where c.Date >= DateTime.Now.Subtract(TimeSpan.FromDays(60))
                    orderby c.Date descending
                    join d in dailyAmexTable.AsEnumerable() on c.Date equals d.Date
                    select new StockMarketCompare { stockClose = d.AdjustedClose, marketClose = c.AdjustedClose };

    calc.Data = amexQuery.ToList();
    calc.fillPctReturns();
    // Run any other calculations here.
    // Save data to DB here.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM