c# - What is the alternative to Static Class in asp.net mvc 4? -
i seen here static class & static functions bad because take lot of memory.
i use many things need interact database etc..
this example of part of static localization class, getresources
public static class sflocalization { public static string getresources(string key) { string currentlanguage = system.threading.thread.currentthread.currentuiculture.tostring(); if (memorycache.default["resources_" + key] == null) { string x using (db _db = new db()) { memorycache.default["resources_" + key] = _db.languages.first(l => l.key == key && l.languagecode == currentthread).value; } } return memorycache.default["resources_" + key]; } }
and in view, controllers etc.. write translated value
@sflocalization.getresources("newsletterboxtitle")
1.) static class in situations bad?
2.) alternative ? maybe dependency injection (ninject etc..)?? (i seen in book apress - pro asp.net mvc 4
a static class (or module) replacement domain or application service. it's convenient expose service static class can accessed anywhere - true if it's cross-cutting concern such localization.
this approach can , running quickly, present several problems. being able access service anywhere/any time encourages poor coding practices , can lead spaghetti code. static modules make classes use them difficult unit test. these both problems grow exponentially project increases in size - it's idea them dealt possible.
as mentioned in question, dependency injection 1 way ensure classes have access service, without making static/global.
Comments
Post a Comment