public object ExecuteUpdate(IEngineContext context, IController controller, IControllerContext controllerContext) { controllerContext.Action = "update"; using (var transaction = new TransactionScope()) { var instance = AR.Find <T>(context.GetParameter(IdParameter)); if (instance == null) { context.Handle404(); return(null); } var pk = AR.Holder.GetModel(Type).Metadata.GetIdentifier(instance, EntityMode.Poco).ToString(); try { var excludeattr = controller.GetType().GetAttr <UpdateExcludeAttribute>(); var includeattr = controller.GetType().GetAttr <UpdateIncludeAttribute>(); var includeprops = includeattr == null ? null : includeattr.Properties; var excludeprops = excludeattr == null ? null : excludeattr.Properties; var form = BuildCompositeNode(context.Request.Form, Queryfilters.Where(qf => qf.ApplyOnSave), true); OnPreBind(controller, instance); Binder.AutoLoad = AutoLoadBehavior.NullIfInvalidKey; Binder.BindObjectInstance(instance, TemplateObjectName, excludeprops, includeprops, form); OnPostBind(controller, instance); OnPreSave(controller, instance); OnPreUpdate(controller, instance); if (Validator.IsValid(instance)) { AR.Save <T>(instance); OnPostUpdate(controller, instance); OnPostSave(controller, instance); transaction.Flush(); transaction.VoteCommit(); pk = AR.Holder.GetClassMetadata(Type).GetIdentifier(instance, EntityMode.Poco).ToString(); controllerContext.PropertyBag[TemplateObjectName] = instance; context.SuccessMessage(instance + " has been updated"); } else { transaction.VoteRollBack(); context.ErrorMessages(Validator.GetErrorSummary(instance).ErrorMessages); } } catch (Exception ex) { transaction.VoteRollBack(); context.ErrorMessages(ex); } finally { context.Flash[TemplateObjectName] = instance; controllerContext.PropertyBag["objectid"] = pk; context.CurrentControllerContext.PropertyBag[TemplateObjectName] = instance; } } return(null); }
public override object Execute(IEngineContext context, IController controller, IControllerContext controllerContext) { base.Execute(context, controller, controllerContext); OnPreList(controller); OnPreView(controller); try { var match = context.Items[RouteMatch.RouteMatchKey] as RouteMatch; if (match == null) { throw new MonoRailException("Cannot find 'id' from the url."); } var instance = AR.Find <T>(match.Parameters["id"]); if (instance == null) { context.Handle404(); return(null); } controllerContext.PropertyBag[TemplateObjectName] = instance; var id = AR.Holder.GetModel(Type).Metadata.GetIdentifier(instance, EntityMode.Poco); controllerContext.PropertyBag["objectid"] = id; var loader = DetachedCriteria.For <T>() .Add(Restrictions.Eq(Projections.Id(), id)); foreach (var b in AR.Holder.GetModel(typeof(T)).BelongsTos) { loader.CreateCriteria(b.Key, JoinType.LeftOuterJoin); } loader.SetResultTransformer(Transformers.DistinctRootEntity).Future <T>(); if (controller.GetType().GetAttr <DontDoManyFetch>() == null) { foreach (var hasmany in AR.Holder.GetModel(typeof(T)).HasManys) { DetachedCriteria.For <T>() .CreateCriteria(hasmany.Value.Type.Role.Replace(typeof(T) + ".", ""), JoinType.LeftOuterJoin) .Future <T>(); } foreach (var hasmany in AR.Holder.GetModel(typeof(T)).HasAndBelongsToManys) { DetachedCriteria.For <T>() .CreateCriteria(hasmany.Value.Type.Role.Replace(typeof(T) + ".", ""), JoinType.LeftOuterJoin) .Future <T>(); } } OnPostView(controller, instance); return(null); } catch (Exception ex) { context.ErrorMessages(ex); } context.Handle404(); return(null); }
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); }
public object ExecuteView(IEngineContext context, IController controller, IControllerContext controllerContext) { OnPreView(controller); try { var instance = AR.Find <T>(context.GetParameter(IdParameter)); if (instance == null) { context.Handle404(); return(null); } var id = AR.Holder.GetModel(Type).Metadata.GetIdentifier(instance, EntityMode.Poco); var loader = DetachedCriteria.For <T>() .Add(Restrictions.Eq(Projections.Id(), id)); foreach (var b in AR.Holder.GetModel(typeof(T)).BelongsTos) { loader.CreateCriteria(b.Key, JoinType.LeftOuterJoin); } loader.SetResultTransformer(Transformers.DistinctRootEntity); if (!DontDoManyFetch) { foreach (var hasmany in AR.Holder.GetModel(typeof(T)).HasManys) { DetachedCriteria.For <T>() .Add(Restrictions.Eq(Projections.Id(), id)) .CreateCriteria(hasmany.Value.Type.Role.Replace(typeof(T).FullName + ".", ""), JoinType.LeftOuterJoin) .Future <T>(); } foreach (var hasmany in AR.Holder.GetModel(typeof(T)).HasAndBelongsToManys) { DetachedCriteria.For <T>() .Add(Restrictions.Eq(Projections.Id(), id)) .CreateCriteria(hasmany.Value.Type.Role.Replace(typeof(T).FullName + ".", ""), JoinType.LeftOuterJoin) .Future <T>(); } } controllerContext.PropertyBag[TemplateObjectName] = instance; OnPostView(controller, instance); } catch (Exception e) { context.ErrorMessages(e); } return(null); }
public void ASimpleObjectCanBeRead() { var ship = new Ship() { Name = "Andrea Doria" }; using (new SessionScope()) AR.Create(ship); using (new StatelessSessionScope()) { Assert.IsTrue(AR.Find <Ship>(ship.Id) != null); Assert.AreEqual("Andrea Doria", AR.Find <Ship>(ship.Id).Name); } }
public object ExecuteDelete(IEngineContext context, IController controller, IControllerContext controllerContext) { using (var transaction = new TransactionScope()) { var instance = AR.Find <T>(context.GetParameter("id")); try { OnPreDelete(controller, instance); AR.Delete <T>(instance); OnPostDelete(controller, instance); transaction.Flush(); transaction.VoteCommit(); context.SuccessMessage(instance + " has been deleted"); } catch (Exception ex) { transaction.VoteRollBack(); context.ErrorMessages(ex); } finally { controllerContext.PropertyBag[TemplateObjectName] = instance; context.Flash[TemplateObjectName] = instance; } } return(null); }
protected virtual object FindByPrimaryKey(Type targetType, object id) { return(AR.Find(targetType, id)); }