Tuesday, September 29, 2009

Tuples!

New from Eco5 is the ability to handle tuples. Tuple is fancy word for something “taped together from bits and pieces of other stuff”. This is very useful when it comes to build result sets that is not necessary something that exists in your model. Like sums or collecting name and address together even if they are stored in different classes.

Tuples are what an sql server return when you omit the “*” and write explicit stuff in the select part. In fact the columns in the select part is the tuple-definition as far as the sql server is concerned.

In Eco 5 OCL operators create tuples: GroupBy and Collect.

Consider this model:

image

I want to GroupBy the contents of Class3.Attribute1;

IElement elem = ocl.Evaluate("Class1.allinstances.class2.class3->groupby(x|x.Attribute1)");

So what do I get back? A Tuple (don’t you just hate rhetoric questions? (I don’t))!

The tuple consists of a type with a generated name and specific properties. The properties are: Attribute1 and List.

The code below shows how to access the tuple result in code:

   1: private void RenderPrintbands()
   2: {
   3:   IOclService ocl=EcoServiceHelper.GetOclService(_ecoSpace);
   4:   IElement elem = ocl.Evaluate("Class1.allinstances.class2.class3->groupby(x|x.Attribute1)");
   5:   foreach (IElement listelem in elem.GetAsCollection())
   6:   {
   7:     string NameOfTupleResultForGroupedValue = ((listelem as IFeatured).Properties[0]).StructuralFeature.Name;
   8:     string NameOfTupleResultForTheGroupByList = ((listelem as IFeatured).Properties[1]).StructuralFeature.Name;
   9:  
  10:     foreach (IElement groupedlistelem in (listelem as IFeatured).Properties[NameOfTupleResultForTheGroupByList].GetAsCollection())
  11:     {
  12:       Class3 c3=groupedlistelem.GetValue<Class3>();
  13:     }
  14:  
  15:   }
  16: }
  17:  

If I had grouped in more stuff, my tuple would have more properties.

If my tuple property is not an existing “thing” from my model but a tuple in itself it will be named part1 (if I another one, part2 etc).

Monday, September 28, 2009

ViewModel override

I had a talk with Kaj Lund from Attracts; they are using the ViewModels for WPF. He felt that the use of a DataGrid in every detail list is a bit “too much”. For example if there is only one column to display, and that you do not want to allow edit or allow user sorting a ListView might be a much more natural choice.

This is a dilemma for all declarative development; you often get a “one size fits all” scenario. Of course there is a way around it… Our take on declarative development aims to remove all the un-necessary degrees of freedom but still leave you in full command. In this case I suggested that Kaj should use the ViewModelWPFUserControl.OnColumnUIOverride event.

This event fires before each component is created and you can create your own based on the given circumstances. Probably you have a set of UI rules in you project like if the details grid only contains 1 column it should be a ListView instead (or whatever).

So this is one way to implement this event in xaml:

   1: <vm:ViewModelWPFUserControl 
   2:     OnColumnUIOverride="ViewModelWPFUserControl2_OnColumnUIOverride" 
   3:     Height="180" x:Name="ViewModelWPFUserControl2"  
   4:     EcoSpaceTypeName="EcoProject1.EcoProject1EcoSpace" 
   5:     ViewModelName="Building">
   6: </vm:ViewModelWPFUserControl>

And in code:

   1: private void ViewModelWPFUserControl2_OnColumnUIOverride(object sender, Eco.ViewModel.Runtime.OnColumnUIOverrideArgs e)
   2: {
   3:     if (e.ViewModelColumn.StyleRef == "DisplayInListView" && 
   4:         e.ViewModelUIComponentType == ViewModelUIComponentType.grid &&
   5:         e.ViewModelColumn.DetailAssociation.Columns.Count>0)
   6:     {
   7:         e.ContinueWithDefault = false;
   8:         ListView lw=new ListView();
   9:         (sender as ViewModelWPFUserControl).LayoutGrid.Children.Add(lw);
  10:         lw.SetBinding(ListView.ItemsSourceProperty, new Binding() { Source = new EcoObjectDataProvider(e.ViewModelColumn.DetailAssociation.Handle) });
  11:         DataTemplate dt=new DataTemplate();
  12:         FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(StackPanel));
  13:         spFactory.Name = "factory";
  14:         spFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
  15:         FrameworkElementFactory tbfact = new FrameworkElementFactory(typeof(TextBlock));
  16:         tbfact.SetBinding(TextBlock.TextProperty, new Binding(e.ViewModelColumn.DetailAssociation.Columns[0].Name));
  17:         spFactory.AppendChild(tbfact);
  18:         dt.VisualTree = spFactory;
  19:         lw.ItemTemplate = dt;
  20:             
  21:         Grid.SetColumn(lw,e.ViewModelColumn.XPos);
  22:         Grid.SetRow(lw,e.ViewModelColumn.YPos);
  23:         Grid.SetColumnSpan(lw, e.ViewModelColumn.ColSpan);
  24:         Grid.SetRowSpan(lw, e.ViewModelColumn.RowSpan);
  25:     }
  26: }

What the code does is in this case: 1. Verify that this column has the StyleRef==”DisplayInListView” and some other prerequisites that I decided should control the use of the ListView.

Then I set e.ContinueWithDefault=false to stop the standard behavior. And then I just add a ListView in the correct position and bind it to the data…

Voilá, we have extended our rule base for the declarative development provided by ViewModels.

This code went into the Buildings and Owners sample from the download; the bottom-right-grid was replaced with a ListView:
image

Sunday, September 27, 2009

Forgotten video

I emailed Rick Weyrauch today about Modlr and I found this video in the KB that I still think is good but a bit forgotten. It shows the obvious stuff you need to know for modeling with Modlr.

There is a lot of features in Modlr that people tend to miss; did you know about the keyboard navigation stuff in the video before?

Premiere

I have really been dissatisfied with our KB software. It is slow all over and a pain to work with. All I was after was a simple way to produce content quickly.

This new approach we are introducing here hopefully will meet our requirements that are:
Spellchecker
Copy-paste images
Easy way to publish code snippets
Ability for users to comment
Easily indexed by search engines
Zero administration

The setup is this

Use the Google-blogger account, with a publish to our site option. And the Windows Live Writer along with the plugin for code snippets.

So here is the main test. Images: image

And Code:

   1: private void button1_Click(object sender, EventArgs e)
   2: {
   3:     WFormsClient.ServiceReferenceGaffrServerAdmin.GaffrServerAdminClient c2 = new WFormsClient.ServiceReferenceGaffrServerAdmin.GaffrServerAdminClient();
   4:     WFormsClient.ServiceReferenceGaffrServerAdmin.CompositeType ct2 = new WFormsClient.ServiceReferenceGaffrServerAdmin.CompositeType();
   5:     WFormsClient.ServiceReferenceGaffrServerAdmin.GaffrModel m = new WFormsClient.ServiceReferenceGaffrServerAdmin.GaffrModel();
   6:     if (File.Exists(textBoxfile.Text))
   7:     {
   8:         byte[] bytes = File.ReadAllBytes(textBoxfile.Text);
   9:         m.Model = Convert.ToBase64String(bytes);
  10:  
  11:         c2.UploadModel(textBox2.Text, Path.GetFileNameWithoutExtension(textBoxfile.Text) , m);
  12:     }
  13:  
  14: }

What do you think? For me its a keeper any way.

The encoding works ok : ÅÄÖ åäö , utf8 all the way.

test post

test post content
 
Contact Us | Terms of Use | Privacy Statement © 2009 CapableObjects