locked
Best practices in creating a Web Application Project - ASP.NET using C# RRS feed

  • Question

  • User2015884762 posted

    Hi all

    It's my first time working on a large web application project and being a beginner, I would like to know what are the best practices in creating a web application project, perhaps to narrow it down, I would like to know how do we organize the individual WebForm.aspx pages. 

    Say, for example, the code behind file for WebForm1. aspx is just too lengthy, how should I divide them into separate class files. For now, I just create a new class file since the codes are growing, but on what basis should I separate the codes. 

    How should I organize my event handlers in the code behind if there are just too many controls on one page.

    Please advice.

    Wednesday, January 30, 2019 8:42 AM

Answers

  • User-943250815 posted

    Anything that can be reused is a candidate to become a class, sub or function, and you can organize your files by function or subject.
    On code behind, in addition to Controls Event Handlers, you can also have classes, subs and functions, and how to organize here depends on you.
    Personally, I write using old Pascal concept, which is a kind of "First define your variables and functions, then write your code"

    Of course during development there will be lots of write and rewrite because you will discover that something new can be reused

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 30, 2019 8:33 PM
  • User1120430333 posted

    https://en.wikipedia.org/wiki/Separation_of_concerns

    MVP for Web forms and speration of duty.

    https://www.codeproject.com/Articles/228214/Understanding-Basics-of-UI-Design-Pattern-MVC-MVP

    http://polymorphicpodcast.com/shows/mv-patterns/

    https://www.c-sharpcorner.com/blogs/understanding-interfaces-via-loose-coupling-and-tight-coupling

    Layered or N-tier...

    https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ee658117(v=pandp.10)

    https://docs.microsoft.com/en-us/visualstudio/data-tools/n-tier-data-applications-overview?view=vs-2017

    https://msdn.microsoft.com/en-us/magazine/mt703433.aspx

    <copied>

    When looking at code to evaluate its coupling, remember the phrase “new is glue.” That is, anywhere you see the “new” keyword instantiating a class, realize you’re gluing your implementation to that specific implementation code. The Dependency Inversion Principle (bit.ly/DI-Principle) states: “Abstractions should not depend on details; details should depend on abstractions.

    <end>

    https://dzone.com/articles/absolute-beginners-tutorial

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 30, 2019 10:23 PM
  • User-2054057000 posted

    I would say that since you are using web forms then you simply use Three Layer Architecture - Link

    You can also use MVC architecture in Web Forms. You can take some knowledge on this topic from here.

    Your questions and My answer:

    1. The code behind file for WebForm1. aspx is just too lengthy, how should I divide them into separate class files - If you have big code that are long and you need them in multiple pages then you can add a new static class and add a new function inside this class. In side this function you add these codes.
    2. How should I organize my event handlers in the code behind if there are just too many controls on one page - event handlers will remain on the .aspx.cs page. You can place there codes inside some other class methods and use them from there.

    In one of my previous project i created a static class that contains some normally used functions throughout the site. The code of this class is given below:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Text.RegularExpressions;
    
    /// <summary>
    /// Summary description for CommonFunction
    /// </summary>
    public static class CommonFunction
    {
        public static string ProductUrl(string title, long id)
        {
            string output = "";
    
            //output = Regex.Replace(title, @"([%,;:/&-'""]+)+", "").Replace(" ", "-") + "-id-" + id;
            output = Regex.Replace(Regex.Replace(title, @"([*.%,;:/&'()""]+)+", "").Replace(" ", "-") + "-id-" + id, "[-]{2,}", "-");
            return output;
        }
    
        public static string WebsiteUrl(string title, long id)
        {
            string output = "";
    
            output = Regex.Replace(Regex.Replace(title, @"([*.%,;:/&'()""]+)+", "").Replace(" ", "-") + "-wid-" + id, "[-]{2,}", "-");
            return output;
        }
        public static string RemoveMainDirectoryName(string value)
        {
            return value.Replace("myotcworld/", "");
        }
        public static string Truncate(string input, int characterLimit)
        {
            string output = input;
            if (output.Length > characterLimit && characterLimit > 0)
            {
                output = output.Substring(0, characterLimit);
                if (input.Substring(output.Length, 1) != " ")
                {
                    int LastSpace = output.LastIndexOf(" ");
                    if (LastSpace != -1)
                    {
                        output = output.Substring(0, LastSpace);
                    }
                }
            }
            return output;
        }
        public static string WordBreaker(string strWord, int breakingLength)
        {
            int len = strWord.Length;
            if (len < breakingLength)
            {
                return strWord;
            }
            string output = string.Empty;
            for (int count = 0; count < len; count += breakingLength)
            {
                // if(len/charCount+50)
                int rem = len / (count + breakingLength);
                if (rem != 0)
                {
                    output += strWord.Substring(count, breakingLength) + "<wbr>";
                }
                else
                {
                    output += strWord.Substring(count, (len - count)) + " <wbr> ";
                }
            }
            return output;
        }
        public static string Set_Paging(Int32 PageNumber, int PageSize, Int64 TotalRecords, string ClassName, string PageUrl, string DisableClassName, int cid)
        {
            string ReturnValue = "";
            try
            {
                Int64 TotalPages = Convert.ToInt64(Math.Ceiling((double)TotalRecords / PageSize));
                if (PageNumber > 1)
                {
                    if (PageNumber == 2)
                        ReturnValue = ReturnValue + "<a href='" + PageUrl.Trim() + "?pn=" + Convert.ToString(PageNumber - 1) + "' class='" + ClassName + "'>Previous</a>&nbsp;&nbsp;&nbsp;";
                    else
                    {
                        ReturnValue = ReturnValue + "<a href='" + PageUrl.Trim();
                        if (PageUrl.Contains("?"))
                            ReturnValue = ReturnValue + "&";
                        else
                            ReturnValue = ReturnValue + "?";
                        ReturnValue = ReturnValue + "pn=" + Convert.ToString(PageNumber - 1) + "' class='" + ClassName + "'>Previous</a>&nbsp;&nbsp;&nbsp;";
                    }
                }
                else
                    ReturnValue = ReturnValue + "<span class='" + DisableClassName + "'>Previous</span>&nbsp;&nbsp;&nbsp;";
                if ((PageNumber - 3) > 1)
                    ReturnValue = ReturnValue + "<a href='" + PageUrl.Trim() + "' class='" + ClassName + "'>1</a>&nbsp;.....&nbsp;|&nbsp;";
                for (int i = PageNumber - 3; i <= PageNumber; i++)
                    if (i >= 1)
                    {
                        if (PageNumber != i)
                        {
                            ReturnValue = ReturnValue + "<a href='" + PageUrl.Trim();
                            if (PageUrl.Contains("?"))
                                ReturnValue = ReturnValue + "&";
                            else
                                ReturnValue = ReturnValue + "?";
                            ReturnValue = ReturnValue + "pn=" + i.ToString() + "' class='" + ClassName + "'>" + i.ToString() + "</a>&nbsp;|&nbsp;";
                        }
                        else
                        {
                            ReturnValue = ReturnValue + "<span style='font-weight:bold;'>" + i + "</span>&nbsp;|&nbsp;";
                        }
                    }
                for (int i = PageNumber + 1; i <= PageNumber + 3; i++)
                    if (i <= TotalPages)
                    {
                        if (PageNumber != i)
                        {
                            ReturnValue = ReturnValue + "<a href='" + PageUrl.Trim();
                            if (PageUrl.Contains("?"))
                                ReturnValue = ReturnValue + "&";
                            else
                                ReturnValue = ReturnValue + "?";
                            ReturnValue = ReturnValue + "pn=" + i.ToString() + "' class='" + ClassName + "'>" + i.ToString() + "</a>&nbsp;|&nbsp;";
                        }
                        else
                        {
                            ReturnValue = ReturnValue + "<span style='font-weight:bold;'>" + i + "</span>&nbsp;|&nbsp;";
                        }
                    }
                if ((PageNumber + 3) < TotalPages)
                {
                    ReturnValue = ReturnValue + ".....&nbsp;<a href='" + PageUrl.Trim();
                    if (PageUrl.Contains("?"))
                        ReturnValue = ReturnValue + "&";
                    else
                        ReturnValue = ReturnValue + "?";
                    ReturnValue = ReturnValue + "pn=" + TotalPages.ToString() + "' class='" + ClassName + "'>" + TotalPages.ToString() + "</a>";
                }
                if (PageNumber < TotalPages)
                {
                    ReturnValue = ReturnValue + "&nbsp;&nbsp;&nbsp;<a href='" + PageUrl.Trim();
                    if (PageUrl.Contains("?"))
                        ReturnValue = ReturnValue + "&";
                    else
                        ReturnValue = ReturnValue + "?";
                    ReturnValue = ReturnValue + "pn=" + Convert.ToString(PageNumber + 1) + "' class='" + ClassName + "'>Next</a>";
                }
                else
                    ReturnValue = ReturnValue + "&nbsp;&nbsp;&nbsp;<span class='" + DisableClassName + "'>Next</span>";
            }
            catch (Exception ex)
            {
            }
            return (ReturnValue);
        }
    }
    

    So in my .aspx.cs page I am able to call the Set_Paging() function like this:

    string paging=CommonFunction.Set_Paging(1, 10, 100, "green", "index.aspx", "disable", 5);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 31, 2019 10:16 AM

All replies

  • User-943250815 posted

    Anything that can be reused is a candidate to become a class, sub or function, and you can organize your files by function or subject.
    On code behind, in addition to Controls Event Handlers, you can also have classes, subs and functions, and how to organize here depends on you.
    Personally, I write using old Pascal concept, which is a kind of "First define your variables and functions, then write your code"

    Of course during development there will be lots of write and rewrite because you will discover that something new can be reused

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 30, 2019 8:33 PM
  • User1120430333 posted

    https://en.wikipedia.org/wiki/Separation_of_concerns

    MVP for Web forms and speration of duty.

    https://www.codeproject.com/Articles/228214/Understanding-Basics-of-UI-Design-Pattern-MVC-MVP

    http://polymorphicpodcast.com/shows/mv-patterns/

    https://www.c-sharpcorner.com/blogs/understanding-interfaces-via-loose-coupling-and-tight-coupling

    Layered or N-tier...

    https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ee658117(v=pandp.10)

    https://docs.microsoft.com/en-us/visualstudio/data-tools/n-tier-data-applications-overview?view=vs-2017

    https://msdn.microsoft.com/en-us/magazine/mt703433.aspx

    <copied>

    When looking at code to evaluate its coupling, remember the phrase “new is glue.” That is, anywhere you see the “new” keyword instantiating a class, realize you’re gluing your implementation to that specific implementation code. The Dependency Inversion Principle (bit.ly/DI-Principle) states: “Abstractions should not depend on details; details should depend on abstractions.

    <end>

    https://dzone.com/articles/absolute-beginners-tutorial

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 30, 2019 10:23 PM
  • User-1174608757 posted

    Hi  callykalpana,

    According to your description, to build a web application. you could follow  advice as below:

    1.Design first, write second. It's important to find what is needed in your project and it will basically reduce code complexity.

    2.Ensure the code which divided into class has independent features.We often put  the code  we often use in separate class.It will reduce  code redundancy, but you should make sure that each has respective function.

    3.For functions which we often use, we could put it into an abstract class or interface, then we could Inherit it in code behind, it could greatly enhanced code reuse.

    4..For too many controls on page , I suggest you to use User Control in asp.net. If there are a lot of  controls which is same and  used for many times on the page, it's a good choice to use user control.

    Best Regards

    Wei Zhang

    Thursday, January 31, 2019 7:39 AM
  • User-2054057000 posted

    I would say that since you are using web forms then you simply use Three Layer Architecture - Link

    You can also use MVC architecture in Web Forms. You can take some knowledge on this topic from here.

    Your questions and My answer:

    1. The code behind file for WebForm1. aspx is just too lengthy, how should I divide them into separate class files - If you have big code that are long and you need them in multiple pages then you can add a new static class and add a new function inside this class. In side this function you add these codes.
    2. How should I organize my event handlers in the code behind if there are just too many controls on one page - event handlers will remain on the .aspx.cs page. You can place there codes inside some other class methods and use them from there.

    In one of my previous project i created a static class that contains some normally used functions throughout the site. The code of this class is given below:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Text.RegularExpressions;
    
    /// <summary>
    /// Summary description for CommonFunction
    /// </summary>
    public static class CommonFunction
    {
        public static string ProductUrl(string title, long id)
        {
            string output = "";
    
            //output = Regex.Replace(title, @"([%,;:/&-'""]+)+", "").Replace(" ", "-") + "-id-" + id;
            output = Regex.Replace(Regex.Replace(title, @"([*.%,;:/&'()""]+)+", "").Replace(" ", "-") + "-id-" + id, "[-]{2,}", "-");
            return output;
        }
    
        public static string WebsiteUrl(string title, long id)
        {
            string output = "";
    
            output = Regex.Replace(Regex.Replace(title, @"([*.%,;:/&'()""]+)+", "").Replace(" ", "-") + "-wid-" + id, "[-]{2,}", "-");
            return output;
        }
        public static string RemoveMainDirectoryName(string value)
        {
            return value.Replace("myotcworld/", "");
        }
        public static string Truncate(string input, int characterLimit)
        {
            string output = input;
            if (output.Length > characterLimit && characterLimit > 0)
            {
                output = output.Substring(0, characterLimit);
                if (input.Substring(output.Length, 1) != " ")
                {
                    int LastSpace = output.LastIndexOf(" ");
                    if (LastSpace != -1)
                    {
                        output = output.Substring(0, LastSpace);
                    }
                }
            }
            return output;
        }
        public static string WordBreaker(string strWord, int breakingLength)
        {
            int len = strWord.Length;
            if (len < breakingLength)
            {
                return strWord;
            }
            string output = string.Empty;
            for (int count = 0; count < len; count += breakingLength)
            {
                // if(len/charCount+50)
                int rem = len / (count + breakingLength);
                if (rem != 0)
                {
                    output += strWord.Substring(count, breakingLength) + "<wbr>";
                }
                else
                {
                    output += strWord.Substring(count, (len - count)) + " <wbr> ";
                }
            }
            return output;
        }
        public static string Set_Paging(Int32 PageNumber, int PageSize, Int64 TotalRecords, string ClassName, string PageUrl, string DisableClassName, int cid)
        {
            string ReturnValue = "";
            try
            {
                Int64 TotalPages = Convert.ToInt64(Math.Ceiling((double)TotalRecords / PageSize));
                if (PageNumber > 1)
                {
                    if (PageNumber == 2)
                        ReturnValue = ReturnValue + "<a href='" + PageUrl.Trim() + "?pn=" + Convert.ToString(PageNumber - 1) + "' class='" + ClassName + "'>Previous</a>&nbsp;&nbsp;&nbsp;";
                    else
                    {
                        ReturnValue = ReturnValue + "<a href='" + PageUrl.Trim();
                        if (PageUrl.Contains("?"))
                            ReturnValue = ReturnValue + "&";
                        else
                            ReturnValue = ReturnValue + "?";
                        ReturnValue = ReturnValue + "pn=" + Convert.ToString(PageNumber - 1) + "' class='" + ClassName + "'>Previous</a>&nbsp;&nbsp;&nbsp;";
                    }
                }
                else
                    ReturnValue = ReturnValue + "<span class='" + DisableClassName + "'>Previous</span>&nbsp;&nbsp;&nbsp;";
                if ((PageNumber - 3) > 1)
                    ReturnValue = ReturnValue + "<a href='" + PageUrl.Trim() + "' class='" + ClassName + "'>1</a>&nbsp;.....&nbsp;|&nbsp;";
                for (int i = PageNumber - 3; i <= PageNumber; i++)
                    if (i >= 1)
                    {
                        if (PageNumber != i)
                        {
                            ReturnValue = ReturnValue + "<a href='" + PageUrl.Trim();
                            if (PageUrl.Contains("?"))
                                ReturnValue = ReturnValue + "&";
                            else
                                ReturnValue = ReturnValue + "?";
                            ReturnValue = ReturnValue + "pn=" + i.ToString() + "' class='" + ClassName + "'>" + i.ToString() + "</a>&nbsp;|&nbsp;";
                        }
                        else
                        {
                            ReturnValue = ReturnValue + "<span style='font-weight:bold;'>" + i + "</span>&nbsp;|&nbsp;";
                        }
                    }
                for (int i = PageNumber + 1; i <= PageNumber + 3; i++)
                    if (i <= TotalPages)
                    {
                        if (PageNumber != i)
                        {
                            ReturnValue = ReturnValue + "<a href='" + PageUrl.Trim();
                            if (PageUrl.Contains("?"))
                                ReturnValue = ReturnValue + "&";
                            else
                                ReturnValue = ReturnValue + "?";
                            ReturnValue = ReturnValue + "pn=" + i.ToString() + "' class='" + ClassName + "'>" + i.ToString() + "</a>&nbsp;|&nbsp;";
                        }
                        else
                        {
                            ReturnValue = ReturnValue + "<span style='font-weight:bold;'>" + i + "</span>&nbsp;|&nbsp;";
                        }
                    }
                if ((PageNumber + 3) < TotalPages)
                {
                    ReturnValue = ReturnValue + ".....&nbsp;<a href='" + PageUrl.Trim();
                    if (PageUrl.Contains("?"))
                        ReturnValue = ReturnValue + "&";
                    else
                        ReturnValue = ReturnValue + "?";
                    ReturnValue = ReturnValue + "pn=" + TotalPages.ToString() + "' class='" + ClassName + "'>" + TotalPages.ToString() + "</a>";
                }
                if (PageNumber < TotalPages)
                {
                    ReturnValue = ReturnValue + "&nbsp;&nbsp;&nbsp;<a href='" + PageUrl.Trim();
                    if (PageUrl.Contains("?"))
                        ReturnValue = ReturnValue + "&";
                    else
                        ReturnValue = ReturnValue + "?";
                    ReturnValue = ReturnValue + "pn=" + Convert.ToString(PageNumber + 1) + "' class='" + ClassName + "'>Next</a>";
                }
                else
                    ReturnValue = ReturnValue + "&nbsp;&nbsp;&nbsp;<span class='" + DisableClassName + "'>Next</span>";
            }
            catch (Exception ex)
            {
            }
            return (ReturnValue);
        }
    }
    

    So in my .aspx.cs page I am able to call the Set_Paging() function like this:

    string paging=CommonFunction.Set_Paging(1, 10, 100, "green", "index.aspx", "disable", 5);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 31, 2019 10:16 AM