private object LoadActiveRecord(Type type, object pk, ARFetchAttribute attr, ActiveRecordModel model) { object obj = null; if (pk != null && !String.Empty.Equals(pk)) { PrimaryKeyModel pkModel = (PrimaryKeyModel)model.Ids[0]; Type pkType = pkModel.Property.PropertyType; if (pk.GetType() != pkType) { pk = Convert.ChangeType(pk, pkType); } obj = SupportingUtils.FindByPK(type, pk, attr.Required); } if (obj == null && attr.Create) { obj = Activator.CreateInstance(type); } return(obj); }
protected override bool BindComplexParameter(ParameterInfo param, IRequest request, object[] args, int argIndex) { object[] arBinderAttributes = param.GetCustomAttributes(typeof(ARDataBindAttribute), false); if (arBinderAttributes.Length != 0) { ARDataBindAttribute attr = (ARDataBindAttribute)arBinderAttributes[0]; args[argIndex] = CustomBindObject(attr.From, param.ParameterType, attr.Prefix, attr.NestedLevel, attr.Exclude, attr.Allow, attr.AutoLoad); return(true); } object[] fetchAttributes = param.GetCustomAttributes(typeof(ARFetchAttribute), false); if (fetchAttributes.Length != 0) { ARFetchAttribute attr = (ARFetchAttribute)fetchAttributes[0]; args[argIndex] = FetchActiveRecord(param, attr, request); return(true); } return(base.BindComplexParameter(param, request, args, argIndex)); }
private object LoadActiveRecord(Type type, object pk, ARFetchAttribute attr, ActiveRecordModel model) { object instance = null; if (pk != null && !String.Empty.Equals(pk)) { var pkModel = ObtainPrimaryKey(model); var pkType = pkModel.Property.PropertyType; bool conversionSucceeded; var convertedPk = converter.Convert(pkType, pk.GetType(), pk, out conversionSucceeded); if (!conversionSucceeded) { throw new MonoRailException("ARFetcher could not convert PK {0} to type {1}", pk, pkType); } if (string.IsNullOrEmpty(attr.Eager)) { // simple load instance = ActiveRecordMediator.FindByPrimaryKey(type, convertedPk, attr.Required); } else { // load using eager fetching of lazy collections var criteria = DetachedCriteria.For(type); criteria.Add(Expression.Eq(pkModel.Property.Name, convertedPk)); foreach (var associationToEagerFetch in attr.Eager.Split(',')) { var clean = associationToEagerFetch.Trim(); if (clean.Length == 0) { continue; } criteria.SetFetchMode(clean, FetchMode.Eager); } var result = (object[])ActiveRecordMediator.FindAll(type, criteria); if (result.Length > 0) { instance = result[0]; } } } if (instance == null && attr.Create) { instance = Activator.CreateInstance(type); } return(instance); }
private object LoadActiveRecord(Type type, object pk, ARFetchAttribute attr, ActiveRecordModel model) { object instance = null; if (pk != null && !String.Empty.Equals(pk)) { PrimaryKeyModel pkModel = ObtainPrimaryKey(model); Type pkType = pkModel.Property.PropertyType; bool conversionSucceeded; object convertedPk = converter.Convert(pkType, pk.GetType(), pk, out conversionSucceeded); if (!conversionSucceeded) { throw new RailsException("ARFetcher could not convert PK {0} to type {1}", pk, pkType); } if (attr.Eager == null || attr.Eager.Length == 0) { // simple load instance = ActiveRecordMediator.FindByPrimaryKey(type, convertedPk, attr.Required); } else { // load using eager fetching of lazy collections DetachedCriteria criteria = DetachedCriteria.For(type); criteria.Add(Expression.Eq(pkModel.Property.Name, convertedPk)); foreach (string associationToEagerFetch in attr.Eager.Split(',')) { string clean = associationToEagerFetch.Trim(); if (clean.Length == 0) { continue; } criteria.SetFetchMode(clean, FetchMode.Eager); } object[] result = (object[]) ActiveRecordMediator.FindAll(type, criteria); if (result.Length > 0) instance = result[0]; } } if (instance == null && attr.Create) { instance = Activator.CreateInstance(type); } return instance; }
protected override String GetRequestParameterName(ParameterInfo param) { object[] fetchAttributes = param.GetCustomAttributes(typeof(ARFetchAttribute), false); if (fetchAttributes.Length == 0) { return(base.GetRequestParameterName(param)); } ARFetchAttribute attr = (ARFetchAttribute)fetchAttributes[0]; return(attr.RequestParameterName != null ? attr.RequestParameterName : base.GetRequestParameterName(param)); }
public object FetchActiveRecord(ParameterInfo param, ARFetchAttribute attr, IRequest request, IDictionary <string, object> customActionParameters) { var type = param.ParameterType; var isArray = type.IsArray; if (isArray) { type = type.GetElementType(); } var model = ActiveRecordModel.GetModel(type); if (model == null) { throw new MonoRailException(String.Format("'{0}' is not an ActiveRecord " + "class. It could not be bound to an [ARFetch] attribute.", type.Name)); } if (model.CompositeKey != null) { throw new MonoRailException("ARFetch only supports single-attribute primary keys"); } var webParamName = attr.RequestParameterName ?? param.Name; if (!isArray) { var value = GetParameterValue(webParamName, customActionParameters, request); return(LoadActiveRecord(type, value, attr, model)); } var pks = GetParameterValues(webParamName, customActionParameters, request); 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); }
public object FetchActiveRecord(ParameterInfo param, ARFetchAttribute attr, IRequest request) { Type type = param.ParameterType; bool isArray = type.IsArray; if (isArray) type = type.GetElementType(); ActiveRecordModel model = ActiveRecordModel.GetModel(type); if (model == null) { throw new RailsException(String.Format("'{0}' is not an ActiveRecord " + "class. It could not be bound to an [ARFetch] attribute.", type.Name)); } if (model.CompositeKey != null) { throw new RailsException("ARFetch only supports single-attribute primary keys"); } String webParamName = attr.RequestParameterName != null ? attr.RequestParameterName : param.Name; if (!isArray) { return LoadActiveRecord(type, request.Params[webParamName], attr, model); } object[] pks = request.Params.GetValues(webParamName); if (pks == null) { pks = new object[0]; } Array objs = Array.CreateInstance(type, pks.Length); for(int i = 0; i < objs.Length; i++) { objs.SetValue(LoadActiveRecord(type, pks[i], attr, model), i); } return objs; }
public object FetchActiveRecord(ParameterInfo param, ARFetchAttribute attr, IRequest request, IDictionary<string, object> customActionParameters) { var type = param.ParameterType; var isArray = type.IsArray; if (isArray) type = type.GetElementType(); var model = ActiveRecordModel.GetModel(type); if (model == null) { throw new MonoRailException(String.Format("'{0}' is not an ActiveRecord " + "class. It could not be bound to an [ARFetch] attribute.", type.Name)); } if (model.CompositeKey != null) { throw new MonoRailException("ARFetch only supports single-attribute primary keys"); } var webParamName = attr.RequestParameterName ?? param.Name; if (!isArray) { var value = GetParameterValue(webParamName, customActionParameters, request); return LoadActiveRecord(type, value, attr, model); } var pks = GetParameterValues(webParamName, customActionParameters, request); 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; }
private object FetchActiveRecord(ParameterInfo param, ARFetchAttribute attr, IRequest request) { Type type = param.ParameterType; bool isArray = type.IsArray; if (isArray) { type = type.GetElementType(); } ActiveRecordModel model = ActiveRecordModel.GetModel(type); if (model == null) { throw new RailsException(String.Format("'{0}' is not an ActiveRecord " + "class. It could not be bound to an [ARFetch] attribute.", type.Name)); } if (model.Ids.Count != 1) { throw new RailsException("ARFetch only supports single-attribute primary keys"); } String webParamName = attr.RequestParameterName != null ? attr.RequestParameterName : param.Name; if (!isArray) { return(LoadActiveRecord(type, request.Params[webParamName], attr, model)); } object[] pks = request.Params.GetValues(webParamName); Array objs = Array.CreateInstance(type, pks.Length); for (int i = 0; i < objs.Length; i++) { objs.SetValue(LoadActiveRecord(type, pks[i], attr, model), i); } return(objs); }