1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
namespace System.Web.ModelBinding {
using System;
using System.Collections.Generic;
using System.Web;
/// <summary>
/// This class provides all the external things that the Model Binding System requires.
/// These include HttpContext and ModelState.
/// </summary>
public class ModelBindingExecutionContext {
private Dictionary<Type, object> _services = new Dictionary<Type,object>();
private HttpContextBase _httpContext;
private ModelStateDictionary _modelState;
public ModelBindingExecutionContext(HttpContextBase httpContext, ModelStateDictionary modelState) {
if (httpContext == null) {
throw new ArgumentNullException("httpContext");
}
if (modelState == null) {
throw new ArgumentNullException("modelState");
}
_httpContext = httpContext;
_modelState = modelState;
}
public virtual HttpContextBase HttpContext {
get {
return _httpContext;
}
}
public virtual ModelStateDictionary ModelState {
get {
return _modelState;
}
}
public virtual void PublishService<TService>(TService service) {
_services[typeof(TService)] = service;
}
public virtual TService GetService<TService>() {
return (TService)_services[typeof(TService)];
}
public virtual TService TryGetService<TService>() {
if (_services.ContainsKey(typeof(TService))) {
return (TService)_services[typeof(TService)];
}
return default(TService);
}
}
}
|