开发者

How best to represent form fields in C#

I'm s开发者_StackOverflow中文版tarting to learn C# for WP7, I'm making an app to scrape various sites and display a couple of items of info off the page.

I'm going to allow the user to create multiple "accounts" on the phone - each is a set of login details for a particular site. For example, if I was to use stackoverflow as an example, I'd have a class:

public abstract class MyBaseClass
{
    public Dictionary<string, string> fields;
}

public class StackOverflow : MyBaseClass 
{
   public StackOverflow()
   {
       fields.Add("Username", "Default Username");
       fields.Add("Password", "");
   }
}

The class will do all the work, I want people to be able to submit new classes for inclusion in later releases.

The application will iterate over each of the fields, displaying the appropriate form field to the user. Once completed, the UI will update the dictionary, ready to start scraping.

Using a dictionary seemed ok to start, but I hadn't thought about how to represent the data type - I want to define whether the input should be text, number, or password.

How would I best include that data?


Given that screen-scraping is bad at best and disasterous at worst, I must recommend that you only include sites (or serviecs) that provide a public documented API and then use that reference to define the data types that you are storing.

That said, if you're on C#/.NET 4 you might want to use the Tuple data type.


One thing to keep in mind with this pattern is that pretty much everything you do with this object is based on a "magic string". String data has very little that can be checked at compile-time; a field name referenced as a string may have a spelling mistake that would be caught immediately by the compiler; similarly, setting "ABC123" as a field that was supposed to be a decimal is just fine 'til you try and parse it out.

If you're determined to go down this path, I would change the Dictionary to a List of a custom class that contained all the metadata of the field; its name, its type, current value, etc. The list should be protected, and accessed via one or more indexers on the class that derives from your MyBaseClass class, which can pull data based on an index position OR by field name, as the situation may call for.

Be aware that in changing to a List, the naive implementation would result in a linear search time for a field, while a Dictionary gives you logarithmic access time. You can mitigate this by sorting the list and implementing a BinarySearch to find field names.


You are going to have to create your own model to represent this. Just do what comes naturally and makes sense. How about this:

public enum InputType
{
    Text,
    Number,
    Password
}

public class Value
{
    public object Value { get; set;}
    public InputType InputType { get; set;}
}

public abstract class MyBaseClass
{
    public Dictionary<string, Value> fields;
}

public class StackOverflow : MyBaseClass 
{
   public StackOverflow()
   {
       fields.Add("Username", new Value() {
           Value = "Default Username",
           InputType = InputType.Text
       });
       fields.Add("Password", new Value() {
           Value = "hw78fksm9",
           InputType = InputType.Password
       });
   }
}

By the way, don't make the fields variable of MyBaseClass public, this is an implementation detail and should be hidden / encapsulated!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜