簡體   English   中英

需要幫助來理解下面的.NET代碼

[英]Need help in understanding the below .NET code

這基本上是一個類庫項目,它以某種方式暴露為WCF服務。 下面的代碼是數據訪問層的一部分。 'db'是DataContext類的對象。 要保存文件,我們會執行以下操作 -

public static Guid SaveFile(FileDetails fileDetails)
{
            System.Nullable<Guid> id = null;

            SystemDataContext.UsingWrite(db =>
                {
                    db.SaveFileData(fileDetails.RunId, fileDetails.FileData, fileDetails.FileExtension, ref id);
                });
            return id ?? Guid.Empty;
 }

然后,以下將執行 -

 public static void UsingWrite(Action<SoftCashCreditDBDataContext> action)
 {
            using (var context = new SystemDataContext())
            {
                try
                {
                    action(context.Write);
                }
                catch (Exception ex)
                {
                    DataAccessExceptionHandler.HandleExcetion(ex, Config.DataLayerPolicy);
                }
            }
  }


 public SystemDataContext()
        {
            if (_stack == null)
            {
                _stack = new Stack<SystemDataContext>();
                this.Depth = 1;
                this.Read = new SoftCashCreditDBDataContext(Config.ReadDatabaseConnection);
                this.Write = new SoftCashCreditDBDataContext(Config.WriteDatabaseConnection);
            }
            else
            {
                var parent = _stack.Peek();
                /// Increment level of node.
                this.Depth = parent.Depth + 1;
                /// Copy data context from the parent
                this.Read = parent.Read;
                this.Write = parent.Write;
            }
            _stack.Push(this);
        }

        public int Depth { get; private set; }

        public bool IsRoot { get { return this.Depth == 1; } }

        [ThreadStatic]
        private static Stack<SystemDataContext> _stack = null;

        public SoftCashCreditDBDataContext Read { get; private set; }
        public SoftCashCreditDBDataContext Write { get; private set; }

        #region IDisposable Members

        public void Dispose()
        {
            var context = _stack.Pop();
            if (context.IsRoot == true)
            {
                context.Read.Dispose();
                context.Write.Dispose();
                _stack = null;
            }
        }

        #endregion

    }

他們在這里實現了LINQ to SQL,並創建了一個DBContext類。 'SaveFileData()'方法實際上是該類的一部分,它只是調用SP內部來保存文件。 我沒有遵循的內容 - 對UsingWrite()的調用到底是做什么的? 什么傳遞給'Action action'參數,它在做什么?

我理解你的困惑。 他們使用2名代表。

這將傳遞給action參數:

db =>
{
    db.SaveFileData(fileDetails.RunId, fileDetails.FileData, fileDetails.FileExtension, ref id);
}

因此,當調用UsingWrite時,在Write委托中設置的SoftCashCreditDBDataContext委托將調用SaveFileData。

一個幫助您理解Action的簡化示例:

public void Main()
{
   Test(x => Debug.Write(x));
}

private void Test(Action<string> testAction)
{
   testAction("Bla");
}

此函數將使用參數x調用Debug.Write,該參數是傳遞給測試操作函數的字符串。

暫無
暫無

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

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