DCSIMG
Text Resource in asp.net mvc - שלמה גולדברג (הרב דוטנט)

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

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

Text Resource in asp.net mvc

 

 פוסט זה נכתב בעזרתם של תותחי העל נטלי אהפוטה וניב לוי (יהלום של סלע)
 
 
כרגיל בהרבה מקומות בהם אנחנו מפתחים אתרי אינטרנט, רוצים המנהלים שליטה על המחרוזות בלי צורך להזדקק למפתחים, הדרך הרגילה לעבוד עם מחרוזות היא בעזרת קבצי resx, שאיתם כמובן יש בעייה מבחינת העבודה איתם (למי שלא מפתח) - בעבר כתבתי כלי שנותן מענה מסויים, אך בפעם הזאת החליט מי שהחליט שהמחרוזות ישמרו בבסיס נתונים, מה שמביא אותנו לנקודות הבאות:
 
  • המידע בבסיס הנתונים, ויש צורך להגדיר דף בו המנהלים יוכלו לערוך את המחרוזות.
  • שמירת המידע במקום כלשהו בזיכרון, כדי לא לגשת כל הזמן לבסיס הנתונים.
  • אפשרות לגשת למידע מקבצי ה - cshtml.
  • אפשרות לגשת למידע מקבצי java script.

 

המודל נראה כך:
 
Resource Model
 
  • Name - ה - key שדרכו מחפשים את הערך.
  • Comment - טקסט שמסביר למנהלים מה המשמעות של הערך.
  • Description - הערך עצמו.
  • IsUseInJs - האם הערך הזה צריך שתהיה לו גישה גם מצד הלקוח.
כעת הגדרנו משתנה סטטי המחזיר מתוך ה - Application את המידע, במידה והמידע לא קיים טוענים אותו.
 

public static List<Resource> Resources

{

    get

    {

        var list = HttpContext.Current.Application["Resources"];

        if (list == null)

        {

            UserModleEntities context = new UserModleEntities();

            //Detach the context to the object underline - read only with entity frame work;

            context.Resources.MergeOption = MergeOption.NoTracking;

            list = context.Resources.ToList();

            HttpContext.Current.Application["Resources"] = list;

        }

        return list as List<Resource>;

    }

}

 
 
כעת נרצה לאפשר בקלות את הגישה מקבצי ה - cshtml, ולכן כתבנו את הקוד הבא:
 

namespace System.Web.Mvc.Html

{

    public static class HtmlHelpersExtensions

    {

        public static string TextResource(this HtmlHelper helper, string name)

        {

            return ApplicationHelper.Resources.Where(x => x.Name == name).Select(x => x.Description).FirstOrDefault();

        }

    }

}

 
כעת בכל מקום בדף נוכל לכתוב:
 

@Html.TextResource("Welcome")

 
וזה יחזיר את הערך הנכון.
 
 
כפי שהגדרנו, אנחנ רוצים גם גישה מקבצי JS, בעבר פרסמתי משהו דומה עם קבצי resx.
 
נייצר קובץ ashx, ונכתוב את הקוד הבא:
 

public void ProcessRequest(HttpContext context)

{

 

    //Setting cache options

    //TimeSpan -Default cache evrey 60 seconds

    TimeSpan freshness = Properties.Settings.Default.ResourceCacheDif;           

    DateTime now = DateTime.Now;

    context.Response.Cache.SetExpires(now.Add(freshness));

    context.Response.Cache.SetMaxAge(freshness);

    context.Response.Cache.SetCacheability(HttpCacheability.Server);

    context.Response.Cache.SetValidUntilExpires(true);

 

 

    context.Response.ContentType = "application/js";

    //The list of all js resources

    List<Resource> jsListResources = ApplicationHelper.JsResources;

 

    //Constructing the array

    StringBuilder stringBuilder = new StringBuilder();

    stringBuilder.Append("var resourceArray = new Array(); ");

 

    foreach (Resource item in jsListResources)

    {

        stringBuilder.Append(string.Format("resourceArray['{0}'] = '{1}'; ", item.Name, item.Description));

 

    }

 

    context.Response.Write(stringBuilder.ToString());

 

}

 
כשהמאפיין JSResource מחזיר מערך של Resources שהוגדר עבורם UseInJs=true.
 
כעת ב - JS נוכל לכתוב בכל מקום
 

resourceArray.CaratsMandatory

תוכן התגובה

Shlomo כתב/ה:

static, cache, application

לכולם אותו משמעות

אורך חיים כל עוד האפליקציה באויר

השמירה במטמון מאפשרת עוד משחקים, כמו ריענון אוטומטי כשבסיס הנתונים משתנה וכדומה

# May 9, 2012 11:57 AM
שלח תגובה

(שדה חובה)  

(שדה חובה)  

(אופציונלי)

(שדה חובה) 

Please add 8 and 1 and type the answer here:


Enter the numbers above: