Nov2008
4

To yield or not yield

by nmgomes

Some time ago, Chris Massey challenged everyone to discuss about four interesting .NET performance questions. One of those questions was "When processing some data into a list, is it faster to build up a collection and return it as an enumerator, or use yield to create the enumerator 'on the fly'?".

Well, I also post a little contribution and surprisingly my post was one of the winners, so I won a free license to ANTS Profiler 4.

Filed in: .NET

Aug2008
12

ASP.NET - EventMappingSettingsCollection bug on Contains method

by nmgomes

Recently, while digging on ASP.NET 2.0 Health Monitoring I found a bug in the EventMappingSettingsCollection.Contains class method.

I was trying to check if an event mapping already exists but every time I try it the following exception was thrown:

System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
  Source="System.Web"
  StackTrace:
       at System.Web.Configuration.EventMappingSettingsCollection.GetElementKey(ConfigurationElement element)
       at System.Configuration.ConfigurationElementCollection.GetElementKeyInternal(ConfigurationElement element)
       at System.Configuration.ConfigurationElementCollection.BaseIndexOf(ConfigurationElement element)
       at System.Web.Configuration.EventMappingSettingsCollection.IndexOf(String name)
       at System.Web.Configuration.EventMappingSettingsCollection.Contains(String name)

I opened a bug in connect, so, if you think this is important go there and vote.

And if you think this one is not a common error ... well ... this is the second bug I found regarding Contains method from a collection class.

Filed in: .NET | ASP.NET

May2008
26

ASP.NET - Dynamic Control Mapping

by nmgomes

I already posted here about Tag Mapping and how helpful it can be, but naturally there's are a few improvements that I would like to see available in future framework release.

The one I expect the most is about the capability of mapping dynamic created controls using the same rules as Tag Mapping uses when interpreting the page markup.

Without this capability we can never use widely the tag mapping because whenever we need to create dynamic controls they will be strongly coupled to a specific control implementation.

Imagine this scenario:

  1. First you have built an web application that use standard ASP.NET TextBox  control, some of them dynamically created.
  2. Now, imagine that you want to reuse that application, as is, but instead of ASP.NET Textbox control you want to use your own Textbox implementation.

This task could be easily accomplished using Tag Mapping if no dynamic controls were used, but in this scenario ASP.NET give us no solution, so the application cannot be reused without modifications.

Naturally, you can copy/paste your application and make the necessary changes, or even add a few if statements, but that will only increase complexity and maintenance effort.

Until the .NET team provide us such capability we must do the magic ourselves.

My proposal is an help class (DynamicControlBuilder) that provide us two methods: GetMappedType and CreateControl.

/// <summary>
/// Gets the mapped <see cref="System.Web.UI.Control"/> type.
/// </summary>
/// <param name="type">The <see cref="System.Web.UI.Control"/> type to be mapped</param>
/// <param name="prefix">The namespace prefix.</param>
/// <returns>A <see cref="System.Type"/> object.</returns>
public static Type GetMappedType(Type type, string prefix)
{
    if (!typeof(Control).IsAssignableFrom(type))
    {
        throw new ArgumentOutOfRangeException("type", "Must inherit from Control.");
    }
    Type mappedtype;
    if (!string.IsNullOrEmpty(prefix))
    {
        TagPrefixInfo prefixinfo;
        if (!m_prefixes.TryGetValue(prefix, out prefixinfo))
        {
            throw new ArgumentException("prefix", "No prefix found.");
        }
        else
        {
            type = BuildManager.GetType(string.Format("{0}.{1}, {2}", prefixinfo.Namespace, type.UnderlyingSystemType.Name, prefixinfo.Assembly), false);
            if (type == null)
            {
                throw new ArgumentException("type", "Control not found within specified prefix.");
            }
        }
    }
    if (m_tagMappings.TryGetValue(type.UnderlyingSystemType, out mappedtype))
    {
        return mappedtype;
    }
    return type;
}

/// <summary>
/// Creates a dynamic mapped <see cref="System.Web.UI.Control"/>.
/// </summary>
/// <param name="type">The <see cref="System.Web.UI.Control"/> type to be mapped</param>
/// <param name="prefix">The namespace prefix.</param>
/// <returns>A <paramref name="T"/> object.</returns>
public static Control CreateControl(Type type, string prefix)
{
    Type mappedType = GetMappedType(type, prefix); ;
    return (Control)Activator.CreateInstance(mappedType);
}

The main goal is to enable any of the following usages:

this.Page.Controls.Add(DynamicControlBuilder.CreateControl<System.Web.UI.WebControls.TextBox>("foo"));

this.Page.Controls.Add(DynamicControlBuilder.CreateControl(typeof(System.Web.UI.WebControls.TextBox), "foo"));

this.Page.Controls.Add(DynamicControlBuilder.CreateControl(typeof(System.Web.UI.WebControls.TextBox)));

Try it !

DynamicControlBuilder.cs (7.13 kb)

kick it on DotNetKicks.com

Filed in: .NET | ASP.NET

May2008
22

BitDiffer - Doing Assembly Differences

by nmgomes

Two weeks ago, Paulo posted about "The Architecture Tool Space Keeps Growing". On this post, Paulo talked about new tools and the continuous improvements in the existing ones.

Well, more recently I read this Scott Hanselman post. Here, Scott talk, apart several other things, about tools for doing Assembly Differences and between them there are new one called BitDiffer.

In the Scott tests, this tool crashed :(

Today, I took some time and install the BitDiffer version 1.3.0.11 so I can explore a little.

BitDiffer 1.3.0.11 

Here are my thoughts:

Advantages

  • Simple but yet complete UI
  • Fast - it took only a 3-5 seconds to compare two versions.
  • Includes a Command Line version

Disadvantages

  • Exports only to HTML / XML format
  • Drag-And-Drop still not working
  • It's not free

Final note

BitDiffer is very similar to Framework Design Studio in functionality but when coming to usability I think it's more intuitive (even without the Drag-And-Drop).

If Gref Ennis add support for Drag-And-Drop and side-by-side comparing code changes, then I will definitely consider to buy it.

Filed in: .NET | Tools

Jan2008
30

ASP.NET Controls - Improving automatic ID generation : Concept ( Part 2)

by nmgomes

Before proceeding to the implementation details, let's discuss a little about how ASP.NET handles automatic Id generation.

First of all, the Control.UniqueID property is the key to all major ASP.NET benefits. In fact, ASP.NET gives us unique Id's for each control which give us a deterministic way of recreating page controls. Without this, Viewstate and Events were impossible to achieve.

ASP.NET also gives us the concept of naming controls, that enable us to easily find the correct control and to write more perceptible code, but More...

Filed in: .NET | ASP.NET

Jan2008
17

.NET Framework 2.0 SP1 - TagMapping Undocumented hotfix

by nmgomes

While searching a way to solve the HtmlControls tag mapping problem I found that the new SP1 bring an extra undocumented hotfix.

This undocumented hotfix changes MainTagNameToTypeMapper.GetControlType method so that, for every tag name regardless is kind, a tag type mapping is always performed.

This little change enables us to tag map HtmlControls the same way we map WebControls and UserControls. Here is an example: More...

Filed in: .NET

Jan2008
5

Email Regular Expression

by nmgomes

From time to time the question strikes back: How to validate an Email?

If you think you know the answer then you must read this Phil Haack post.

Filed in: .NET