Error handling when use views with Dynamic model

瀏覽次數:447 次
跳到第一則未讀訊息

mogadanez

未讀,
2012年6月22日 中午12:21:412012/6/22
收件者:spar...@googlegroups.com
public class DynamiclView : AbstractSparkView
{
  public dynamic model     {     get{...} set{...}    }
}

Most work fine except:

1. when  some part of dynamic call chain returns null i got ambiguous error

 Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The call is ambiguous between the following methods or properties: 'System.IO.TextWriter.Write(string, params object[])' and 'System.IO.TextWriter.Write(char[])'

2. now i should  write in my views: ${model.some.bla.bla},  i wanr remove  necessary to write "model." each time and write just  ${some.bla.bla} . How can i do this?  will it work if i implement IDynamicMetaObjectProvider for my  DynamiclView  ? 

3. Does it possible to catch and fallback write source expression not on NullReferenceException, but on all exceptions or provided list of exceptions?
 



Rob G

未讀,
2012年7月7日 凌晨3:23:302012/7/7
收件者:spar...@googlegroups.com
Hi Alexander,

1. I'm not sure what's happening there, without a code example to debug, I won't be able to help.

2. Without more context information, I can't imagine that would work since ${Model.blah} (! note the CAPS in 'Model') is a special property created in the code of the view. By calling ${some.blah}, you're effectively calling ${this.some.blah} on the view itself, so unless you manage to create a partial class at compile time (which we do on some projects) or are able to inject the properties you want somehow, then I'm not sure it would work. Perhaps if you could explain more detail I could help.

3. Currently the fallback only checks for NRE, all other exceptions just spit out the stack trace.

 



--
You received this message because you are subscribed to the Google Groups "Spark View Engine Dev" group.
To view this discussion on the web visit https://groups.google.com/d/msg/spark-dev/-/0quehkHuAmQJ.
To post to this group, send email to spar...@googlegroups.com.
To unsubscribe from this group, send email to spark-dev+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spark-dev?hl=en.

RobertTheGrey

未讀,
2012年11月13日 清晨7:17:372012/11/13
收件者:spar...@googlegroups.com
Hi there,

Is there any chance you could provide a more detailed stack trace from the output?

I'm assuming that you have use cases where properties on your dynamic view model are quite likely to be null or non-existent by design? If that's the case, then we may have to find a way to make Spark more robust against these kinds of dynamic queries.

I have used dynamic view models in my projects, but usually don't tend to attach any properties to them that end up being null or non-existent because that usually leads to very unexpected edge cases, and I prefer to fail early rather that let something like that catch us out in production. Could you describe any of these use cases perhaps that require Spark to be clever enough not to process these or somehow mitigate errors thrown by the C# runtime? 

Cheers,
Rob

On Wednesday, October 31, 2012 7:03:55 AM UTC, SamousPrime wrote:
Hi, RobertTheGray.

I have the same problem as Alexander has. Code example:

public class TestModel2
    {
        public int Id { get; set; }
        public string Name { get; set; }       
    }
 
public class HomeController : Controller
    {
         public ActionResult Index()
         {
             var model = new TestModel2();
             return View("~/Modules/Home/Index", model);
         }
    }

and spark view is:

<use namespace='MvcApplication1.Helpers'/>
<use namespace='System'/>
<viewdata model="dynamic"/>
<div>
  ${Model.Name}
</div>
 
When it renders I get the error

The call is ambiguous between the following methods or properties: 'System.IO.TextWriter.Write(string, params object[])' and 'System.IO.TextWriter.Write(char[])'

this happens because the "Name" property is null and the Model's type is "dynamic".

суббота, 7 июля 2012 г., 11:23:30 UTC+4 пользователь RobertTheGrey написал:

SamousPrime

未讀,
2012年11月16日 清晨7:27:062012/11/16
收件者:spar...@googlegroups.com
Hi, thx for support.

This is a full stack trace:

[RuntimeBinderException: The call is ambiguous between the following methods or properties: 'System.IO.TextWriter.Write(string, params object[])' and 'System.IO.TextWriter.Write(char[])']
   CallSite.Target(Closure , CallSite , TextWriter , Object ) +195
   System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2(CallSite site, T0 arg0, T1 arg1) +456
   MvcApplication6.Controllers.Viewdbb04328d6f14a2690bb8edcce0877b7.RenderViewLevel0() +504
   MvcApplication6.Controllers.Viewdbb04328d6f14a2690bb8edcce0877b7.Render() +5
   Spark.SparkViewBase.RenderView(TextWriter writer) +105
   Spark.Web.Mvc.SparkView.Render(ViewContext viewContext, TextWriter writer) +344
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +23
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +92
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +126
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +45
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +25
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +61
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +25
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +49
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +28
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +25
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +49
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8969201
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

and code:
************************************************************
Controller.cs:
public ActionResult Index()
         {
             var model = new TestModel2();
             return View("~/Modules/Home/Index", model);
         }
************************************************************
TestModel2.cs:
public class TestModel2
    {
        public int Id { get; set; }
        public string Name { get; set; }       
    }
************************************************************
Index.spark:
<viewdata model="dynamic"/>
<div>
  ${Model.Name}
</div>
************************************************************

I'm looking forward quick response from you.

вторник, 13 ноября 2012 г., 16:17:38 UTC+4 пользователь RobertTheGrey написал:

Rob G

未讀,
2012年11月16日 清晨7:33:112012/11/16
收件者:spar...@googlegroups.com
Hi Alexander ,

Before I look too deeply at this, it does look quite easy to reproduce from your code and the obvious answer here would be that a dynamic which contains a property that may or may not exist or be null seems to confuse the C# compiler as to whether or not to interpret the write operation as a string or char. This makes sense - but what doesn't make sense is why you're sending null values down to the view without handling them properly?

I asked some questions in my previous response - are you able to answer them so I can get a better idea of what you're trying to achieve and why Spark needs to change to accommodate null values on dynamics?

Thanks,
Rob


To view this discussion on the web visit https://groups.google.com/d/msg/spark-dev/-/1IrYko7D0AsJ.

SamousPrime

未讀,
2012年11月16日 上午8:24:542012/11/16
收件者:spar...@googlegroups.com
Hi Robert,

I'll try to explain what I do. I have a json-storage from which I create a different models (this part of code is missed in my example). Some properties may contain values, some - not (it means, that they are null), all I should know about all properties are their names only, In my C# code I write something like this:
Object obj = CreateFromJson(...);
I just use the "Object"-type instead of "dynamic", because I don't to add any properties in runtime, but for spark view I define model as dynamic because it allows me to write something like ${Model.ObjOne.Prop1} without defining specific model type (<viewdata model="SomeSpecificType"/>). Of course, before I write ${Model.ObjOne.Prop1} I always check that the Model.ObjOne is not null, but I don't want to check value of Prop1, because in this case I have to check every property, but sometimes it's very complicated.

пятница, 16 ноября 2012 г., 16:33:12 UTC+4 пользователь RobertTheGrey написал:

Rob G

未讀,
2012年11月16日 上午8:28:262012/11/16
收件者:spar...@googlegroups.com
Do you get the same exception if you use <viewdata model="Object"/>


To view this discussion on the web visit https://groups.google.com/d/msg/spark-dev/-/exqWQy9AdwQJ.

SamousPrime

未讀,
2012年11月16日 上午8:38:372012/11/16
收件者:spar...@googlegroups.com
It is impossible for me to use <viewdata model="Object"/>, because the Object doesn't contains my properties, I can not even execute them.

пятница, 16 ноября 2012 г., 17:28:28 UTC+4 пользователь RobertTheGrey написал:

Rob G

未讀,
2012年11月16日 上午10:12:272012/11/16
收件者:spar...@googlegroups.com
Ok thanks - if you could raise and issue https://github.com/SparkViewEngine/spark/issues?state=open I'll take a look as see what can be done about it.

Cheers,
Rob


To view this discussion on the web visit https://groups.google.com/d/msg/spark-dev/-/zb2ysnvsBEgJ.

SamousPrime

未讀,
2012年11月19日 上午9:18:582012/11/19
收件者:spar...@googlegroups.com
Hello.
The same bug is already exists https://github.com/SparkViewEngine/spark/issues/31 .

пятница, 16 ноября 2012 г., 19:12:31 UTC+4 пользователь RobertTheGrey написал:
回覆所有人
回覆作者
轉寄
0 則新訊息