开发者

C Sharp/ASP.NET: OOP Good Practice Question

I'm currently in the process of working in a class design for a web application I'm building. I'm relatively 开发者_StackOverflow社区new to OOP (although I have done some). For the most part I'm fairly confident that I know what I'm doing in the paradigm: I know that it's not good practice for one class to access the internal workings of another class, that unsafe static methods are unsafe because they can modify global state, and that generally speaking, the more purely functional and modular I can keep my code the better off I will be.

I'm a little unsure of what to do in this situation though. I have multiple web pages which all have their own GridView control. Some logic will go through each row and change the color of the row based on certain conditions. Would it be considered bad practice, for example, to keep one static class encapsulating these style changes, which will be accessed by every page? Technically this means that this class would be modifying a member of another class. How should I go about this? I would prefer not to duplicate my code through each class, as I try to adhere to the DRY principle as much as possible.

EDIT: Here's what I'm thinking.

public static class RowStyle
{
    public static void SetRed(GridViewRow row)
    {
        row.BackColor = Color.Red;
    }

    // More methods here
}

And each page will pass many GridViewRows to this class, and then have them modified.


Generally, static methods are not so much of a problem if they dont modify static state. Therefore, as long as everything it needs is either encapsulated in the method, or passed in via params then there is less of an issue.

Eg, following your example, the following would IMO be fine

public static class Colorizer
{
    public static void Colorize(GridView gv)
    {
      // do you're funky logic here.
    }
}

And use that method in each of your pages that need the logic.

However, this would be bad bad bad:

public static class Colorizer
{
    private static bool haveIAlreadyColorized = false;
    public static void Colorize(GridView gv)
    {
       if(!haveIAlreadyColorized)
            // do you're funky logic here.
    }
}


I'd recommend creating a custom UserControl containing your GridView and add all associated logic there. That is the best way to do it within the ASP.NET platform.


If same color codes you will follow then, you can follow this:

  1. Create a common class. Make it static.

  2. Create a method as that will be called in the rowdatabound event and based on event you can change color


I think it would be fine to have a static class with a method like this:

public static System.Drawing.Color GetRowColor(GridRow row)
...

If the rules which govern the value of the color are global, then this is proper.


I would encapsulate the style changes in a single place (web.config would be my choice), and create a helper method that takes a GridView instance as parameter. That helper method's sole purpose in life would be read the style info from the config file, to apply it to the passed GridView instance.

Any pages containing a GridView to which those style changes should be applied, would call the helper method.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜