Saturday, February 26, 2011

Where is IsolatedStorage in Silverlight

Depending on your OS it could be various locations:

OS Location
Windows 7 and Vista %userprofile%\AppData\LocalLow\Microsoft\Silverlight\is
Windows XP %userprofile%\Local Settings\Application Data\Microsoft\Silverlight\is
Mac /Users/<user>/Library/Application/Support/Microsoft/Silverlight/is

An interesting thing that I found out about isolated storage is that there isn’t a separate isolated storage for each browser, which means that users can switch between browsers and still have access to the same data and settings.

Monday, February 21, 2011

Dream of ubiquitous computer coming true

In the fall of 2009, I dreamt of a computer that would be as powerful as current laptops/desktop computers that would be contained completely within the form factor of a mobile phone device. My dream was that this would be the only device that we would need that would have computing power built into it and using it for different scenarios would require us to just dock it into various devices. I just saw an ad for the Motorola Atrix and it reminded of my 2009 post and how much more closer we are to the one device that defines us dream. The original post: http://blog.aggregatedintelligence.com/2009/09/future-of-computers-what-does-iphone.html.

The scenarios that I dreamt the device would support would go something like this:

  • Go to work and dock it (the phone) into your monitor and start working. (Atrix provides this though its laptop dock)
  • Unplug the phone and walk up to your car and plug it into a slot in your car and the car customizes itself to you and displays the route using the GPS built into the phone. (Atrix sort of has this – via a GPS app)
  • Go home and dock the phone into its dock and continue working on your office work. Your TV plays videos from the phone and your audio system plays music from the phone. (Atrix has this)
  • Need extra storage or computing power – use the Cloud. (Windows Phone 7 does this through its live.com service – SkyDrive, etc).
  • Play games on the go or on your TV (once the phone is parked in the home dock) (The Windows Phone 7 does this to the greatest degree of any phone out there and is one of the best features it has via Xbox).

Even cooler with the above scenario – instead of docking your device, all connections will occur wirelessly.

image

imageimageimage

Read the more about the Motorola Atrix: http://www.motorola.com/Consumers/US-EN/Consumer-Product-and-Services/Mobile-Phones/ci.Motorola-ATRIX-US-EN.overview

The Atrix brings me close to my dream device, but its not quite there yet. So I wonder how long it will take to get to my dream device? (Atrix still is not powerful enough and the services all havent come together yet).

Saturday, February 19, 2011

Sunday, February 06, 2011

You can’t control what you can’t measure

From the person who is attributed with the quote “You can’t control what you can’t measure”, comes an article called “Software Engineering: An idea whose time has come and gone”.

Interesting points from the article:

I’m suggesting that first we need to select projects where precise control won’t matter so much. Then we need to reduce our expectations for exactly how much we’re going to be able to control them, no matter how assiduously we apply ourselves to control.

In support of the above, Tom provides the following example:

To understand control’s real role, you need to distinguish between two drastically different kinds of projects:
- Project A will eventually cost about a million dollars and produce value of around $1.1 million.
- Project B will eventually cost about a million dollars and produce value of more than $50 million.
What’s immediately apparent is that control is really important for Project A but almost not at all important for Project B. This leads us to the odd conclusion that strict control is something that matters a lot on relatively useless projects and much less on useful projects. It suggests that the more you focus on control, the more likely you’re working on a project that’s striving to deliver something of relatively minor value.

I really liked his analogy of trying to apply “you cant control what you cant measure” to your teenager’s upbringing.

Most things that really matter—honor, dignity, discipline, personality, grace under pressure, values, ethics, resourcefulness, loyalty, humor, kindness—aren’t measurable. You must steer your child as best you can
without much metric feedback.

Towards the end he writes:

So, how do you manage a project without controlling it? Well, you manage the people and control the time and money. You say to your team leads, for example, “I have a finish date in mind, and I’m not even going to share it with you. When I come in one day and tell you the project will end in one week, you have to be ready to package up and deliver what you’ve got as the final product. Your job is to go about the project incrementally, adding pieces to the whole in the order of their relative value, and doing integration and documentation and acceptance testing incrementally as you go.

Sounds like he thinks going Agile is the answer!

Note: Jeff Atwood had an interesting commentary on the above article and the comments are equally interesting. http://www.codinghorror.com/blog/2009/07/software-engineering-dead.html

Complexity of Software

"The complexity of software is an essential property, not an accidental one.”

Frederick P. Brooks Jr., No Silver Bullet

Saturday, February 05, 2011

Sql Server Exception: Database name is not allowed with a table-valued parameter

While working on persisting some business entities to a SQL Server database using “user-defined table-types” (UDTs), I was getting the following cryptic error.

The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Table-valued parameter 1 ("@MyTable"), row 0, column 0: Data type 0xF3 (user-defined table type) has a non-zero length database name specified.  Database name is not allowed with a table-valued parameter, only schema name and type name are valid.”

In my case, I was using the Enterprise Library to call a stored procedure and was passing a DataTable to the stored procedure. The problem seems to be the fact that EntLib does not handle DataTable parameters that are being passed to a StoredProc that expects a UDT (especially when you use one of the Execute overloads that take an array of objects as its parameters.

Instead, I had to manually create the SqlParameters that needed to be sent to my stored procedure and also set the SqlDataType to Structured for the DataTable that was being passed to the StoreProc that expected a UDT. Once that was done, the above exception went away.

Here is some sample code: (where database is of type EnterpriseLibrary.Data.Database)


            DbCommand dbCommand = database.GetStoredProcCommand(procName);
            try
            {
                dbCommand.Parameters.Add(new SqlParameter(“UDTParam1”, dataTable) { SqlDbType = SqlDbType.Structured});
                DataSet result = database.ExecuteDataSet(dbCommand);
                …..
            }
            catch (SqlException ex)
            {
                …
            }

Thursday, February 03, 2011

Dynamic sorting of lists using Lambda Expressions

Lambda expressions provide a simple method by which you can sort an IEnumerable object by just providing the property on which to sort via a string parameter.

Here is the code:

public static List<T> Sort<T>(IEnumerable<T> list, string sortField, SortDirection sortDirection)
{
var param = Expression.Parameter(typeof(T), string.Empty);

//normally one would use Expression.Property(param, sortField), but that doesnt work
//when working with interfaces where the sortField is defined on a base interface.
//so instead we search for the Property through our own GetProperty method and use it to build the
//Expression property
PropertyInfo propertyInfo = GetProperty(typeof (T), sortField);
var property = Expression.Property(param, propertyInfo);

var lambda = Expression.Lambda<Func<T, object>>(Expression.Convert(property, typeof(object)), param);

var returnList;
if (sortDirection == SortDirection.Ascending)
returnList = list.AsQueryable().OrderBy(lambda).ToList();
else
returnList = list.AsQueryable().OrderByDescending(lambda).ToList();

return returnList;
}

///
// Allows you to get the PropertyInfo for a property defined on the provided type
// Code allows you to find the property even from base interfaces (Type.GetProperty does not return
// properties from base interfaces
// if you wish to look for a specific property defined on a certain interface, then provide the
// propertyName as 'interface.propertyname'
///
private static PropertyInfo GetProperty(Type type, string propertyName)
{
string typeName = string.Empty;
if (propertyName.Contains("."))
{
//name was specified with typename - so pull out the typename
typeName = propertyName.Substring(0, propertyName.IndexOf("."));
propertyName = propertyName.Substring(propertyName.IndexOf(".")+1);
}

PropertyInfo prop = type.GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public);
if (prop == null)
{
var baseTypesAndInterfaces = new List<Type>();
if (type.BaseType != null) baseTypesAndInterfaces.Add(type.BaseType);
baseTypesAndInterfaces.AddRange(type.GetInterfaces());
foreach (Type t in baseTypesAndInterfaces)
{
prop = GetProperty(t, propertyName);
if (prop != null)
{
if (!string.IsNullOrEmpty(typeName) && t.Name != typeName)
continue; //keep looking as the typename was not found
break;
}
}
}
return prop;
}

Some notes about the code above:

  1. To create an property expression using the Expression.Property method worked fine when I was using classes. But the minute I started using interfaces where the property was defined on a base interface, Expression.Property began to fail.
    1. The reason for the failure was that .Net by default does not return properties defined on base interfaces via the call Type.GetProperty. Hence Expression.Property fails too.
  2. Instead, I implemented a custom method that would walk up the entire hierarchy of the interface/class and attempt to find a property with the provided name. The PropertyInfo returned by the method is then used to create the Expression property.

To call the above method:

var sortedList = Sort<MyClassOrInterface>(aListContainingObjectsOfTypeMyClassOrInterface, “APropertyDefinedOnMyInterfaceOrClass”, SortDirection.Ascending);

The above method works well when you need to sort based on a property that is defined by a string. (Works great when you have to work with Asp.Net grids where the sorting field is defined by name).

But if you instead need a strongly typed method of sorting you can use the following code:

public List<T> Sort<T,T1>(IEnumerable<T> list,
Func<T, T1> sorter, SortDirection direction)
{
List<T> returnList = null;
if (direction == SortDirection.Ascending)
returnList = list.OrderBy(sorter).ToList();
else
returnList = list.OrderByDescending(sorter).ToList();
return returnList;
}

And to call the above method use the following code: (Where C is the name of a class/interface and Name is a property defined on that class/interface and cList is a list).

List<C> sorted = Sort<C,string>(cList, c => c.Name, SortDirection.Ascending);