DotNet Web Coding Best Practice

Web Coding


1. Page Structure Overview

#region properties
	// private const variable
	// private variable
	// property
#endregion

#region method
#endregion

#region event handler
#endregion

#region utility/commom //canbe here or utility class
	e.g:
	ResetInput() by div
	ErrorHandler()
	OperationResultMessageHandler()
#endregion

2. variable vs property vs method

target: avoid multiple times call database

When every operations(data) can be completed in one request but different method: property + variable

private Customer _cust;
private Customer Cust
{
    get
	{
		if(_cust==null)
		{
			_cust=calldb();
		}
		return _cust;
	}
}

private void SampleMethod()
{
    var c=Cust;
	...
}

private void SampleMethodB()
{
	var c=Cust;
	...
}

When every operations can be completed in one request and same method: property + local variable(in method)

private Customer Cust
{
    get
	{
		return calldb();
		
	}
}

private void SampleMethod()
{
    var c=Cust;
	...
}

When every operations need to completed via multiple request: property + viewstate or session

private Customer Cust
{
    get
	{
		if(Viewstate["cust"]==null)
		{
			Viewstate["cust"]=calldb();
		}
		return Viewstate["cust"];
	}
}

private void SampleMethod()
{
    var c=Cust;
	...
}

When operations need parameters: method

3. Best Practice

3.1 use "?"  for object

public int? SelectedItemValue;

3.2 use foreach

foreach(Customer c in customerList)
{
}

3.3 use using

using(SqlConnection conn =new SqlConnection())
{
}

3.4 Null is different with 0 or ""

3.5 Strategy Design Pattern

Base Class + same workflow sub classes, base class control  work flow + common method

Note: use master page is sub pages have same layout

3.6 User control + Event handler

delegate all user control 's event to parent page event handler to handle,user control is only define the schema to display

Sample:forward user control's "Button1_Click" event to Default (Parent) page

1> UserControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="HelloWorld.ascx.cs"
    Inherits="UsercontrolDelegateEventToParent.HelloWorld" %>
<div>
    <asp:Button ID="Button1" runat="server" Text="Click Me" OnClick="Button1_Click" />
    <p>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></p>
</div>
2> UserControl.ascx.cs

public partial class HelloWorld : System.Web.UI.UserControl
    {
        #region controls
        public Label WelcomeLabel
        {
            get
            {
                return Label1;
            }
            set
            {
                Label1 = value;
            }
        }
        #endregion

        #region event handler
        public delegate void UserControlButton1_Click(object sender, EventArgs e);
        protected event UserControlButton1_Click _btn1Click;
        public event UserControlButton1_Click Btn1Click
        {
            add
            {
                if (this._btn1Click == null)
                    this._btn1Click += value;
            }
            remove
            {
                if (this._btn1Click != null)
                    this._btn1Click -= value;
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this._btn1Click != null)
                this._btn1Click(sender, e);
        }
3. Default.aspx (Parent Page)

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="UsercontrolDelegateEventToParent._Default" %>

<%@ Register Src="~/HelloWorld.ascx" TagName="UserControl" TagPrefix="HW" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>
    <HW:UserControl id="uc1"
       runat="server"
       OnBtn1Click="Button1_Click"
       ></HW:UserControl>
</asp:Content>

4> Default.aspx.cs

public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            uc1.WelcomeLabel.Text = "Hello World!";
        }
    }

3.7 Give each enum item with a specific valueinstead of making it auto increasing,  if some one add one item in between, then the number will screwed, especially you have been refereed by database record

public enum CarOptions
{
    SunRoof = 0x01,
    Spoiler = 0x02,
    FogLights = 0x04,
    TintedWindows = 0x08,
}


3.8 Use partial class in need

e.g: the entity framework generate class, if u want to add some properties for your business need, you can use business class

public partial class MyEntity {
  public String MyCustomProperty {
    get;
    set;
  }
}

4 make User Control as a atomic unit, should never open access to controls which is inside User Control 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dotnet是一个由微软公司开发的、跨平台的开源框架,用于开发各种类型的应用程序。DotnetWeb界面是指使用Dotnet框架开发的面向Web的应用程序的用户界面。 在Dotnet中,我们可以使用多种技术来创建Web界面,其中最常用的是ASP.NETASP.NET是一个开发Web应用程序的框架,它可以与多种编程语言(如C#、VB.NET等)配合使用。ASP.NET提供了丰富的工具和组件,使开发人员能够快速、高效地构建功能强大的Web界面。 使用Dotnet开发Web界面的优势包括: 1. 强大的开发工具和组件库:Dotnet提供了丰富的工具和组件,可以加速开发过程,并提供各种功能和样式的组件供开发人员使用。 2. 跨平台支持:Dotnet框架可以在多个操作系统上运行,包括Windows、Linux和MacOS等,这意味着开发人员可以轻松地将Web应用程序部署到各种平台上。 3. 安全性:Dotnet框架提供了许多安全特性,如认证和授权机制、防止跨站脚本攻击等,可以帮助开发人员构建安全可靠的Web应用程序。 4. 性能优化:Dotnet框架经过了专门的优化,可以提供高性能的Web应用程序,能够处理大量并发请求。 5. 易于维护和扩展:Dotnet框架使用面向对象的编程模型,代码结构清晰,易于理解和维护。同时,Dotnet还提供了一系列的扩展功能,开发人员可以根据需要轻松地扩展应用程序的功能。 总之,Dotnet框架提供了丰富、可靠、高性能的开发工具和组件,使开发人员能够快速构建功能强大、安全可靠的Web界面。无论是企业级应用程序还是个人网站,Dotnet都能够满足各种需求,并提供可维护和可扩展的解决方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值