A good way to stretch an object across multiple classes

I have a set of orders. I would like to hit the database once, get orders, save them, and then access that collection through multiple forms. I know in asp.net you can use things like Application Object or Session Object, but how do you do this in a win form application? I was thinking about creating a static collection that can be accessed through multiple forms, classes or wherever. Does this sound right and is it possible?

thank

+1


source to share


2 answers


The fundamental difference between WebForms and WinForms is that WinForms are workable. In other words, you can ensure that the objects you load are available until the program terminates. The same is not true for WebForms, since HTTP is not stateless and ASP.Net dumps a lot of "magic" [ViewState, Server session, Cookies, Hidden fields, etc.] to glue the web application pages together.

With this basic concept, it is easy to make an object globally available in WinForms. Use Singleton / Static class and assign it to members. You can always say .Instance .. [in case of static / static classes] to extract values ​​from any form.

One Gottcha is "thread safe" ... aka .. multiple forms of read / write for a static property at the same time. If you can take care of it, relaxation will be a breeze.

For your problem:

You can have your own Singleton Cache class that will cache records. The Cache class will execute the custom collection. You need to make sure it is thread safe [or make sure your application only loads once and reads from now on] ... You may need to consider a full-fledged ORM if you want database changes to be reflected in entity objects.



CacheList.OrderCache[orderNo].Customer.Address.City = "Las Vegas";

class static CacheList {public static Cache OrderCache {get; internal set;}

}

public class Cache: CollectionBase where T: Entity {.....

}

+1


source


Do you have to have some kind of data object that accesses the database anyway? Wrap this in a CacheProxy class that goes into the database if it doesn't have a local copy. I heard you can use System.Web cache in Winforms .



0


source







All Articles