我是否在课堂内外使用语句编写这些内容?

时间:2014-12-28 05:30:36

标签: c# sql strongly-typed-dataset

我有一个我编写的类,它包含对从数据库中提取的数据执行计算的不同方法。每个方法从数据库中提取信息并对其执行计算,然后将其保存到我初始化类时可以访问的类中的变量。我下面有两个伪代码方法,我试图找出以下内容:

  1. 如果每个方法都提取数据然后对其进行计算,我应该使用using语句?
  2. 我使用最有效的方式存储数据吗?
  3. 这是我在初始化课程时所做的事情

        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();
            }
    

    //这个例子在数据表的计算类中有公共属性,你可以看到我像上面那样设置它们

    希望你能说出我想要做的事情。

1 个答案:

答案 0 :(得分:1)

看起来您展示的第二种方法现在允许您使用块包裹dailyAmexTabledailyGlobalTable,因此如果它们各自的类实现IDisposable(我假设它们基于在你的其他代码),然后我更喜欢第一种方法。这允许您将所有IDisposable实例包含在使用块中,这应该是您应该做的。

使用块的主要原因是,您通常希望在不再需要它们时立即关闭它们以释放资源。因此,除非您需要在执行计算后将某些内容保存回数据库,否则您应该尝试在外部使用块进行计算。所以它看起来像这样:

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
}

编辑:

既然我已经更清楚地看到你在做什么,我建议在课堂外使用使用块。主要原因是确保您的代码松散耦合,在这种情况下意味着数据访问代码与业务逻辑分开(又名计算)代码。一种方法是在计算类之外运行查询,并将查询结果传递给计算类中的属性。例如:

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.
}