Theory
Settings ક્યાં રાખવી?
FestConnect ને database નો connection string જોઈએ છે, અને કદાચ બીજી થોડી settings પણ: admin નું email, page ની માપ, site maintenance mode માં છે કે નહીં. આ બધું ક્યાં રહેવું જોઈએ? તમારા compile થયેલા C# ની અંદર દટાયેલું તો નહીં જ, કારણ કે પછી database server બદલવો હોય તો code બદલવો પડે અને આખી app ફરી build કરવી પડે.
ASP.NET નો જવાબ છે web.config: application માટેની એક જ settings file. આ પાઠમાં જોઈશું કે એમાં શું હોય છે અને configuration ને code થી બહાર રાખવી એ આટલી સારી ટેવ શા માટે છે.
Theory
web.config એટલે શું
web.config એ XML file છે જે તમારી ASP.NET application ને configure કરે છે. એ application ના root folder માં બેસે છે (અને કોઈ પણ subfolder પોતાની file રાખીને એ વિસ્તાર માટેની settings બદલી શકે છે).
એમાં સામાન્ય રીતે જે હોય છે તે: connectionStrings (database સુધી પહોંચવાની રીત), appSettings (તમારી પોતાની key-value settings), authentication અને authorization ના નિયમો (કોણ શું વાપરી શકે), custom error pages, અને session ની settings. App કઈ રીતે configure થઈ છે એ જોવા માટેની આ એક જ જગ્યા છે. સૌથી અગત્યનું, ASP.NET એને runtime વખતે વાંચે છે, એટલે setting બદલવા માટે code ફરી compile કરવો પડતો નથી.
Practical
FestConnect ના web.config નો એક ટુકડો
<configuration>
<connectionStrings>
<add name="FestDb"
connectionString="Server=.;Database=FestConnect;Trusted_Connection=True;" />
</connectionStrings>
<appSettings>
<add key="AdminEmail" value="admin@festconnect.example" />
<add key="EventsPerPage" value="10" />
</appSettings>
</configuration>This example runs in Gri-Learn on the web, where you can edit it and see the output.
Practical
એ settings ને code માં વાંચવી
// Read the connection string by name (not hard-coded in the C#)
string cs = ConfigurationManager.ConnectionStrings["FestDb"].ConnectionString;
// Read a custom setting
string adminEmail = ConfigurationManager.AppSettings["AdminEmail"];
int perPage = int.Parse(ConfigurationManager.AppSettings["EventsPerPage"]);Formula
Configuration, code નહીં
web.config નો હેતુ configuration ને code થી અલગ રાખવાનો છે. જે settings બદલાઈ શકે, ખાસ કરીને તમારા development machine અને ખરા server વચ્ચે, તે config file માં રહે છે, અને તમારો code એમને નામથી વાંચે છે.
એટલે FestConnect ને નવા database server પર લઈ જવું એટલે web.config માં એક લીટીનો ફેરફાર, code માં કશો ફેરફાર નહીં અને ફરી build પણ નહીં. Code-behind અને master pages પાછળનો 'એક વાર, સાચી જગ્યાએ વ્યાખ્યા આપો' એ જ સિદ્ધાંત અહીં settings પર લાગુ પડે છે.
Quiz
Database નો connection string સીધો તમારા C# code માં લખવાને બદલે web.config માં શા માટે રાખવો?
- કારણ કે C# માં લખાણનાં strings રાખી શકાતાં નથી
- જેથી code બદલ્યા કે ફરી compile કર્યા વગર connection બદલી શકાય, અને એ એક જાણીતી જગ્યાએ રહે
- કારણ કે web.config code કરતાં ઝડપથી ચાલે છે
- કારણ કે server પર connection strings રાખવાની છૂટ નથી
Show the answer
જેથી code બદલ્યા કે ફરી compile કર્યા વગર connection બદલી શકાય, અને એ એક જાણીતી જગ્યાએ રહે
Connection string web.config માં રાખવાનો અર્થ એ કે તમે એને, દાખલા તરીકે બીજા database server પર જવું હોય ત્યારે, ફક્ત config file બદલીને બદલી શકો છો, code માં કશો ફેરફાર નહીં અને ફરી compile પણ નહીં, અને એ ક્યાં મળશે તે બધાને ખબર હોય છે. વિકલ્પ A અર્થહીન છે: C# strings સહેલાઈથી સંભાળે છે; સવાલ ક્ષમતાનો નહીં, જાળવણીનો છે. વિકલ્પ C ખોટો છે: web.config એ settings file છે, 'ઝડપથી ચાલતી' કોઈ વસ્તુ નથી; કારણ ઝડપ નથી. વિકલ્પ D ઉપજાવેલો છે. ખરો ફાયદો configuration ને code થી અલગ કરવાનો છે: જે settings બદલાય છે (ખાસ કરીને તમારા machine અને production વચ્ચે) તે config માં રહેવી જોઈએ અને runtime વખતે નામથી વંચાવી જોઈએ.
Think first
Settings ને app માં hard-code કરો તો શું બગડે?
ધારો કે તમે દરેક page ના C# માં connection string સીધો ચોંટાડી દો છો. પછી શું ખોટું થાય? વિચારીને પછી tap કરો.
Show the answer
કેટલીક દુખદાયક વસ્તુઓ થાય. પહેલું, ખસેડવું કે બદલવું એ code નો ફેરફાર બની જાય છે: તમે FestConnect ને તમારા laptop પરથી કૉલેજના server પર deploy કરો ત્યારે database નું સરનામું બદલાય છે, અને હવે config ની એક લીટી બદલવાને બદલે તમારે આખા code માં શોધવું પડે, દરેક hard-code કરેલો string બદલવો પડે અને આખી application ફરી BUILD કરવી પડે. બીજું, નકલ અને વિચલન: connection string ઘણી files માં ચોંટાડ્યો હોય તો ક્યારેક કોઈ એક રહી જ જશે, અને app નો એક ભાગ ખોટા database સાથે વાત કરશે. ત્રીજું, સલામતી અને અલગતા: પર્યાવરણ પ્રમાણે બદલાતાં ગુપ્ત મૂલ્યો compile થયેલા code માં ભેળવવાથી એમને સંભાળવાં અને source control થી બહાર રાખવાં અઘરાં બને છે. Setting ને web.config માં કેન્દ્રિત કરવાથી આ બધું સુધરી જાય છે: એક જ અધિકૃત કિંમત, ફરી compile કર્યા વગર બદલી શકાય એવી, અને જ્યાં જરૂર પડે ત્યાં નામથી વંચાય. આ સત્યના એક જ સ્રોતનું configuration વાળું રૂપ છે, એ જ કારણ જેના લીધે તમે layout ને logic થી અલગ કર્યું અને styles કેન્દ્રિત કરી. જે બદલાવાનું જ છે એને ક્યારેય hard-code ન કરો.
Summary
Key takeaways
- web.config એ ASP.NET application ની XML configuration file છે, જે app ના root માં હોય છે (subfolders એને બદલી શકે છે).
- એમાં connectionStrings, appSettings (પોતાની key-value settings), authentication અને authorization ના નિયમો, custom error pages અને session ની settings હોય છે.
- ASP.NET એને runtime વખતે વાંચે છે, એટલે setting બદલવા માટે ફરી compile કરવું પડતું નથી.
- Code માં settings નામથી વાંચો: ConfigurationManager.ConnectionStrings[...] અને ConfigurationManager.AppSettings[...].
- ફાયદો એ છે કે configuration code થી અલગ થાય છે: app બદલ્યા કે ફરી build કર્યા વગર database કે setting બદલી શકાય.
- Settings hard-code કરવાથી દરેક ફેરબદલી વખતે code બદલવો પડે છે, નકલ અને વિચલન થાય છે અને સલામતીની સમસ્યાઓ ઊભી થાય છે.
- યાદ રાખવાની કડી: જે બદલાય છે તેને web.config code થી બહાર રાખે છે.