the_law
the_law

Reputation: 215

What's going on in DynamicViewDataDictionary.cs?

I wonder if anyone can explain this to me:

internal sealed class DynamicViewDataDictionary : DynamicObject {
    private readonly Func<ViewDataDictionary> _viewDataThunk; //// eg () => ViewData

public DynamicViewDataDictionary(Func<ViewDataDictionary> viewDataThunk)
{ 
    _viewDataThunk = viewDataThunk;
}

private ViewDataDictionary ViewData {
    get {
        ViewDataDictionary viewData = _viewDataThunk();
        Debug.Assert(viewData != null);
        return viewData;
    }
}
    .................

This is instantiated in WebViewPage for the ViewBag property, with () => ViewData in the constructor. So it appears that here the ViewData property would be returning itself with this func()? What's going on here and why is it done this way?

Upvotes: 1

Views: 334

Answers (1)

Pablo Montilla
Pablo Montilla

Reputation: 3050

I can only speculate, but maybe it is done that way to aid during testing. Having a function, you can change the return value during execution.

Upvotes: 1

Related Questions