Yet Another Join Method

Posted by Ryan Baxter Fri, 12 Sep 2008 21:27:00 GMT

The .NET String type has a Join method, but in my latest ASP.NET project I had the need for joining String array elements with additional prefix and suffix values. The method below delimits array elements with the provided separator and concatenates the elements with the prefix and suffix string parameters.

I’ve found this particularly handy when creating SQL statements that require the IN keyword.

string[] names = { "Bobby", "Suzy" };

sql += "WHERE People.FirstName IN (" + Utility.Join(",", names, "'", "'") + ")";

Add this method to your utility class or extended String type.

public static string Join(string separator, string[] value, string
prefix, string suffix)
{
    string toReturn = String.Empty;

    int i;
    for (i = 0; i < value.Length; i++)
    {
        if (i != value.Length - 1)
            toReturn += prefix + value[i] + suffix + separator;
        else
            toReturn += prefix + value[i] + suffix;
    }

    return toReturn;
}

Using LoginView on HyperLinkFields within a GridView

Posted by Ryan Baxter Wed, 13 Aug 2008 19:45:00 GMT

The title of this post is a bit misleading. Using a LoginView to manage the security of HyperLinkFields within a GridView does not work. There are, however, other means to achieve the same result.

Using the GridView’s OnRowDataBound event, I’ve set my Cells containing HyperLinkFields, on DataRows with a RowIndex of -1, to an empty string if the user does not belong to the “Administrators” role. This hides my HyperLinkFields from underprivileged users and prevents elements of my GridView’s header and footer from not appearing.

protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowIndex != -1)
    {
        if (!Page.User.IsInRole("Administrators"))        
            e.Row.Cells[0].Text = String.Empty;
    }           
}

*Update

I just found a thread on the ASP.NET forums that covers the same problem. In their solution, the GridView’s RowType is checked before setting the Cell’s Visibility property to false. This makes more sense than relying on the RowIndex property to determine whether or not a DataRow’s Cell should be hidden. In the method below, I’ve integrated the DataControlRowType enumeration as suggested by the ASP.NET forums. Since setting the Visibility property of Cells containing HyperLinkFields caused my GridView headings to not line up properly, I decided to assign the Cell’s Text property to String.Empty as in my previous example.

protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{                  
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (!Page.User.IsInRole("Administrators"))
            e.Row.Cells[0].Text = String.Empty;
    }   
}

Removing Duplicate Items from an Abstract Generic List

Posted by Ryan Baxter Thu, 31 Jul 2008 14:40:00 GMT

I’ve got some explaining to do. I was hesitant in posting this code for fear that it might be too niche to benefit anyone. It may be, but the underlying problem affects many programmers working in the IT industry. What do you do when the company legacy system’s data model doesn’t work with your fancy ORM (Object-relational mapping)?

“Any problem in computer science can be solved with another layer of indirection. But that usually will create another problem.” - David Wheeler

At my last job, the ERP (Enterprise resource planning) system was something of mystery and voodoo. Only a few had sufficient knowledge to work with it and because of its arcane nature it was deemed untouchable. Within my first months as an employee I wrote a rudimentary ORM to serve as a layer between our client applications and the ERP system. Its performance was terrible. I wrote it off as “experience” and the department ultimately decided to ignore interfacing directly with the ERP system.

I’m using the SubSonic ORM on a few ASP.NET projects with my current employer. SubSonic has worked great, but a few of its database requirements have left me in the dust with yet another ERP system. The ERP’s tables have no primary keys, constraints, or relationships, but rather than write a complete ORM I decided to roll my own data layer.

All of the strongly-typed collections in the ERP’s data layer implement the AbstractList type. AbstractList implements List where ItemType implements IUniqueIdentifier. That is a mouthful, but the key (pun intended) to removing duplicate items is to make sure they’re unique.

The IUniqueIdentifier interface contains only one property, UniqueIdentifier.

public interface IUniqueIdentifier
{
    string UniqueIdentifier
    {
        get;
        set;
    }
}

If the ERP system contained a table called Customers a Customer type implementing IUniqueIdentifier would be created.

public class Customer : IUniqueIdentifier
{
    private string uniqueIdentifier;

    // etc...

    public UniqueIdentifier
    {
        get { return this.uniqueIdentifier; }
        set { this.uniqueIdentifier = value; }
    }
}

I’d also have a CustomerCollection class:

public class CustomerCollection : AbstractList<Customer, CustomerCollection>
{
    // etc...
}

After such a long-winded introduction I can feel better about dumping the following code on anyone that has happened to read this far (kudos to you).

public abstract class AbstractList<ItemType, ListType> :
List<ItemType> where ItemType:IUniqueIdentifier where
ListType:AbstractList<ItemType, ListType>, new()
{
    public ListType RemoveDuplicates()
    {
        Dictionary<string, int> uniqueStore = new Dictionary<string, int>();
        ListType list = new ListType();

        foreach (ItemType item in this)
        {
            if (!uniqueStore.ContainsKey(item.UniqueIdentifier))
            {
                uniqueStore.Add(item.UniqueIdentifier, 0);
                list.Add(item);
            }
        }

        return list;
    }
}

I’d love to hear how others have worked around legacy systems and still kept their code clean.

Older posts: 1 ... 3 4 5 6 7 ... 9