This question relates to a desktop application.
Something I'm always thinking about, is a way to be able to access an object that could be considered global, like if it is declared as static. But I always avoid this.
For example, in an application a User object (or something similar) is present very often. Imagine somewhere from the business logic a data model/repository is used, and you need the user ID to pass it as a parameter to the data layer so it can load the corresponding data for the user. Most times in web applications I see that information like a user ID is coming from a URL parameter, or HTTP post data, and is sent into a controller via its action method.
In desktop applications this is different, since all 'state' stays the same. Suppose a user object is constructed once on program startup, and of course stored now in memory somewhere. How do I access it from within another part of the application? It seems there are only two options:
- Using a static method on a general class that contains the user object, which returns it.
- Injecting the user object through the whole tree of classes that need the object.
I think both options are ugly and not optimal. Since I think it's a very common situation, what are best practises to keep a good structure?
Aucun commentaire:
Enregistrer un commentaire