Home > .NET, عام > .NET Best Practice : Enterprise Library , Database Component

.NET Best Practice : Enterprise Library , Database Component

فبراير 2nd, 2009 admin Leave a comment Go to comments

تختلف نوعية البرامج التي نقوم ببرمجتها بمختلف أفكاره وأهدافها ، برامج مرتبطة بالصوت ، بالصورة ، بالرسم ، بالتحليل وانواع وأنواع ومن ضمن تلك البرامج ، البرامج المرتبطة بقواعد البيانات ولأن نوعية البرمجة على قواعد البيانات على رغم بساطتها إلا أنها تدخل في تفاصيل مهمة جدا ، تصبح بصورة أو أخرى مهمة جدا ، خصوصا مع نوعية البرامج .

عادة مايكروسوفت تدعم المشاريع المقدمة من خبراء البرمجة ولعل codeplex.com أكبر دليل على ذلك ..
عموما من ضمن المشاريع المقدمة من هؤلاء الخبراء ، توزيعة Enterprise library وهو توزيعة تجمع أكثر من مكونة منها cache , security , database وخلافه ، المهم بالنسبة لي هنا هو Database Component .

الهدف الحقيقي والفعلي في تقديم Database Component هو عمل مكتبة برمجية من الممكن تحتوي الجميع بأفضل حل وأفضل طريقة أيضا .. وهذا فعلا ما حققته DC ..

الفكرة ، نحن نستخدم sql server ماذا لو استخدمنا غدا Oracle ، MYSQL  ؟ الواجهة الفعلية التي تكتب فيها أكوادك نفسها لن تتغير .. كل ما تحتاجه تغيير في الـ Base ليتعامل مع مختلف أنواع الداتابيس ..

public UserInfo GetUserInfo(string username)
{
       try
       {
           //DataBase Name On Configration File
           Database db = DatabaseFactory.CreateDatabase("DefaultDB");

           UserInfo user = null;
           // usp_GetUserByUsername is SP Name
           using (DbCommand dbSPCommand = db.GetStoredProcCommand("usp_GetUserByUsername"))
           {
               // add in parameter value
               db.AddInParameter(dbSPCommand, "p_username", DbType.String, username);
               // Execute
               IDataReader dr = db.ExecuteReader(dbSPCommand);

               // start reading , because we know the result will be one record. we use if.
               // otherwise use while(dr.Read())
               if (dr.Read())
               {
                    user = new UserInfo();
                    user.Name = (string)dr["USERNAME"];
                    user.DateOfBirth = (DateTime)dr["DATE_OF_BIRTH"];
               }
               return user;
           }
        }
        catch (Exception ex)
        {
            // add the error details on the event viewer
            DALException.Publish(ex);
            // translate the exception base on type of error.
            // here we have to check the type of error to make it more generic for sql server , oracle or mysql
            throw DALException.Translate(ex);
        }
}
Categories: .NET, عام Tags:
  1. No comments yet.
  1. No trackbacks yet.