mimicking MVC DisplayExtensions methods (fun and confusion with lambdas) - asp.net-mvc-4

I'm trying to develop my own editable data grid for MVC in a fluent interface usable in a View. My question is really about using lambdas, not about data grids, but the context might help for understanding my question. Anyway, I have some code that looks like this:
#model IEnumerable<AdamOneilSoftware.Models.Something>
#{
var firstItem = Model.FirstOrDefault();
}
#(Html.DataGrid(Model)
.CssClass("data")
.TextBox(model => firstItem.Text)
.TextBox(model => firstItem.Position)
.Render())
This much compiles okay. You can see that I want to use lambdas to setting up columns of my data grid. The thing I don't understand is how to do anything with those lambdas in my data grid class. I have surmised that I need to compile/invoke the expression, but I'm stuck on the Invoke part. Here's what I have:
public DataGridBuilder<TModel> TextBox<TValue>(Expression<Func<TModel, TValue>> expression)
{
var del = expression.Compile();
// invoke the delegate?
var result = del.Invoke(); // but this expects a TModel argument
return this;
}
The Invoke call expects a TModel argument (according to the Func declaration -- I understand that). But this doesn't work--and rightly so:
var result = del.Invoke(TModel);
How do I form the TModel argument in the Invoke call? Or am I heading the wrong direction completely? The goal is to do something like what the DisplayFor and EditorFor methods do--render some HTML with some awareness of the target property's metadata (DataFormatString, Display label, and so on).

You need to get the view's Model instance to pass as the parameter.
You can find that in Html.ViewData.Model.

Related

Recommended pattern for setting initialization parameters to custom widget

I'm creating my own template based widgets and I was trying to pass some objects through the constructor on creation like this
var widget = new myWidget(obj1, obj2, obj3);
where my constructor of the widget looks like
constructor: function(param1, param2, param3)
However I was getting some errors and found they were due to _WidgetBase functionality (specifically the create method) that is expecting something special in the first and second parameters.
create: function(params, srcNodeRef)
So in order to avoid my parameters nuking the params, and srcNodeRef that was expected in position one and two, I had to move my parameters to after the second position like this
constructor: function (params, srcNodeRef, myParam1, myparam2, myParam3)
But naturally this is not an expected way to solve this compared to the usual way to instantiate objects in normal object oriented languages (ex. c#)
My question is, is there a recommended pattern for passing initialization parameters to a custom widgets constructor, that avoids this issue of having to remember the first and second parameter positions are reserved?
NOTE:
An important note is that whatever parameters I send into the widget, must be acted on or made available before postCreate executes, just like it is if I passed them to the constructor.
Actually, there is a "dojo" way to pass parameters into your widget:
var widget = new myWidget({obj1: obj1, obj2: obj2});
In instance of your widget these object will refer to
this.obj1, this.obj2. You don't have to override constructor.
Some comments from dojo source of _WidgetBase on this topic:
//////////// INITIALIZATION METHODS ///////////////////////////////////////
/*=====
constructor: function(params, srcNodeRef){
// summary:
// Create the widget.
// params: Object|null
// Hash of initialization parameters for widget, including scalar values (like title, duration etc.)
// and functions, typically callbacks like onClick.
// The hash can contain any of the widget's properties, excluding read-only properties.
// srcNodeRef: DOMNode|String?
// If a srcNodeRef (DOM node) is specified:
//
// - use srcNodeRef.innerHTML as my contents
// - if this is a behavioral widget then apply behavior to that srcNodeRef
// - otherwise, replace srcNodeRef with my generated DOM tree
},
=====*/
I +1'd Kirill's answer as that's the easiest. But from the other comments it sounds like you might need to massage the input or initialize other variables based on the input.
If so, take a look at the postMixinProperties lifecycle method and override it in your widget. If your widget is templated and the template expects the massaged data, you'll need this. In here you refer to your properties with this as you expect.
postMixInProperties: function(){
// summary:
// Called after the parameters to the widget have been read-in,
// but before the widget template is instantiated. Especially
// useful to set properties that are referenced in the widget
// template.
// tags:
// protected
},
Don't forget to invoke this.inherited(arguments); in here as you should in all of the dijit lifecycle methods.
Defining setters for you properties is another way to massage these properties. You'll want this if a template will use these properties. Example of a setter from the Writing Widgets page. So here 'open' would be the name of the parameter as passed to the contructor, or in a widget template.
_setOpenAttr: function(/*Boolean*/ open){
this._set("open", open);
domStyle.set(this.domNode, "display", open ? "block" : "none");
}

Chain up to 'Gtk.Box.new' not supported

I'm new to Vala and so far I think it's pretty cool but I'm having trouble understanding inheritance. I read here that I should use base() to call the parents constructor. Alright, cool, seems understandable but It din't work for me. I kept getting the error on the title. Here is my snippet to show:
public class MyBox : Gtk.Box {
public MyBox(Gtk.Orientation orientation, int spacing) {
// I have to this
this.set_orientation(orientation);
this.set_spacing(spacing);
// I want to do this:
base(orientation, spacing);
//workaround is this:
Object(orientation: orientation, spacing: spacing);
}
}
Please help me understand why Object(....) works but not base(...)
Shouldn't it be the same thing?
This is due to implementation of the C code. When Vala generates a constructor, it generates two C functions a _new function that allocates memory and calls the _construct and a _construct function that initialises the object. When you case the base constructor using base(), it needs a matching _construct function to call. Not all the classes written in C have this; in the VAPI file, you will find has_construct_function = false for some constructors. If this is the case, no chain-up can be done. The base GObject can set properties from arguments, so this becomes the only way to set defaults in the base class.

ASP MVC Lambda Expressions or Query Extensions?

I am using a Model Factory to query the related entities from requesting model.
The result is a ViewModel from a given entity.
Get Data
var posts = _postRepo.GetCommunityPosts(Id); // get an iqueryable
ICollection<PostViewModel> result = await ModelFactory.PostFactory(posts, take, skip); //using the ModelFactory to create the viewmodel
Process Data
public async Task<ICollection<PostViewModel>> PostFactory(IQueryable<TRDPost> posts)
{
return await posts.Select(a => new PostViewModel //the viewmodel
{
// simple properties
Created = a.Created,
Body = a.Body,
Id = a.Id,
// related viewmodels
Member = new MemberViewModel
{
// Member related stuff here
// a lot of properties and also related viewmodels here
}
}
}).ToListAsync<PostViewModel>();
The Question
Having a lot of different Factories which all have the "Member" View Model. This is really a bad style because I have tons code redundancy. Is there a way to execute a function in the query, e.g.:
public async Task<ICollection<PostViewModel>> PostFactory(IQueryable<TRDPost> posts)
{
return await posts.Select(a => new PostViewModel //the viewmodel
{
// simple properties
Created = a.Created,
Body = a.Body,
Id = a.Id,
// related viewmodels
Member = GetMemberViewModel(a) // is this possible?
}
}).ToListAsync<PostViewModel>();
Read a lot about Expressions all I tried was without success. It would be nice if you point me into the right direction.
Thanks to Tomas Petricek who said this worth words:
Declaration of lambda expression that will be used by the query is
straightforward, because it isn't different than any other C# lambda
expressions. The query is more interesting because it uses two
extension different methods. First method called ToExpandable creates
a thin wrapper around the DLINQ Table object. Thanks to this wrapper
you can use second method called Invoke that extends the Expression
class to invoke the lambda expression while making it still possible
to translate query to T-SQL. This works because when converting to the
expression tree, the wrapper replaces all occurrences of Invoke method
by expression trees of the invoked lambda expression and passes these
expressions to DLINQ that is able to translate the expanded query to
T-SQL.
And reading this article must be helpful: http://tomasp.net/blog/linq-expand.aspx/

Perform an overload resolution programmatically

Is there a way to use either System.CodeDom or the Roslyn API to resolve an overloaded method call from code? At runtime, I have a method name and a list of parameters, I want to find the method the compiler would have picked at compile time.
As #SLaks and #just.another.programmer suggested it is possible to exploit C# dynamic infrastructure. But it's really not straightforward nor simple. I was gazing into decompiled code for couple of hours to find following magic code. And I don't understand it completely. In the first step Binder class is created, with parameter options set to use compile time types.
In the second magic step, the binder is called with the actual parameters. Each parameter has its value (null, its not used when UseCompileTimeType is set) and their expression. If you know types instead of expression, use Expression.Parameter(yourType) to substitute them.
public static Expression GetInvokeMemberExpression(Expression expr, string name, Expression[] arguments)
{
var type = expr.Type;
var argTypes = arguments.Select(e => e.Type).ToArray();
// do magic #1
var binder = (DynamicMetaObjectBinder)Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(
CSharpBinderFlags.None, name,
Type.EmptyTypes, // type arguments
typeof(object),
Enumerable.Repeat(0, argTypes.Length + 1).Select(_ => CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null)).ToArray());
// do magic #2
var result = binder.Bind(DynamicMetaObject.Create(null, expr), arguments.Select(a => DynamicMetaObject.Create(null, a)).ToArray());
var resultMethodCall = (result.Expression as UnaryExpression).Operand as MethodCallExpression; // unwrap convert to ebject expression
var method = resultMethodCall.Method;
return resultMethodCall;
}
I was trying similar piece of code in DotVVM framework, but I haven't found a way how to "inject" custom implicit conversions, so I wrote a simple method overload recognition function. But we are still using dynamic for operators. You can find the code on github

flex 3 iterate through object values

i have an object which represents a database table. I want to iterate through this object and print printing each value. What can i use to do this?
i want to do this inside my mxml not actionscript
for each object attribute i want to create an imput field
Look up the documentation on Flex 3 looping. If you do, you'll find this:
for..in
The for..in loop iterates through the properties of an object, or the elements of an array. For example, you can use a for..in loop to iterate through the properties of a generic object (object properties are not kept in any particular order, so properties may appear in a seemingly random order):
var myObj:Object = {x:20, y:30};
for (var i:String in myObj)
{
trace(i + ": " + myObj[i]);
}
// output:
// x: 20
// y: 30
Instead of trying to create an input field for each object, I'd suggest you take a look at DataGrid and custom ItemEditors.
I agree that this answer isn't useful. It only works with generic objects, not user declared
objects.
However, here's some code that should/could work using the describeType as suggested above. (And I don't really think it's too complex). Be aware that only public properties/methods, etc. are exposed:
var ct:CustomObject = new CustomObject();
var xml:XML = describeType(ct);
for each(var accessor in xml..accessor) {
var name:String = accessor.#name;
var type.String = accessor.#type;
trace(ct[name]);
}
The problem with "for...in" is that it iterates only on dynamic properties. That is, if your object is defined as a Class (and not dynamically), "for..in" won't give anything.
The ActionScript documentation suggest to use describeType() for fixed properties, but it looks over-complicated for this simple taskā€¦
You can write it like actionscript but include it inside the mxml file with the script tag:
<mx:Script>
<![CDATA[
public function LoopAndPrint() : void
{
//your code here
}
]]>
</mx:Script>