NameTransformer help

Jun 2, 2011 at 9:25 PM
Edited Jun 2, 2011 at 9:28 PM

Can someone help me to write regex to cover following cases:

ViewModels.Main.MainPageViewModel

Views.Main.MainPage

Jun 2, 2011 at 9:53 PM

My mistake it's already covered.

Jun 2, 2011 at 10:00 PM

Add these two calls to your bootstrapper:

            ViewLocator.NameTransformer.AddRule
                (
                    @"ViewModels\.(?<namespace>(.*\.)*)(?<basename>[A-Za-z]\w*)(?<suffix>ViewModel$)",
                    @"Views.${namespace}${basename}",
                    @"ViewModels\.(.*\.)*[A-Za-z]\w*ViewModel$"
                );

            ViewModelLocator.NameTransformer.AddRule
            (
                @"Views\.(?<namespace>(.*\.)*)(?<basename>[A-Za-z]\w*)",
                new[] {
                                @"ViewModels.${namespace}${basename}ViewModel",
                                @"ViewModels.${namespace}${basename}",
                                @"ViewModels.${namespace}I${basename}ViewModel",
                                @"ViewModels.${namespace}I${basename}"
                            },
                @"Views\.(.*\.)*[A-Za-z]\w*"
            );
Jun 2, 2011 at 10:04 PM
awattar wrote:

My mistake it's already covered.


No, I think you'll need to add the custom rules in your case.

Jun 2, 2011 at 10:06 PM

Thanks a lot!

Jun 2, 2011 at 10:07 PM
cb55555 wrote:
awattar wrote:

My mistake it's already covered.


No, I think you'll need to add the custom rules in your case.

I'm having two related issues in same time that's why i was thinking it was ok.

Jun 2, 2011 at 10:30 PM

It still cannot find 

Views.Main.InboxView

for

ViewModels.Main.InboxViewModel

Jun 2, 2011 at 10:47 PM
awattar wrote:

It still cannot find 

Views.Main.InboxView

for

ViewModels.Main.InboxViewModel


This is different from your original example. If you need both the original example and this one, you'll need a total of four seperate custom rules in this exact order:

            ViewLocator.NameTransformer.AddRule
            (
                @"ViewModels\.(?<namespace>(.*\.)*)(?<basename>[A-Za-z]\w*)(?<suffix>ViewModel$)",
                @"Views.${namespace}${basename}View",
                @"ViewModels\.(.*\.)*[A-Za-z]\w*ViewModel$"
            );

            ViewLocator.NameTransformer.AddRule
            (
                @"ViewModels\.(?<namespace>(.*\.)*)(?<basename>[A-Za-z]\w*)(?<suffix>PageViewModel$)",
                @"Views.${namespace}${basename}Page",
                @"ViewModels\.(.*\.)*[A-Za-z]\w*PageViewModel$"
            );


            ViewModelLocator.NameTransformer.AddRule
            (
                @"Views\.(?<namespace>(.*\.)*)(?<basename>[A-Za-z]\w*)(?<suffix>View$)",
                new[] {
                                @"ViewModels.${namespace}${basename}ViewModel",
                                @"ViewModels.${namespace}${basename}",
                                @"ViewModels.${namespace}I${basename}ViewModel",
                                @"ViewModels.${namespace}I${basename}"
                            },
                @"Views\.(.*\.)*[A-Za-z]\w*View$"
            );

            ViewModelLocator.NameTransformer.AddRule
            (
                @"Views\.(?<namespace>(.*\.)*)(?<basename>[A-Za-z]\w*)(?<suffix>Page$)",
                new[] {
                                @"ViewModels.${namespace}${basename}${suffix}ViewModel",
                                @"ViewModels.${namespace}${basename}${suffix}",
                                @"ViewModels.${namespace}I${basename}${suffix}ViewModel",
                                @"ViewModels.${namespace}I${basename}${suffix}"
                            },
                @"Views\.(.*\.)*[A-Za-z]\w*Page$"
            );

 

Jun 2, 2011 at 10:53 PM

Sorry it's late and i'm a little bit confused by this new name transformer. In previous version it was working out of the box. Maybe it's good idea to add transforming from View.Sub1.Sub2.SubN.NameView to ViewModels.Sub1.Sub2.Subn.NameViewModel to default rules. In other words if "paths" are identical.

Jun 2, 2011 at 11:07 PM
awattar wrote:

Sorry it's late and i'm a little bit confused by this new name transformer. In previous version it was working out of the box. Maybe it's good idea to add transforming from View.Sub1.Sub2.SubN.NameView to ViewModels.Sub1.Sub2.Subn.NameViewModel to default rules. In other words if "paths" are identical.


The default rules consider namespaces that look like this:

  • Some.Name.Space.ViewModels.NameViewModel
  • Some.Name.Space.Views.NameView

Yours look like this:

  • ViewModels.Some.Name.Space.NameViewModel
  • Views.Some.Name.Space.NameView

The old way used global string replacement, and as a result had a lot of unintended side-effects. That yours worked out of the box before might have been one of these side-effects.

Jun 2, 2011 at 11:19 PM

Damn! I had to do it that way because i had some views with the same name but grouped in bigger structures. Thanks a lot!

Jun 2, 2011 at 11:43 PM
awattar wrote:

Damn! I had to do it that way because i had some views with the same name but grouped in bigger structures. Thanks a lot!


Once you add those 4 custom rules, you should be good to go. These custom rules should even handle namespaces that look like this:

  • Some.Name.Space.ViewModels.Some.Other.Name.Space.NameViewModel
  • Some.Name.Space.Views.Some.Other.Name.Space.NameView

Not that you'd do such a thing. ;)

I admit that it does seem a little overwhelming with so many rules, but it's mainly due to the subtle differences in the current transform conventions for Views that end with "View" vs. those that end with "Page". The simplest transform would be to just tack on "ViewModel" to whatever the View name is. But this would then yield ViewModel names like MainViewViewModel. While this doesn't bother me, it may seem repetitive/redundant to others. On the other hand nobody has a problem with a ViewModel named MainPageViewModel.

Jun 14, 2011 at 3:20 AM

I added a changeset to my latest pull request to handle these cases as built-in rules. You should be able to remove your custom code after my updates get pushed into the trunk. 

Jul 5, 2011 at 5:45 PM

Thank you.