In ASP.NET, is there any reason not to make a set of functions that Add/Remove/Get from the Cache object Static?Get() - just gets the item, no reason not to be staticAdd(), Remove() - I've read that adding/deleting into the cache has it's own internal locking mechanism, so they can be static without me creating my own lock(){} wrapping.
I am developing a web application, which has Data Access Layer and this layer has only one class, in which all methods are static methods like static Insert, static Update, static Search. It has no properties. I am using these methods in my Bussiness Logic class for my users who are visiting my website.Now my question is : 1. Is it right to use static methods in this scenario ?2. What will happen if 10 users call Insert method at the same time ?
public static class PageExtensions { public static int GetUserId(this Page targetPage) { var user = Membership.GetUser(targetPage.User.Identity.Name); return (int)user.ProviderUserKey; } }
Now in a page I need to use this method in a static WebMethod, so I have added another 'extension method' to PageExtensions:
public static int GetUserId() { return (int)Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey; }
and I call it as follows in my WebMethod: PageExtensions.GetUserId()
I did some research after posting. All I found was simple examples for no-layer architectures, like connecting to a database from your aspx page, so, in a corporate environment, it is unnaceptable.
I need to call a server-side method (using ASP.NET Ajax) in a 3-layer architecture.
For example, my Default.aspx contains a method LoadProducts().
[Code]....
[Code]....
This cannot change. There is no way to convert Business and Data layers to static.
How can I call the LoadProducts() method using ASP.NET Ajax?
Actually I have done all my Biz(business layer) and DAL CRUD Opprations using static methodes and I just send my error messages to my log table a sample of biz layer
public static bool Delete(Guid LogGroupID) { using (DAL.ChroXEntities db = new ChroX.DAL.ChroXEntities()) { var q = (from lg in db.LogGroupSet
[code]...
so what should i do to propagate an user friendly error to my users?
I want to be able to maintain certain objects between application restarts.
To do that, I want to write specific cached items out to disk in Global.asax Application_End() function and re-load them back on Application_Start().
I currently have a cache helper class, which uses the following method to return the cached value:
return HttpContext.Current.Cache[key];
Problem: during Application_End(), HttpContext.Current is null since there is no web request (it's an automated cleanup procedure) - therefore, I cannot access .Cache[] to retrieve any of the items to save to disk.
Question: how can I access the cache items during Application_End()?
Im faced with an impending upgrade to an ASP.NET site and I am thinking of introducing DI using Unity. I have researched the ASP.NET DI side of things and have 2 options (Global.asax or IHttpModule). Im happy to use either. As always, there is a legacy object model in place that I would like to upgrade/change. I would like to go down the route of Constructor injection (passing in the DAO) however, each object has a mix of static and instance methods that both currently call into the DB using SqlCommand objects themselves. I want this removed to provide DB independence, therefore can anyone suggest the best way to do the DI in this case? Im open to drastic changes if they are needed.
public class ExampleClass { public ExampleClass(int test) { TestProperty = test; } public int TestProperty {get; set;} public int Save() { // Call DB and Save
I have a class that is creating an instance of StreamReader to an xml file on the local filesystem. It may be possible that this same file is requested multiple times per-second.
I was wondering whether I need to manually add this file to the System.Web.Cache and read it from there, or whether Windows itself is clever enough to cache the item itself so that it 'knows' when ASP.NET requests this file the second/third etc time that it doesnt have to do a disk seek/read operation and pulls it from its own cache?
This article: http://dotnetperls.com/file-read-benchmarks seems to back this up, but this: article:
[URL](although not discussing from a performance perspective, and maybe for other reasons entirely) lists how to add a physical file to the cache.
In one of the interview, I was asked why should we have to go for Single Design pattern, instead of just creating static methods. Because creating static methods also serve the same purpose, i.;e avoiding flooding of objects.
Last night I wrote up my first IHttpModule to do some request processing. I'm using a regular expression to inspect the raw url. The IHttpModule will be called on every request, so it seems reasonable to do some sort of caching of the regular expression object to prevent creation of it on every request.
Now my question... what is better: use the HttpContext.Current.Cache to store the instantiated object or to use a private static Regex in my module?I'm looking forward to the reasons why. Just to clarify: the regex will never change and thus always be the same thing.
I know the big difference, but one thing is confusing me. Cache works only on a single machine on webfarm and to make it globally we use Velocity or memcache.So is static does same, or is it already work globally ? eg
Cache["someid"] = "value"; //this will only work on single machine cache,
static string abc = "value"; // will this work on single machine or globally ?
I load css and js files dynamicaly based on which controls are used on particular page. I am doing it by using following code:
[code]....
where AddLinks method adds HtmlLink controls to Page.Header with href attribute set to coresponding css and/or js file.
I would like to add Interface that would force new controls to have AddLinks method but it is impossible since it is a static method. Because my custom controls inherit from Control class I cannot use abstract class and/or virtual methods either. How can I achieve my goal?
TL;DR: Which is likely faster: accessing static local variable, accessing variable stored in HttpRuntime.Cache, or accessing variable stored in memcached?At work, we get about 200,000 page views/day. On our homepage, we display a promotion. This promotion is different for different users, based on their country of origin and language.
All the different promotions are defined in an XML file on each web server. We have 12 web servers all serving the same site with the same XML file. There are about 50 different promotion combinations based on country/language. We imagine we'll never have more than 200 or so (if ever) promotions (combinations) total.
The XML file may be changed at any time, out of release cycle. When it's changed, the new definitions of promotions should immediately change on the live site. Implementing the functionality for this requirement is the responsibility of another developer and I.
Originally, I wrote the code so that the contents of the XML file were parsed and then stored in a static member of a class. A FileSystemWatcher monitored changes to the file, and whenever the file was changed, the XML would be reloaded/reparsed and the static member would be updated with the new contents. Seemed like a solid, simple solution to keeping the in-memory dictionary of promotions current with the XML file. (Each server doing this indepedently with its local copy of the XML file; all XML files are the same and change at the same time.)
The other developer I was working holds a Sr. position and decided that this was no good. Instead, we should store all the promotions in each server's HttpContext.Current.Cache with a CacheDependency file dependency that automatically monitored file changes, expunging the cached promotions when the file changed. While I liked that we no longer had to use a FileSystemWatcher, I worried a little that grabbing the promotions from the volitile cache instead of a static class member would be less performant.
(Care to comment on this concern? I already gave up trying to advocate not switching to HttpRuntime.Cache.)
Later, after we began using HttpRuntime.Cache, we adopted memcached with Enyim as our .NET interface for other business problems (e.g. search results). When we did that, this Sr. Developer decided we should be using memcached instead of the HttpRuntime (HttpContext) Cache for storing promotions. Higher-ups said "yeah, sounds good", and gave him a dedicated server with memcached just for these promotions. Now he's currently implementing the changes to use memcached instead.
I'm skeptical that this is a good decision. Instead of staying in-process and grabbing this promotion data from the HttpRuntime.Cache, we're now opening a socket to a network memcached server and transmitting its value to our web server.This has to be less performant, right? Even if the cache is memcached. (I haven't had the chance to compile any performance metrics yet.)On top of that, he's going to have to engineer his own file dependency solution over memcached since it doesn't provide such a facility.
Wouldn't my original design be best? Does this strike you as overengineering? Is HttpRuntime.Cache caching or memcached caching even necessary?
I am working on a asp.net web site, like normal user, we use asp.net developer server during coding and testing.Today, I found the firefox not cache any static file of my site, since our application is pretty big, it made page load time very slow.I checked firefox about:cache, all the static file cache setting looks like
Key: http://localhost:26851/App_Layout/icons/actions/email/folder.png Data size: 871 bytes Fetch count: 1[code]...
The expires date is set back to 1969-12-31, I believe that's the reason why they are loaded very time.I am using Visual studio 2008, windows 7 machine. The application works fine in IE, the contents are properly cached.
I have a class that maintains a static dictionary of cached lookup results from my domain controller - users' given names and e-mails.My code looks something like:
private static Dictionary<string, string> emailCache = new Dictionary<string, string>(); protected string GetUserEmail(string accountName) { if (emailCache.ContainsKey(accountName)) { return(emailCache[accountName]); } lock(/* something */) { if (emailCache.ContainsKey(accountName)) [code]...
firstly a static class only ever exists once and is not an instance. Any static members (ie static int NoOfPeople;) is stored in one place and is shared between all sessions (like the old global variables). Now static methods is where i'm not 100% sure. If I have a static method that doesn't use any other static members could this cause inconstant results, example (this is a fairly pointless method but just a quick example of the top of my head)
[Code]....
So in this example if two sessions (or threads) were to call this at the same time - would they both get back the expected results, because the method only uses private data (a, b and totalToReturn).Im sure this sounds a little simple but I will be using static methods to build user objects and various other objects that there will have to be a 100% garentee that the objects will not get mixed up between sessions and the wrong things return to the user.
I am using DataClassesDataContext to map all the tables from the db into my asp.net application.For doing CRUD operations i have made static classes with methods, and inside every method a instantiate DataClassesDataContext.For instance:
public static class UserQ { public static User getUserById(int userId)
I am using <%# MyFormatClass(Eval("fieldname")) %> to display data in a grid view on my page. MyFormatClass works fine, but now I want to move it to a central location that can be used by many pages. When I try to access the class <%# Utils.MyFormatClass(Eval("fieldname")) %> it no longer works. Is this something we are allowed to do?
I have a web site project using the .NET 2.0 Framework that I am working on in Visual Studio 2008. I am using a third-party DLL in my project. I have added a reference to the DLL in my project and I can use everything as expected, including a number of extension methods for certain objects. Intellisense sees these extension methods, and I am able to make use of them and I can build my project on my local machine without any errors.
However, when running my website on a production server, I get a Compilation Error saying that the extension method I am trying to use could not be found. I can use everything else in the DLL besides the extension methods on the production server.
This is my first exposure to extension methods and understand what they are, but I can't seem to figure out why I can't make use of these methods outside of my local machine.