DotNet Web Coding Best Practice

原创 2012年03月22日 22:22:56

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 

用命令行创建.NET Core

PHP和JAVA程序员都说.NET程序员不会使用命令行,听了我就火了发火,直接上手用命令行给这俩货演示了一把创建.NET Core项目的过程,直接惊道这俩货。以下是惊道的过程。在这里我们使用dotne...
  • gangzhucoll
  • gangzhucoll
  • 2016年10月21日 11:40
  • 1150

计算机顶级会议的历年最佳文章 (1996-2013)

Best Paper Awards in Computer Science (since 1996) By Conference:   AAAI   ACL   CHI   CIKM   CVPR ...
  • wanrenwangxuejing
  • wanrenwangxuejing
  • 2014年04月30日 09:08
  • 4451

dotnet core 命令详解

本篇博客来了解一下dotnet这个神奇的命令。我会依次对dotnet,dotnet new,dotnet restore,dotnet build,dotnet test,dotnet run,dot...
  • daguanjia11
  • daguanjia11
  • 2016年09月07日 09:44
  • 5944

Effective Coding with VHDL Principles and Best Practice 无水印原版pdf

  • 2017年12月29日 01:10
  • 14.53MB
  • 下载

Effective.Coding.with.VHDL.Principles.and.Best.Practice

  • 2017年12月08日 12:54
  • 14.53MB
  • 下载

js 递归 best practice

知识点. JS 函数内部有两个特殊对象 arguments 和 this. arguments 对象中有一个属性 callee. 该属性是一个指针,指向拥有这个arguments对象的函数。 ...
  • terry1989
  • terry1989
  • 2012年06月21日 15:17
  • 248

TiDB Best Practice

本文档用于总结在使用 TiDB 时候的一些最佳实践,主要涉及 SQL 使用、OLAP/OLTP 优化技巧,特别是一些 TiDB 专有的优化开关。 建议先阅读讲解 TiDB 原理的三篇文章(讲存储,说...
  • TiDB_PingCAP
  • TiDB_PingCAP
  • 2017年07月05日 18:20
  • 204

HttpClient 4.5 Best Practice

最近在封装http请求相关的library,希望接口的行为和实现分离,这样底层library再切换不同http请求实现 例如:Apache HttpClient、OkHttp、AsyncHttpCli...
  • FX_SKY
  • FX_SKY
  • 2016年08月22日 00:05
  • 1336

Solution Beacon Security Best Practice #6 - Set Application User Signon Profile Values

Your organization likely has IT standards and policies relating to authentication. For instance, you...
  • pan_tian
  • pan_tian
  • 2012年09月06日 11:02
  • 1941

Atitit 数据存储视图的最佳实际best practice attilax总结

Atitit 数据存储视图的最佳实际best practice attilax总结   1.1. 视图优点:可读性的提升1 1.2. 结论  本着可读性优先于性能的原则,面向人类编程优先...
  • attilax
  • attilax
  • 2016年12月23日 16:11
  • 298
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:DotNet Web Coding Best Practice
举报原因:
原因补充:

(最多只允许输入30个字)