/// <summary> /// Get data stored in current view for given key and type. /// If no matching data are found, new data is created via provider. /// </summary> /// <typeparam name="T">Type of searched data</typeparam> /// <param name="key">Key of data - because multiple sources can store data with same type</param> /// <param name="provider">Provider used for data creation</param> /// <returns>Stored data, created data, or null if the data is not found and provider is not present</returns> internal T Data <T>(object key, ViewDataProvider <T> provider) where T : ExecutionViewData { var type = typeof(T); var canCreate = provider != null; Dictionary <Type, ExecutionViewData> keyData; if (!_data.TryGetValue(key, out keyData)) { if (!canCreate) { return(null); } _data[key] = keyData = new Dictionary <Type, ExecutionViewData>(); } ExecutionViewData viewData; if (!keyData.TryGetValue(type, out viewData)) { if (!canCreate) { return(null); } keyData[type] = viewData = provider(); } return(viewData as T); }
public void should_ignore_incorrect_codeRRContext() { var context = new ErrorReporterContext(this, new Exception()); var sut = new ViewDataProvider(); var result = sut.Collect(context); result.Should().BeNull(); }
public void should_return_null_when_the_collection_is_empty() { var httpContext = Substitute.For <HttpContextBase>(); var context = new AspNetMvcContext(this, new Exception(), httpContext); context.ViewData = new ViewDataDictionary(); var sut = new ViewDataProvider(); var result = sut.Collect(context); result.Should().BeNull(); }
public void should_include_included_items() { var httpContext = Substitute.For <HttpContextBase>(); var context = new AspNetMvcContext(this, new Exception(), httpContext); context.ViewData = new ViewDataDictionary { { "Id", 3 }, { "Title", "Hello world" } }; var sut = new ViewDataProvider(); var result = sut.Collect(context); result.Property("Id").Should().Be("3"); result.Property("Title").Should().Be("Hello world"); }
/// <summary> /// Get data stored in current view for given key and type. /// If no matching data are found, new data is created via provider. /// </summary> /// <typeparam name="T">Type of searched data.</typeparam> /// <param name="key">Key of data - because multiple sources can store data with same type.</param> /// <param name="provider">Provider used for data creation.</param> /// <returns>Stored data, created data, or null if the data is not found and provider is not present.</returns> public T Data <T>(object key, ViewDataProvider <T> provider = null) where T : ExecutionViewData { return(_viewData.Data <T>(key, provider)); }