protected virtual ActionParameterModel GetFetchParameter(ActionParameterModel originalParam, ARFetchAttribute fetchAttribute) { Type arType = originalParam.ParamInfo.ParameterType; var arModel = ActiveRecordModel.GetModel(arType); if (arModel == null) { return(originalParam); } if (arModel.PrimaryKey == null) { return(originalParam); } string paramName = fetchAttribute.RequestParameterName; object paramValue = null; if (originalParam.Value != null) { paramValue = arModel.PrimaryKey.Property.GetValue(originalParam.Value, null); } if (paramValue == null) { return(originalParam); } return(new ActionParameterModel(originalParam.ParamInfo, paramName, paramValue)); }
protected virtual ActionParameterModel GetFetchParameter(ActionParameterModel originalParam, ARFetchAttribute fetchAttribute) { Type arType = originalParam.ParamInfo.ParameterType; var arModel = ActiveRecordModel.GetModel(arType); if (arModel == null) { return originalParam; } if (arModel.PrimaryKey == null) { return originalParam; } string paramName = fetchAttribute.RequestParameterName; object paramValue = null; if (originalParam.Value != null) { paramValue = arModel.PrimaryKey.Property.GetValue(originalParam.Value, null); } if (paramValue == null) { return originalParam; } return new ActionParameterModel(originalParam.ParamInfo, paramName, paramValue); }
private object LoadActiveRecord(Type type, object pk, ARFetchAttribute attr, Castle.ActiveRecord.Model model) { object instance = null; if (pk != null && !String.Empty.Equals(pk)) { var pkModel = ObtainPrimaryKey(model); var pkType = pkModel.Value.ReturnedClass; bool conversionSucceeded; var convertedPk = _converter.Convert(pkType, pk.GetType(), pk, out conversionSucceeded); if (!conversionSucceeded) { throw new ActiveRecordException(string.Format("ARFetcher could not convert PK {0} to type {1}", pk, pkType)); } if (string.IsNullOrEmpty(attr.Eager)) { // simple load instance = attr.Required ? AR.Find(type, convertedPk) : AR.Peek(type, convertedPk); } else { // load using eager fetching of lazy collections var criteria = DetachedCriteria.For(type); criteria.Add(Expression.Eq(pkModel.Key, convertedPk)); foreach (var associationToEagerFetch in attr.Eager.Split(',')) { var clean = associationToEagerFetch.Trim(); if (clean.Length == 0) { continue; } criteria.SetFetchMode(clean, FetchMode.Eager); } var result = AR.Execute(type, s => criteria.GetExecutableCriteria(s).List()); if (result.Count > 0) { instance = result[0]; } } } if (instance == null && attr.Create) { instance = Activator.CreateInstance(type); } return(instance); }
protected override IEnumerable <ActionParameterModel> Decorate(ActionParameterModel paramToDecorate) { if (paramToDecorate.ParamInfo.IsDefined(arFetchAttrType, false)) { ARFetchAttribute attr = (ARFetchAttribute)paramToDecorate.ParamInfo.GetCustomAttributes(arFetchAttrType, false)[0]; yield return(GetFetchParameter(paramToDecorate, attr)); } else { yield return(paramToDecorate); } }
public object FetchActiveRecord(ParameterInfo param, ARFetchAttribute attr, NameValueCollection dict, IDictionary <string, object> customActionParameters) { var type = param.ParameterType; var isArray = type.IsArray; if (isArray) { type = type.GetElementType(); } var model = AR.Holder.GetModel(type); if (model == null) { throw new ActiveRecordException(String.Format("'{0}' is not an ActiveRecord " + "class. It could not be bound.", type.Name)); } var webParamName = attr.RequestParameterName ?? param.Name; if (!isArray) { var value = GetParameterValue(webParamName, customActionParameters, dict); return(LoadActiveRecord(type, value, attr, model)); } var pks = GetParameterValues(webParamName, customActionParameters, dict); var objs = Array.CreateInstance(type, pks.Length); for (var i = 0; i < objs.Length; i++) { objs.SetValue(LoadActiveRecord(type, pks[i], attr, model), i); } return(objs); }