一印度学生Asp.net源码分享讨论

 (本文原标题:Asp.net源码程序分析所感——印度,一个不可轻视的近邻!)
    最近在国外Asp.net网站晃悠,搜集到了不少宝贝,觉得不错的就汉化调试一下发到51aspx与大家分享,一般代码美洲和欧洲的朋友发布比较多,这些朋友写的有个特点那就是比较粗狂,用的Asp.net技术比较罕见或者前卫(也许是我掌握的肤浅)。
    一直听说印度的软件业比较发达,而且平民化程度也比较高,我以前其实是不以为然的。直到前天下载了一个叫做Timmy M.John印度大学生朋友写的Asp.net程序才让我改变了这个看法,也深刻体会到了了印度软件业扎实的基础,还是言归正传看看那个代码吧,是采用Asp.net2.0(C#)开发的一个大学课程管理系统,是现在商用程序
http://www.indiastudychannel.com/的一个雏形,主要功能:实现大学课程的搜索,用户注册后可以自行添加课程,可以通过后台管理大学以及所属二级学院等。下面是程序抓图(注:已经liudao汉化调试)


下面是类结构图

程序的功能方面实现起来并不是很轻松,但是这位印度朋友(Timmy M.John)实现起来思路清晰、简单明了,不像好多朋友一样一个简单的程序弄得很复杂,云山雾罩的。
M.John使用的是面向对象开发,我把几个积累代码给大家看看吧,先看一下数据库操作类:

DataManager.cs

namespace IndiaStudyChannel.Utils
{
    /**//// <summary>
    /// Summary description for DataManager.
    /// </summary>
    /// 由 liudao 翻译整理
    /// 该源码下载自www.51aspx.com(51aspx.com)
    public class DataManager
    {
        public DataManager()
        {
        }

        public static DataTable ExecuteQuery(string query)
        {
            string connectionString = System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();
            
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
                DataSet ds = new DataSet();
                adapter.Fill(ds);

                return ds.Tables[0];
            }
            finally
            {
                if ( connection.State == ConnectionState.Open )
                    connection.Close();
            }
        }


        public static void ExecuteNonQuery(string query)
        {
            string connectionString = System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();
            
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd = connection.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = query;

                cmd.ExecuteNonQuery();
            }
            finally
            {
                if ( connection.State == ConnectionState.Open )
                    connection.Close();
            }
        }

        public static object ExecuteScalar(string query)
        {
            string connectionString = System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();
            //www.51aspx.com
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd = connection.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = query;

                return cmd.ExecuteScalar();
            }
            finally
            {
                if ( connection.State == ConnectionState.Open )
                    connection.Close();
            }
        }
    }
}

把常用的sql方法写成一个类,看起来非常清晰,功能简单。大家常用的的SqlHelper类既有存储过程又有sql语句实现的方法,让新手一看就晕(我现在偶尔晕晕)~~

通用函数类(字符串处理等)


namespace IndiaStudyChannel.Utils
{
    /**//// <summary>
    /// Summary description for Utils.
    /// </summary>
    /// 由 liudao 翻译整理
    /// 该源码下载自www.51aspx.com(51aspx.com)
    public class Utils
    {
        public Utils()
        {
        }

        /**//// <summary>
        /// This method removes some dangerous characters from the word to avoid 'Sql Injection attack'.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string MakeSafeWord(string s)
        {
            if ( s == null )
                return "";

            return s.Replace("'", "").Replace("--", "");
        }

        /**//// <summary>
        /// This method checks if the passed user id is an adinistrator or if this is same as current user.
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static bool IsOwner(object userId)
        {
            if ( System.Web.HttpContext.Current.Session["CurrentUser"] == null )
            {
                // There is no userid saved in session. This means user has not logged in.
                return false;
            }

            // Get current user from session.
            string currentUser = System.Web.HttpContext.Current.Session["CurrentUser"].ToString();

            // Get the admin user id from config file.
            string adminUser = System.Configuration.ConfigurationSettings.AppSettings["AdminUser"];

            if ( currentUser == adminUser )
            {
                // Current user is an administrator. Administrator is Owner for all submissions.
                return true;
            }

            if ( userId != null && userId.ToString() == currentUser )
            {
                // Current user is same as the userId passed.
                return true;
            }

            return false;
        }

    
        /**//// <summary>
        /// This method checks if the passed user id is an adinistrator or if this is same as current user.
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static bool IsAdministrator()
        {
            if ( System.Web.HttpContext.Current.Session["CurrentUser"] == null )
            {
                // There is no userid saved in session. This means user has not logged in.
                return false;
            }

            // Get current user from session.
            string currentUser = System.Web.HttpContext.Current.Session["CurrentUser"].ToString();

            // Get the admin user id from config file.
            string adminUser = System.Configuration.ConfigurationSettings.AppSettings["AdminUser"];

            if ( currentUser == adminUser )
            {
                // Current user is an administrator. Administrator is Owner for all submissions.
                return true;
            }

            return false;
        }

        public static string FormatFileName(string s)
        {
            char[] chars = {'#', '@', '?', ':', '\'', '\"', '.', '/', '\\', ' ', '<', '>', '&', '*', '(', ')', '!', ',', ';', ':', '-', '+', '='};
            foreach (char c in chars)
            {
                s = s.Replace(c, '-');
            }
            
            s = s.Replace(" ", "-");
            s = s.Replace("--", "-");

            if ( s.LastIndexOf("-") == (s.Length - 1) && s.Length > 2 )
            {
                s = s.Substring(0, s.Length - 1);
            }

            return s;
        }
    }
}

函数的调用也很简单

        protected void Page_Load(object sender, System.EventArgs e)
        {
            string query = "Select UserId, Name, Email, DateJoined from Members";

            dg.DataSource = Utils.DataManager.ExecuteQuery(query);
            
            dg.DataBind();
        }

    其他优秀的地方都体现在细节方面,比如Tab菜单的切换,验证函数的处理等。

    通过这个程序发现我们在软件方面要向印度方面学习的确实太多太多,我相信我们之间的差距并不是仅仅因为我们的母语不同而造成的,我们的基础软件教育需要反思的太多——为了暂时的小利益而放弃长远利益(特别是某些民间教育机构);软件需求大环境需要反思的太多——有时候是为了编程而写代码。感慨太多,所以把这个源码翻译了一下特分享给大家来研究、借鉴!

    ·去除了部分“印度”字样
    ·使数据库等等支持中文字符(修改排序规则,否则中文会变成问号)
    ·翻译了大部分菜单及控件名称
文中有翻译可笑或者不妥之处还望大家批评指正!(liudao)

该项目的完整源码下载地址>>

译者补注:该源码适合初学者,高手勿下!一个菜鸟认为“优秀”的代码可能也不足以说明一个国家软件业的如何如何,但是我们永远抱着一个学习的虚心态度去对待可能对于我们这个年代的年轻人没有什么坏处的!
   没想到文章发完以后引各位朋友这么热心的关注,总结一下:
 ·该代码对于高手来说确实是没有什么“优秀”可研,但是思路清晰,使新手容易上手,不云山雾罩
 ·大家要抱着初学者的心态来看待这个源码,过来人想想自己当初走的路,能分享一下经验最好
 ·高手应该在这里引领新手,应该指出新手的不足并提出合理的见解,不是指指点点(这些体现不出你的“高”)
 ·这里提到的印度也许是一个理想的不存在的国度——一个需要我们去实现的良好软件大环境


也许是本人太菜,也许是本人目光短浅,但一个不容置疑的事实——

印度!一个不可轻视的近邻!
posted on 2007-07-10 09:13  念时  阅读(497)  评论(0编辑  收藏  举报

细节决定成败!态度决定一切!