Monday, August 23, 2010

Silverlight4 + RIA services : DomainDataSource not found.

I've started to review Silverlight4 + RIA services and got some minor problem following some of the videos/website.

Specifically I could not find the DomainDataSource.

While watching this great video by Tim Heuer I got stuck when he starts talking about the DomainDataSource.

Assembly: System.Widows.Controls.DomainServices.dll

For some reason DomainDataSource was not in the toolbox. Furthermore, I could not even find it neither on Add new reference dialog nor in the GAC.

If found that assembly at:
C:\Program Files (x86)\Microsoft SDKs\RIA Services\v1.0\Libraries\Silverlight

So this post might save you some searching minutes..

Monday, October 26, 2009

Microsoft Security Essentials and SourceGear Vault

Lately I've been having problems using Source Gear Vault version 4.1.4
I've never had any problem like this with Vault so i started thinking what did I install on the last days. The answer was Microsoft Security Essentials.

I was getting this ugly error message:
Getting latest version of files
Local file update for $/MyFile.cs failed: System.UnauthorizedAccessException: Access to the path 'C:\Users\genwise\AppData\Local\Temp\tmpC692.tmp' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.Delete(String path)
at VCDiff.VCDiff.Apply()
at VaultClientOperationsLib.WorkingFolder.UpdateHiddenFileContentsFromDelta(VaultClientFile file, String deltaFile, Int64 baseVersion, Int64 targetVersion, UInt32 targetCRC, Int64 targetSize)
at VaultClientOperationsLib.UpdateThread.ProcessCommand(UpdateThreadCommand command, UpdateThreadCommandResult& outputResult)


Luckily they guys at Microsoft included a nice option to exclude certain files and folders. Thanks to this I was able to get rid of this problem ( I am not a security export so i hope I didn't open a Pandora Box with this change..)

Sunday, October 11, 2009

IIS 64bits and Oracle 32bits

At the office we have new i7 machines with 64bit OS.
I've spent 2hs to find a solution to this problem so it's worth blogging about it.

I had a DNN site using my own BOLayer which in turn used Oracle (10.2)

I was getting this error on IIS : "the provider is not compatible with the version of Oracle client"

Tried almost everything until I've noticed a nice option in the IIS Application pool advance settings:
"Enable 32-Bit Applications" which weirdly enough was configured as false (default value).





Thursday, October 8, 2009

Google Translate API for .NET 0.3

We use localization in some of our WPF projects and I was wondering how easy would be to integrate the Google Translate API. I must say it has been unbelievable easy to use it! Check the code below:


using Google.API.Translate;
using NUnit.Framework;
 
namespace GoogleTranslation
{
    [TestFixture]
    public class GoogleTransationFixture
    {
        [Test]
        public void TranslateTest()
        {
            var client = new TranslateClient("test");
            var result  = client.Translate("casa", Language.Spanish, Language.Dutch);
            Assert.IsNotEmpty(result);
            Assert.AreEqual("huis", result);
        }
    }
}

Thursday, February 26, 2009

Creating RibbonTab programatically

I've spent several hours on this issue so it's worth document it just in case it might help someone out there...

When using CWPF (aka Prism) in combintation with the RibbonControlsLibrary I was having a weird problem that my RibbonTabs created "inside" the Prism Modules where loosing it's bindign context and therefore the icon/text was gone (the 2nd time).


Both  Bartek Szafko and Joachim Kerschbaumer articles explain very nicely how to create adapter to be able to use RibbonTabs inside CWPF. I am not going to talk about that in this entry so please read them before in case you are just starting with the RibbonControlsLibrary .

My problem is that each of the RibbonTabs created inside CWPF IModule is also driven by a ViewModel. 

  public partial class DynamicTab 
    {
        public DynamicTabVM ViewModel { get; private set; }

        public DynamicTab()
        {
            InitializeComponent();
            DataContext = ViewModel= new DynamicTabVM();
        }

The technique of re-attaching the RibbonTab works fine if there is no databinding or it's using static resources.

In my scenario all the RibbonButtons have a Bindingg against an ICommand in the viewModel.
                     
When "switching" between Ribbon Tabs --> the DataContext was gone for the "dynamic" RibbonTab. 

I COULD not solve the problem by changing the binding to RelativeSource. Probably this is because I am new to WPF binding and the IDE (vs.net 2008) does not really help at all here.


      private void AddDynamicTabs()
        {
            //Create the Tab.
            var dynamicTab = new DynamicTab();

            // The tab DataContext is the ViewModel.
            var vm = (DynamicTabVM) dynamicTab.DataContext;

            // Take the included ribbon tab.
            RibbonTab tab = dynamicTab.DynamicRibbonTab;
            
            // Disconnect from container
            dynamicTab.Content = null;

            // Re-assign the DataContext
            tab.DataContext = vm;


            foreach (var group in tab.Groups)
            {
                group.DataContext = vm;

                foreach (var control in group.Controls)
                {
                    if (control is RibbonButton)
                    {
                        (control as RibbonButton).DataContext = vm;
                    }
                    // TODO : Applies to more controls.. We are currently using RibbonButton.
                }
            }

            MyRibbon.Tabs.Add(tab);

            // This is needed to be able to view the NEW tab using the correct Style/Skin.
            MyRibbon.InvalidateVisual();

        }


You can download the Example code.

The example is not actually using CWPF and therefore this code should be included in the Adapter. 



Thursday, November 6, 2008

VSCT Designer

Long time since my last entry, I have just moved and have been extremely busy / chaotic during the last weeks. Back online now.

I've found the following tool this seems very interesting for anybody working with DSL or VSX: 
http://www.codeplex.com/VSCTDesigner

I did have a little problem with adding bitmaps and therefore I've created the following post in the Codeplex website.

Wednesday, July 9, 2008

How to copy hibernate.config to the VSTS Output Folder?

VSTS is now included by default in the standard/professional version of vs.net 2008. Until now i was using NUnit + TestDriver.net, etc.

I've decided to test VSTS and found 1 big problem : my nhibernate.config file was not copied to the output directory of the test output folder. I was expecting this to be automatically since I've set the "Always Copy" in the build property for the file.

Doing some basic Google searches I've noticed I am not the only guy having this problem.

The solution is to open the "localtestrun.testrunconfig" file which is added automatically to the solution by VSTS, go to "Deployment" and then add the file you want to deploy/copy.










Once this is done, the hibernate.config file copied every time we run a test. This can be seen in the image below. VSTS creates 1 output folder for each test run.

Saturday, June 28, 2008

Code Generation 2008 : more than just content

The conference is now over.

Besides the fact of being a very good conference, one the most important things is that during the conference I've met wonderful people. Not only we had a lot of fun together but also we discussed some very interest topics.

Pedro Molina , me , Alejandro Silva and Gaston Milano

It's was amazing to see how much we had in common and how much effort we had wasted trying to solve all of us the same stuff. We are working in our little isolated island without having ways to correctly shared/reuse some of the common concepts in our tools/products.

Besides there deep technical knowledge and wonderful ideas, the most important thing was that we had a lot of fun together.

Another person which I was very pleased to met from Microsoft was Jean-Marc Prieur (picture, and yes, the guy in the background is Steve Cook).

He was "extremely" helpful, always open to new ideas , listened far beyond the extend of his responsibility, stayed "after" the conference to keep listening.
"Chapeau!!!".

Later during next week I will write down more stuff related to my point of view of some very specific stuff I've learn.

Monday, June 23, 2008

DSL : Model References (Backplane)

A couple of months ago I've wrote this MSDN Forum post where I was a little bit confused (and surprised) with the way of solving DSL Model References.

Edward Bakker finally found out that in Rosario the VSX team is actually giving a standard solution for this problem. Here is a detail BLOG entry about his findings, which I found very interesting.

What I wonder is when (and package into what product? I hope/think it should be the SDK), this improvement will be available.I've tried to google based on "Microsoft.VisualStudio.ToolIntegration.Backplane.dll" and Edward's blog was the only result.

I will try to find this information in Code Generation conference which I will be attending this week.

Tuesday, April 8, 2008

ObjectDataSource : Manually controlling the sorting expression.

By default the GenWise ASP.NET Templates uses the object datasource SortParamterName. When that property is used , the objectdatasource will manipulate internally the sort expression (change to asc , desc) .

  269     ObjectDataSource runat="server" ID="ObjectDataSource1" TypeName="BOLayer.CustomerFactory"
  270                 SelectCountMethod="Count" EnablePaging="True" UpdateMethod="Save" DeleteMethod="Delete"
  271                 SortParameterName="pSortExpression" MaximumRowsParameterName="pMaxResult" StartRowIndexParameterName="pFirstResult"
  272                 SelectMethod="GetAll">
  273                 
  274                     Parameter Name="pSortExpression">Parameter>
  275                     Parameter Name="pCriteria">Parameter>
  276                 
  277                 
  278                     Parameter Name="CustomerID">Parameter>
  279                 
  280             ObjectDataSource>


For more advanced sorting scenarios you might want to manually control what's passed to the pSortExpression method parameter. This can be done in the following way :

Subscribe to the ObjectDataSource Selecting method (genwise does that automatically)
   44 ObjectDataSource1.Selecting += new ObjectDataSourceSelectingEventHandler(ObjectDataSource1_Selecting);



  253    protected void ObjectDataSource1_Selecting(object pSender, ObjectDataSourceSelectingEventArgs pEventArgs)
  254         {
  255             if (pEventArgs.ExecutingSelectCount)
  256                 pEventArgs.InputParameters.Remove("pSortExpression");
  257             else
  258                 pEventArgs.InputParameters["pSortExpression"] = _newSortExpression;
  259 
  260             pEventArgs.InputParameters["pCriteria"] = Browse1_GetCriteria();
  261         }

Where does _newSortExpression come from ?


  198         string _newSortExpression = null;
  199         protected void Browse1_Sorting(object pSender, GridViewSortEventArgs pEventArgs)
  200         {
  201             // Calls this method to adapt the sort syntax (required for Composite sorting).
  202             _newSortExpression = WebUtils.SortExpressionTransformer(pEventArgs);
  203             pEventArgs.Cancel = true;
  204         }


SortExpression Tranformer takes the ObjectDataSource parameters and manipulates them to make them compatible with the GenWise Factories sorting syntax.

Below is the code of this method:

   63   public static string SortExpressionTransformer(GridViewSortEventArgs pEventArgs)
   64     {
   65         if (pEventArgs == null) throw new ArgumentNullException("pEventArgs");
   66 
   67         if (pEventArgs.SortDirection == SortDirection.Ascending)
   68         {
   69             return pEventArgs.SortExpression;
   70         }
   71 
   72         string newSortExpression = pEventArgs.SortExpression;
   73         string DESC = " DESC";
   74 
   75         if (pEventArgs.SortDirection == SortDirection.Descending)
   76         {
   77             if (pEventArgs.SortExpression.Contains(","))
   78             {
   79                 // Case : We have a composite sorting ( Field1,Field2) 
   80                 newSortExpression = string.Empty;
   81                 string[] sortParts = pEventArgs.SortExpression.Split(',');
   82                 for (int i = 0; i <> 
   83                 {
   84                     string sortPart = sortParts[i];
   85 
   86                     int sortDirectionIndex = sortPart.IndexOf(DESC);
   87                     bool isDescending = (sortDirectionIndex > -1);
   88 
   89                     string property = sortPart;
   90                     if (isDescending)
   91                     {
   92                         // sta : udpate templates.
   93                         //property = sortPart.Substring(0, (sortPart.Length - sortDirectionIndex + 1));
   94                         property = sortPart.Substring(0, (sortPart.Length - sortDirectionIndex + 1 + DESC.Length));
   95                     }
   96 
   97                     if (i > 0) newSortExpression += ",";
   98                     newSortExpression += property;
   99 
  100                     // Only add the DESC to all except the last (Last one will be controlled automatically by the object datasource.
  101                     if (!isDescending)
  102                     {
  103                         if (i < (sortParts.Length - 1))
  104                             newSortExpression +=  DESC ;
  105                     }
  106                 }
  107                 pEventArgs.SortExpression = newSortExpression;
  108             }
  109         }
  110         return newSortExpression;
  111     }

Monday, April 7, 2008

GenWise generated project added to vs.net solution using Source control.

Several customers have asked me how they can integrate an existing GenWise project into their vs.net solutions. To be more specific, GenWise already generates 100% compatible project files, so what's the problem?

Well, If you want to add the generated project into an existing solution and at the same time you can to keep that solution in source safe ( or in sourcegear vault which I use and recommend) then you might hit some little problems.

Problem #1 : Every time you regenerate the project is seen as a "new" project for the solution.
The reason behind this is that when you add the project to the solution the .proj file is modified with specific source control information + and ID (Guid) is given, etc.
To solve this problem you can "copy/paste" this information from the "modified" version of the project file INTO the GenWise project, so when you re-generate you don't loose this info .

Click on the image to enlarge an example for the solution to problem #1.




Problem #2 : After you have added the project to a source control provider ( vss or vault) all the files will be automatically read-only (by default) so you need to explicity do a complete project check out before you use the regenerate options from GenWise.

Thursday, February 7, 2008

Custom Security MembershipProvider & Login attempts (MaxInvalidPasswordAttempts)

The Problem : Have a limit on the amount of possible logins when using a Custom provider.

Let's say that you write your own MembershipProvider (quite common if you want to use your own object model ..)

   11   public class MyMemberShipProvider : System.Web.Security.MembershipProvider
   12     {

Like many other articles explain, the most important Method is ValidateUser.
What very people often "forget"to mention is that all other features are not present in your provider. For example, the MaxInvalidPasswordAttempts is a property that you can set it to any number but nothing is going to happen.

In a Nutshell : you need to implement it yourself.

What I am showning below is a quite simple version but it's proves the point.

  180     public override System.Boolean ValidateUser(System.String pUsername, System.String pPassword)
  181         {
  182             if (pUsername.Equals("demo") && pPassword.Equals("demo"))
  183             {
  184                 return LoginSuccess();
  185             }
  186             return LoginFailed();
  187 
  188         }

LoginFailed() will basically return false but also it would do the check if another attempt should be granted or not:

  211     private bool LoginFailed()
  212         {
  213             //WebFailureAuditEvent.Raise(WebEventCodes.AuditFormsAuthenticationFailure);
  214             LoginFailuresCount++;
  215             if (LoginFailuresCount >= this.MaxInvalidPasswordAttempts)
  216                 throw new Exception("OK, that's it. Bye Bye");
  217 
  218             return false;
  219 
  220         }



You need to keep track of how many login failures :

  197  protected int LoginFailuresCount
  198         {
  199             get
  200             {
  201                 if (HttpContext.Current.Session["Failures"] == null)
  202                     return 0;
  203                 return (int)HttpContext.Current.Session["Failures"];
  204             }
  205             set
  206             {
  207                 HttpContext.Current.Session["Failures"] = value;
  208             }
  209         }


In the commented line where I was trying to Raise the "WebFailureAuditEvent", I read somewhere that I was a good idea to raise it but could not find any reference or example. If anybody knows how to do it --> please let me know.

Note: An improvement to this model would be to support "PasswordAttemptWindow" which in this case I did not implement.

Note: New Browser == new Session so it's not really state-of-the-art security technique it's a basic

Thursday, January 17, 2008

DSL - How to use DesignVerbs with shapes?

Although I've previously wrote in this post about how you could add Menu Commands to shapes, I am still searching for alternative (maybe better?) approaches for exposing commands from my domain classes.

Why? Because the other way is too complex (too many steps) and I have many actions/commands.

Every time you click on a shape, a PropertyGrid is used to expose the ShapeProperties + DomainProperties (yes, they are merged in the PropertyGrid).

So, one natural approach would be to think "Why not add DesignVerbs as done for Custom Controls?". This question was asked in the msdn forum with null replied (until now ;) )

What I did was combined the idea from Geoff Appleby with a Custom partial class for each shape (off course only for the ones you need some special command/verb).

This is how the final solution looks like:

As you can see, "My Custom Action" and "Synchronize" are my exposed Actions/Commands. Here is the code that defines what to show:

public DesignerVerbCollection Verbs
{
get {
DesignerVerbCollection verbs = new DesignerVerbCollection();
verbs.Add( new DesignerVerb("My Custom Action", new EventHandler(ExecuteAction1) ));
verbs.Add(new DesignerVerb("Synchronize", new EventHandler(Synchronize)));
return verbs;
}
}

private void Synchronize(object sender, EventArgs e)
{
ExampleElement element = this.ModelElement as ExampleElement;
if ( element == null ) return;
MessageBox.Show("Action 1 was executed on model Element:" + element.Name);
}

Off course it's not only that since you need to also implement 2 interfaces:

public partial class ExampleShape : DslDiagrams::NodeShape, IComponent, IVerbsHost
{

private ISite _site;

public ISite Site
{
get
{
if (_site == null)
_site = new VerbSite(this, new VerbMenuCommandService(this));
return _site;
}
set { _site = value; }
}

public interface IVerbsHost { DesignerVerbCollection Verbs { get; }}

Drop me an e-mail if you are interested in the complete source code ( blogger does not have file upload i think.. need to check..)

TODO : Next I would like to combine both techniques be able to access this design verbs not only from the PropertyGrid but also from the Shape's right mouse and DSL Explorer.

Tuesday, January 15, 2008

Cleaning "old" DSL Languages

If you are , like me, new to DSLs you will end up creating many DSL projects. ;)

After a while your "New Item" dialog will end up looking really crowded.

Don't ask me why but the "Reset the Microsoft Visual Studio 2008 Experimental hive" option does not seem to work on all cases, and I still have several old dsl languages which won't go away. ( Note: I guess that the reset option by design will not mess us local user's document as explained below)

First, I've tried , with no success, to follow the tip from "El Bruno" (spanish blog) .

What's the solution ?

You need to "remove"all the zip files in the following directories:

C:\Users\\Documents\Visual Studio 2008\My Exported Templates
C:\Users\\Documents\Visual Studio 2008\Templates\ItemTemplates\CSharp\1033
C:\Users\\Documents\Visual Studio 2008\Templates\ItemTemplates\VisualBasic\1033

Note: On every DSLPackage build , a new file .zip will be created and placed on that directory. So before trying to open your DSL make sure you build the project first.

Wednesday, January 9, 2008

Text Templating (tt) : calling custom method

The simplest re-usability technique inside .tt files would be to call a class method.

Below there is an example of how to achieve this:

It's important to notice that this syntax has changed because long time ago you could have seen it as <#! , otherwise look at this old post from Gareth

Text Templating (.tt) Validation

DSL supports a nice validation model for the Domain classes/model.

But what about the Text Templating requirements?

This are validations that apply only to a specific generation task. Let's say if have a property named DotNetType. It could be that this property does not affect generation of [t1] but I need this property for [t2]. For this cases, you will need to somehow validate in the (tt) Template itself.


Note: You could argue (please don't) that the example below is a bad example, I know but there are still some other real cases.

The 'correct' way








I must say that before finding out that I've tried this 2 other options:

  • Raise an exception (hard but work since it's integrated with the Error list as shown below):

  • Just add a comment to the output file (this is not always possible ( example : asp.net template based controls like GridView don't support XML comment inside their tpl tags..)


Friday, December 28, 2007

Adding a Menu to a DSL (vs.net 2008 SDK + VSCT file)

It's just strange that something so simple is not integrated as part of the basic features of the DSL tools themselves. Furthermore, the only DSL book I have it shows old code (refers to the old ctc file), the document in the vs.net 2008 SDK shows the same problem, etc.

The "closest" example I've found is this one: El Bruno (Spanish) but unfortunately, this is also based on CTC file and not using the new VSCT example. I must admit that with the new VSCT file it's easier than the previously CTC file.

All the BLOG's I found showed vs.net 2005 examples all using the old ctc file.

I first tried to use Clarius SFT (Software Factories Toolkit) which probably would have been a better options but I could not install it ( VS.NET 2008 RTM , can't install old GAX extensions…so SFT would also fail to install). Victor Garcia Aprea had exactly the same problem as looks like I share his point of view about how obscure adding a menu could be.

This msdn page: VSCT Samples in C# showed me the missing link: How to define the command Guid's and ID's.

Step#1: Modify Commands.vsct

  • Project = DSLPackage
  • Open Commands.vsct
  • Add Symbols ( Guid + ID)
    • Please remmember that the commands ID and GUID need to be unique not only in your project but in your whole vs.net environment. I’ve created by accident 2 projects which shared the same ID and I was getting the wrong text.
  • Add Commands
Example modified VSCT File

   18 <Commands package="guidPkg">
   19 
   20     <Buttons>
   21       <Button guid="cmdImportDbSchemaGUID" id="cmdImportDbSchemaID" priority="0x0902"  type="Button">
   22         <Parent guid="guidCmdSet" id="grpidContextMain"/>
   23         <Strings>
   24           <CanonicalName>cmdImportDbSchemaCanonicalName>
   25           <ButtonText>Import from Database SchemaButtonText>
   26           <ToolTipText>Use this option to create the domain from an existing Database SchemaToolTipText>
   27         Strings>
   28       Button>
   29 
   30       <Button guid="cmdImportClassesGUID" id="cmdImportClassesID" priority="0x0902"  type="Button">
   31         <Parent guid="guidCmdSet" id="grpidContextMain"/>
   32         <Strings>
   33           <CanonicalName>cmdImportClassesCanonicalName>
   34           <ButtonText>Import from Existing ClassesButtonText>
   35         Strings>
   36       Button>
   37     Buttons>
   38   Commands>
   39 
   40   <Symbols>
   41     <GuidSymbol name="cmdImportDbSchemaGUID" value="{EC120F9A-9E7F-469d-8D61-F4E2A97E5725}">
   42       <IDSymbol name="cmdImportDbSchemaID" value="0x810">IDSymbol>
   43     GuidSymbol>
   44 
   45     <GuidSymbol name="cmdImportClassesGUID" value="{EC120F9A-9E7F-469d-8D61-F4E2A97E5726}">
   46       <IDSymbol name="cmdImportClassesID" value="0x811">IDSymbol>
   47     GuidSymbol>
   48   Symbols>
   49 CommandTable>

Step#2: Modify Package version
  • Locate the Package.tt , file , edit
  • Increment the version number (2nd parameter) as shown below

Step#3: Search for "your" CommandSet class
  • Search in your DSL Solution (DslPackage project) for the text “DslShell::CommandSet”
  • You should notice that’s found 1 time in the CommandSet.cs
  • If you now find the references for that found class (in this case DomainDesignCommandSetBase ) but in your project will be named differently. You should find that it’s derived + made partial to allow customization
Step#4: Customize your specific CommandSet
  • (optional) Create a folder to hold your own customized classes
  • Create a partial class (as shown below)
    1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5 using System.ComponentModel.Design;
    6 using Microsoft.VisualStudio.Modeling.Shell;
    7 
    8 namespace GenWise.VsNet.DomainDesign.DslPackage
    9 {
   10     internal partial class DomainDesignCommandSet : DomainDesignCommandSetBase
   11     {
   12         // Note: Here you need to repeat the code from the VSCT .
   13         //       I could not find an easy way of re-using the Symbols from the vsct
   14         public Guid dbImportSchemaGuid = new Guid("EC120F9A-9E7F-469d-8D61-F4E2A97E5725");
   15         public const int dbImportSchemaID = 0x810;
   16 
   17         public Guid dbImportClassesGuid = new Guid("EC120F9A-9E7F-469d-8D61-F4E2A97E5726");
   18         public const int dbImportClassesID = 0x811;
   19 
   20 
   21         protected override IList GetMenuCommands()
   22         {
   23             IList commands = base.GetMenuCommands();
   24 
   25             // You need to create a new command and add it to
   26             // the commands collection.
   27             DynamicStatusMenuCommand cmdImportDbSchema =
   28                     new DynamicStatusMenuCommand(
   29                          new EventHandler(OnPopUpMenuDisplayAction),
   30                          new EventHandler(OnPopUpMenuClick),
   31                          new CommandID(dbImportSchemaGuid, dbImportSchemaID));
   32             commands.Add(cmdImportDbSchema);
   33 
   34             // You need to create a new command and add it to
   35             // the commands collection.
   36             DynamicStatusMenuCommand cmdImportClasses =
   37                     new DynamicStatusMenuCommand(
   38                          new EventHandler(OnPopUpMenuDisplayAction),
   39                          new EventHandler(OnPopUpMenuClick),
   40                          new CommandID(dbImportClassesGuid, dbImportClassesID));
   41 
   42             commands.Add(cmdImportClasses);
   43 
   44             return commands;
   45         }
   46 
   47

   56         internal void OnPopUpMenuDisplayAction(object sender, EventArgs e)
   57         {
   58             MenuCommand command = sender as MenuCommand;
   59 
   60             foreach (object selectedObject in this.CurrentSelection)
   61             {
   62                 if (selectedObject is ClassDiagram)
   63                 {
   64                     // The popmenu command is always visible
   65                     command.Visible = true;
   66                     command.Enabled = true;
   67                     return;
   68                 }
   69             }
   70 
   71             // The popmenu command is always visible
   72             command.Visible = false;
   73             command.Enabled = false;
   74         }
   75 
   76         internal void OnPopUpMenuClick(object sender, EventArgs e)
   77         {
   78             MenuCommand command = sender as MenuCommand;
   79 
   80             StringBuilder sb = new StringBuilder();
   81             foreach (object selectedObject in this.CurrentSelection)
   82             {
   83                 sb.AppendLine("Selected Shape: " + selectedObject.ToString());
   84 
   85                 // the Current selection will hold your "shape" class, so if you are interested
   86                 // in getting the Model Class you need to use the ModelElement Property as shown below.
   87                 if (selectedObject is ClassShape)
   88                 {
   89                     ModelClass modelClass = (ModelClass)(selectedObject as ClassShape).ModelElement;
   90                     sb.AppendLine("*** Related Domain Class: " + modelClass.ToString());
   91                 }
   92 
   93                 if (selectedObject is ClassDiagram)
   94                 {
   95                 }
   96             }
   97 
   98             System.Windows.Forms.MessageBox.Show(sb.ToString());
   99         }
  100 
  101     }
  102 
  103 
  104 }

Step #5 : Transform Templates + Test It

  • Ok, finally, you can now transform templates + run + test it.
  • You should see the following in your diagram:



How to access the Related Domain Object (given a shape)?

  // the Current selection will hold your "shape" class, so if you are interested
  // in getting the Model Class you need to use the ModelElement Property as shown below.
   87                 if (selectedObject is ClassShape)
   88                 {
   89                     ModelClass modelClass = (ModelClass)(selectedObject as ClassShape).ModelElement;
   90                     sb.AppendLine("*** Related Domain Class: " + modelClass.ToString());
   91                 }


How to control visibility of the Command?
Look at lines 56 - 74 from the previous code, it clearly shows how you can based on a condition, enable or disable the command.

Friday, December 21, 2007

NHibernate and WinForms article (1st draft)

As I've mentioned before, I am trying to create a Article for using NHibernate with Windows Forms (WinForms).

Both the article and the example project are not finished (it's work in progress) but I can't wait any longer , and therefore decided to share and get some initial feedback / reactions.

Wednesday, December 19, 2007

Warning : Renaming a DSL --> old name still used in .tt files

If you manually rename the DSL File ( DSLDefinition.dsl ) to something else you will notice that your project will not work anymore giving several weird error messages.

The Solution is to manually also change all the .tt files that are still referencing the old name.

Default Name : DslDefinition.dsl

Search for this name and you will find that all the .tt files are still referencing the old name.

<#@ Dsl processor="DslDirectiveProcessor" requires="fileName='..\DslDefinition.dsl'" #>
<#@ include file="Dsl\ConnectionBuilder.tt" #>

Monday, December 17, 2007

WinForms inheritance workaround

I've started some days ago with a simple mini-guide for creating Windows Forms Applications with NHibernate. It's not finished yet but what's interesting because I wanted to have visual inheritance in my application + using Generics. I initially thought that this should not be a problem but i was wrong.

After adding Generics support to my Forms + creating abstract Form , the Designer stopped working with one of those "ugly design-time errors" , which BTW are very nicely shown in vs.net 2008.
So my conclusion is : neither vs.net 2005 nor vs.net 2008 can easily support Generics + USer Control (or forms) inheritance (they both share this same "bug" / limitation ).

Luckily I found this workaournd from Frank Bakker .I could work-around and create a common/base UserControl for my Business Object UI.

Code Example:

public partial class ProductFormUC : ProductFormUCDummy
{
public ProductFormUC() : base()
{
InitializeComponent();
}

public ProductFormUC(int pProductID) : this()
{
Product product = this.Repository.ProductDAO.Fetch(pProductID);
if (product == null)
throw new Exception("Not Found : was it deleted? improve this case");

SetCurrentBO(product);

categoryBindingSource.DataSource = this.Repository.CategoriesDAO.GetAll();
categoryBindingSource.DataMember = "CategoryName";

productBindingSource.DataSource = product;

}

}

This "intermediate" class makes the trick :
public partial class ProductFormUCDummy : BaseBOFormUC
{
protected override IDAO DAO
{
get { return this.Repository.ProductDAO; }
}

}