DCSIMG
יצירת מקטע מותאם אישית בקובץ הקונפיג - שלמה גולדברג (הרב דוטנט)

שלמה גולדברג (הרב דוטנט)

מרצה בסלע ויועץ בעולם ה - net.

יצירת מקטע מותאם אישית בקובץ הקונפיג

 

בדרך כלל כשאנחנו רוצים לשמור דברים בקובץ הקונפיג, אנחנו משתמשים ב - AppSettings. וכותבים בצורה הבאה:
 

<appSettings>

  <add key="Name" value="Shlomo Goldbedrg"/>

</appSettings>

 
ובקוד אנחנו שולפים את הערך בצורה הבאה:
 

string name = ConfigurationManager.AppSettings["Name"];

 
זה נחמד ומתאים להרבה מקרים, אבל מה קורה כשאנחנו צריכים יותר מ key ו - value, ואנחנו צריכים לשמור מידע על אובייקט שיש לו מאפיינים ?
 
אחד הדרכים הנפוצות היא קובץ xml.
 
לדוגמא - אני כרגע מתעסק עם יצירת AddIn ל - PowerPoint בעזרת VSTO, וכתבתי קוד שניגש לבלוג שלי ומביא את הפוסט האחרון ומייצר שקף.
נניח שאני רוצה לשמור מידע איזה בלוגים לייבא - מה מספר השקף שלתוכו נשפוך את המידע, באיזה interval יש לגשת לבלוג לבדוק האם יש פוסט חדש וכו'.
 
בצורה הפשוטה נכתוב קובץ xml שיראה כך:
 

<Bloggers>

  <Blogs ContentTop="0" Interval="10000">

    <Blog Name="Shlomo Goldberg" SlideIndex="0"

          RssUrl="rss.aspx" TitleDirection="rtl"></Blog>

    <Blog Name="Alon" SlideIndex="1"

          RssUrl="alon/rss.aspx" TitleDirection="ltr"></Blog>

  </Blogs>

</Bloggers>

 
 
מה החיסרון - שעכשיו אני צריך לכתוב קוד שניגש לקובץ xml וקורא את הנתונים.
 
הייתי רוצה לכתוב את ה - xml בקובץ הקונפיג (כמו כל האלמנטים האחרים) ובקוד לגשת בעזרת מחלקות שיווצרו (המופע שלהם) בצורה אוטומטית.
 
 
 
נכתוב את ה - xml שכתבנו מקודם בקובץ הקונפיג. ונשנה את האלמנטים של Blog ל - add (כך)
 
 

<Bloggers>

  <Blogs ContentTop="0" Interval="10000">

    <add Name="Shlomo Goldberg" SlideIndex="0"

          RssUrl="rss.aspx" TitleDirection="rtl"/>

    <add Name="Alon" SlideIndex="1"

          RssUrl="alon/rss.aspx" TitleDirection="ltr"/>

  </Blogs>

</Bloggers>

 

מעליו נכתוב את ההגדרה הבאה:

<configSections>

  <section name="Bloggers" type="RSSBlogs.Bloggers, RSSBlogs"/>

</configSections>

 
(אנחנו מגדירים שמותר לייצר section בקונפיג שייקרא Bloggers וההגדרה של המחלקה שלו יושבת ב - dll שנקרא RssBlogs בתוך namespace בשם RssBlogs במחלקה שנקראת Bloggers.)
 
כעת נייצר את המחלקות.
 
נתחיל באלמנט הפנימי (זה שמייצג מופע של Blog - למעשה זה ה - section של add)
 
אם נסתכל על אחד מהאלמנטים
 

<add Name="Shlomo Goldberg" SlideIndex="0"

      RssUrl="rss.aspx" TitleDirection="rtl"/>

נוכל לראות שיש לנו מופע של מחלקה עם ארבעה מאפיינים.
שם.
מיקום.
כתובת.
ויישור.
 
נייצר את המחלקה הבאה:
 
 

public class BlogElement : ConfigurationElement

{

    [ConfigurationProperty("Name", DefaultValue = "", IsKey = true, IsRequired = true)]

    public string Name

    {

        get

        {

            return ((string)(base["Name"]));

        }

        set

        {

            base["Name"] = value;

        }

    }

 

    [ConfigurationProperty("SlideIndex", DefaultValue = "0", IsKey = false, IsRequired = true)]

    public int SlideIndex

    {

        get

        {

            return (int)(base["SlideIndex"]);

        }

        set

        {

            base["SlideIndex"] = value;

        }

    }

 

    [ConfigurationProperty("RssUrl", DefaultValue = "", IsKey = false, IsRequired = true)]

    public string RssUrl

    {

        get

        {

            return ((string)(base["RssUrl"]));

        }

        set

        {

            base["RssUrl"] = value;

        }

    }

 

    [ConfigurationProperty("TitleDirection", DefaultValue = "rtl", IsKey = false, IsRequired = true)]

    public Direction TitleDirection

    {

        get

        {

            return (Direction)Enum.Parse(typeof(Direction), base["TitleDirection"].ToString());

        }

        set

        {

            base["TitleDirection"] = value;

        }

    }

}

 
המחלקה יורשת מ - ConfigurationElement, מעל כל מאפיין מוגדר מה השם בקובץ הקונפיג שמתייחס למאפיין, האם זה key ומה ערך ברירת המחדל והאם זה מאפיין חובה.
 
כעת נראה את המחלקה שמייצגת את העטיפה של כל ה - Blog
 

<Blogs ContentTop="0" Interval="10000">

 
המחלקה:
 

[ConfigurationCollection(typeof(BlogElement))]

public class BlogCollection : ConfigurationElementCollection

{

    protected override ConfigurationElement CreateNewElement()

    {

        return new BlogElement();

    }

 

    protected override object GetElementKey(ConfigurationElement element)

    {

        return ((BlogElement)(element)).SlideIndex;

    }

 

    [ConfigurationProperty("ContentTop", DefaultValue = "0", IsKey = false, IsRequired = true)]

    public int ContentTop

    {

        get

        {

            return ((int)(base["ContentTop"]));

        }

        set

        {

            base["ContentTop"] = value;

        }

    }

 

    [ConfigurationProperty("Interval", DefaultValue = "0", IsKey = false, IsRequired = true)]

    public int Interval

    {

        get

        {

            return ((int)(base["Interval"]));

        }

        set

        {

            base["Interval"] = value;

        }

    }

}

 
המחלקה יורשת מ - ConfigurationElementCollection מגדירה ב - attribute מי ה - collection שלה (BlogElement)
מגדירה את שני המאפיינים שלה, ודורסת את המתודות של יצירת אלמנט חדש והחזרת ה - key.
 
הדבר האחרון זה להגדיר את המחלקה לעטיפה הכוללת
 

public class Bloggers : ConfigurationSection

{

    [ConfigurationProperty("Blogs")]

    public BlogCollection BlogItems

    {

        get

        {

            return ((BlogCollection)(base["Blogs"]));

        }

    }

}

 
 
כעת בקוד נוכל לכתוב בצורה הבאה.
 

Bloggers section =

       (Bloggers)ConfigurationManager.GetSection("Bloggers");

 

if (section != null)

{

    int i = section.BlogItems.Interval;

    foreach (BlogElement item in section.BlogItems)

    {

        string str = item.Name;

    }

}

 
 
ניתן כמובן להרחיב על הנושא הרבה יותר, אבל זהו הבסיס.
 
 
פורסם: Feb 03 2010, 09:50 PM by Shlomo | with 1 comment(s)
תגים:, ,
שלח תגובה

(שדה חובה)  

(שדה חובה)  

(אופציונלי)

(שדה חובה) 

Please add 8 and 8 and type the answer here:


Enter the numbers above: