mvc

C# Updating an object with a dictionary<string, string> value

This is more of an exploratory entry than a solution to a problem. Today I wrote this code to handle updating a C# object from a Dictionary. If the object had a key, then it should update its value. I ran into trouble because the object had nullable values, and this tripped the Converter object. I tried a few times, but the clock was ticking away, so I had to find another solution.

At his point I remembered about UpdateModel(). If you are using .NET MVC, you don't need to use this. There is already the handy UpdateModel() function that will do this work for you. So if you have this problem in MVC, use UpdateModel().

At the end of the day, I came back to this problem because I knew that there was a solution, since UpdateModel() does the same function, and I wanted to know how it was done. So I attempted a number of solutions to how to convert a string to a nullable value. At some point I found the solution below in Stack Overflow:

     using System.ComponentModel;
 
    public partial class Memo
    {
        public void Update(Dictionary<string,string> collection)
        {
            var properties = TypeDescriptor.GetProperties(this);
 
                foreach (PropertyDescriptor property in properties)
                {
                    if (collection.Contains(property.Name))
                    {
                        var value = collection[property.Name];
                        property.SetValue(this, property.Converter.ConvertFromInvariantString(value));
                    }
                }
            }
 
        }
    }

The solution was the PropertyDescriptor and its converter. This worked. I should learn more about this object in the future.

Source:

On using PropertyDescriptorCollection
http://stackoverflow.com/questions/446522/how-to-set-nullable-type-via-r...

.Net, Agile: If you want to TDD, you must use MVC

I just started working on a new project. As you know, I got agile religion earlier this month, and I am a born-again coder, who wants to test his code as he goes. Also, after doing a few TDD projects, not doing it seems wrong, and it is just not as fun.

So I got a new .Net project, and this time I actually can do it right. I am going to do it right. I set up Moq and NUnit, and off I go to test land. Now All what I have to figure out is how to mock HttpRequest to test an upload method. I research the question. And in most cases, most of the solutions involved Asp.net mvc. I kept trying to find something specific to just plain asp.net, but the information that I got was either very old, or was a tangential mention that in an mvc page about how the solution could also work with regular asp.net pages.

So the pragmatic solution was to move the back end of the project to mvc. Fortunately for me the project only has a couple of pages of code, and translating the behavior from asp.net to mvc was quick. In fact, it seems that it takes a lot less work code to do the same work.

I am not saying that it is not possible to TDD regular asp.net. I am sure it is. But most of the information and resources exist for asp.net mvc, so, if it is possible, it makes a lot more sense to adopt it if one wants to TDD a asp.net project.

Syndicate content