How do I use dynamic objects with asp.net? - dynamic

I have a dynamic object built inside IronPython and I would like to build controls on my asp.net page dynamically based on what types of objects are nested inside my dynamic object:
dynamic variousComplexObjects = IronPythonApp.GetControls();
repeater.DataSource = variousComplexObjects;
repeater.DataBind();
Can someone write me a quick example of what to do next? I'm sure there is a tutorial out there doing something similar, but I'm having a bit of trouble googling it. Feel free to recommend me the correct keywords or point me in the direction of properly consuming DLR data in an asp.net app.
Thanks!!

Assuming you use .net 4, you can just use dynamic in your databound event.
repeater.ItemDataBound += OnItemDataBound;
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
dynamic dynObj = (dynamic)e.DataItem;
string text = dynObj.Text; // Etc.
}
I'd probably have a type property or similar to check on - otherwise you're stuck with trying to use GetType() which I'm not certain whether works with IronPython.

Here's a thread entitled "Databind object with 'dynamic' properties". It involves creating customer get and set methods on the type.
It doesn't exactly fit with your scenario of C#'s dynamic and sourcing data from the DLR, but it might help.
Here's another thread entitled "Can I databind to a collection of Dynamic Class". The authored created a dynamic class using Reflection.Emit.
Please comment if any of these solutions fit your case. I'm also interested in the solution, but don't have the tolls to test, at the moment.

Thanks.. I guess nobody is coding in traditional asp.net web forms and dynamic objects. There's probably a way to do it.. but I just switched to MVC instead.

Related

C++/CLI - XML based active reports

This is in continuation to one of my previous queries(active reports in C /CLI). I am accessing an xml-based active report from a C++/CLI application. Is there any way by which I can have a data communication with the active report from C++/CLI, for example, I want to print the managed data present in the C++/CLI application on the details section of the XML report which the application accesses. I don't want to use any c# code. Can it be done? Thanks.
Sure, ActiveReports can do it. Since C++/CLI produces standard .NET objects you can create objects in C++/CLI and ActiveReports will bind to them. Create an IEnumerable collection of objects that you want to bind to (each object in the collection is like a database "row").
Take a look at the examples at Binding Reports to a Data Source. Expand the code sections under the heading To use the IEnumerable data source and you'll see how to do it in C#. You would do precisely the same thing in C++/CLI, you'll just change the syntax from C# to C++/CLI. Clearly, you know C++/CLI syntax so you can do that part, but I think this answers your question regarding how to do this with ActiveReports.
Update based on question asked in comments:
You should be able to handle an ActiveReports' event such as the FetchData event using something like the following code:
void MyFetchDataHandler(Object^ sender, FetchEventArgs^ eArgs)
{
//put handling code here...
}
myReport->FetchData += ref new FetchEventHandler(this, &MyClass::MyFetchDataHandler)
I didn't compile this (I don't have AR handy), but it should be close. Please see Microsoft's reference documentation on C++/CLI event syntax here.

Intellisense in VS 2012 RC not working for SignalR classes

I have imported SignalR Nuget package and SignalR sample is working well in my project. But even after having all required using statements I can't get the intellisense working for the classes in SignalR (like Hub class).
The hubs proxy is dynamically generated at runtime, so you won't get any intellisense for it.
You can use Hubify.exe (see Hubify-section on http://weblogs.asp.net/davidfowler/archive/2012/06/10/signalr-0-5-1-released.aspx ) to generate a static javascript-file.
Or you can create your own T4-Template that does the same thing. See: https://github.com/SignalR/SignalR/issues/106
Update:
Regarding intellisense for C#
You won't get intellisense for Clients and Caller, since they are dynamic.
Absence of compile-time type checking leads to the absence of IntelliSense as well. Because the C# compiler doesn't know the type of the object, it can't enumerate its properties and methods. This problem might be solved with additional type inference, as is done in the IronPython tools for Visual Studio, but for now C# doesn't provide it.
http://visualstudiomagazine.com/articles/2011/02/01/understanding-the-dynamic-keyword-in-c4.aspx
public class Chat : Hub
{
public void Send(string message)
{
// No intellisense for addMessage, sorry
Clients.addMessage(message);
}
}
look at the SignalR documentation here
the Hub.Caller and Clients are dynamic in nature.
dynamic is a new keyword added in .Net 4 and dosent support compile time checking so you cannot get intellisense for dynamic objects. all the dynamic objects are checked at runtime only. so even if you your self create a dynamic object like
dynamic d = new ExpandoObject();
and try to do this "d.". you wont get any intellisense because the framework dosent know whats all is present in the dynamic object. and will be discovered only at runtime.

Internationalization in VB.Net : Choosing the right structure type

I'm about to start translating my vb.net application, and I don't want to use the default methods provided by Visual Studio to do so. I need my application to be very light, and it nearly doubles it size to use the resources option.
Therefore, I'm planning to use some thing like a class, of which I would have one instance per language. Since I don't want to distribute language files as separate files (I'd rather have them hard-coded), I would like to find an easy way to check if every field of the class is initialized. I was thinking of something like an Interface, where I would do something like this:
Public Interface Language
Dim HelloMsg As String
Dim GoodbyeMsg As String
End Interface
Public class English Implements Language
HelloMsg = "Hello!"
GoodbyeMsg = "Goodbye!"
End Class
It's obviously not the right way to do it (although I could use properties instead of vars), but I was wondering whether the was a way to have the compiler check that everything is translated and warn about it if not.
Anyway, maybe is there a much better way to handle this problem ?
Thanks a lot!
CFP.
I'm not convinced that you should dump the resource-based localization approach just because your app has grown in size. Indeed, it could've grown from 100 Kb to 200 Kb, but this is it! It won't grow this much more. And 200 Kb is nothing nowadays.
So my advice is to reconsider and go resource-based route.
I've decided to use a singleton class that loads a translation file, with a method that loops through all items on a form and translate on-the-fly. See Create Synchronicity source code for more details (more specifically TranslateControl in this code file)

Subsonic VB.NET problem

When I'm using VB.NET to use subsonic, It seems to have problem marking records as Old and Clean. Whenever I query using ExecuteSingle or ExecuteTypedList, i need to manually MarkClean and MarkOld, else whenever I save it will save as a new record.
Am I the only one facing this problem ? I'm using SubSonic 2.2 btw.
I checked the source code of SubSonic.. and I found that the VB class generator doesn't implements the IActiveRecord. I think most likely is because VB.Net doesn't seem to support 're-implementation' of inheritance or whatever you call that...
So when I debug, I found that Utility.IsSubSonicType returns false (because the ActiveRecord class returns as IReadOnlyRecord, but IsSubSonicType checks for IActiveRecord and IRecordBase) and thus doesn't call the SetLoadState and MarkClean.
So I'm not sure if this is a bug or it is intentional. Any way to solve this?
When you use ExecuteSingle or ExecuteTypedList, you could be doing with a class that didnt have those properties, I think the intention is that you are populating a POCO and not (necessarily) an Entity or other ORM object.
ExecuteAsCollection and all of the .Load methods behave as you expect because they call SetLoadState() and/or MarkClean().
Personally, I dont face this problem because I use Subsonic purely as a (smart) DAL (CRUD Only) and my own entity layer takes care of things like dirty/new.
Yes, I had the same problem. MarkClean and MarkOld before setting properties and saving fixed the issue. see this

How do you implement C#4's IDynamicObject interface?

To implement "method-missing"-semantics and such in C# 4.0, you have to implement IDynamicObject:
public interface IDynamicObject
{
MetaObject GetMetaObject(Expression parameter);
}
As far as I can figure out IDynamicObject is actually part of the DLR, so it is not new. But I have not been able to find much documentation on it.
There are some very simple example implementations out there (f.x. here and here), but could anyone point me to more complete implementations or some real documentation?
Especially, how exactly are you supposed to handle the "parameter"-parameter?
The short answer is that the MetaObject is what's responsible for actually generating the code that will be run at the call site. The mechanism that it uses for this is LINQ expression trees, which have been enhanced in the DLR. So instead of starting with an object, it starts with an expression that represents the object, and ultimately it's going to need to return an expression tree that describes the action to be taken.
When playing with this, please remember that the version of System.Core in the CTP was taken from a snapshot at the end of August. It doesn't correspond very cleanly to any particular beta of IronPython. A number of changes have been made to the DLR since then.
Also, for compatibility with the CLR v2 System.Core, releases of IronPython starting with either beta 4 or beta 5 now rename everything in that's in the System namespace to be in the Microsoft namespace instead.
If you want an end to end sample including source code, resulting in a dynamic object that stores value for arbitrary properties in a Dictionary then my post "A first look at Duck Typing in C# 4.0" could be right for you. I wrote that post to show how dynamic object can be cast to statically typed interfaces. It has a complete working implementation of a Duck that is a IDynamicObject and may acts like a IQuack.
If you need more information contact me on my blog and I will help you along, as good as I can.
I just blogged about how to do this here:
http://mikehadlow.blogspot.com/2008/10/dynamic-dispatch-in-c-40.html
Here is what I have figured out so far:
The Dynamic Language Runtime is currently maintained as part of the IronPython project. So that is the best place to go for information.
The easiest way to implement a class supporting IDynamicObject seems to be to derive from Microsoft.Scripting.Actions.Dynamic and override the relevant methods, for instance the Call-method to implement function call semantics. It looks like Microsoft.Scripting.Actions.Dynamic hasn't been included in the CTP, but the one from IronPython 2.0 looks like it will work.
I am still unclear on the exact meaning of the "parameter"-parameter, but it seems to provide context for the binding of the dynamic-object.
This presentation also provides a lot of information about the DLR:
Deep Dive: Dynamic Languages in Microsoft .NET by Jim Hugunin.