patterncsharpMinor
WPF object model control with singleton and static messenger? ConditionalWeakTable?
Viewed 0 times
messengercontrolwithwpfconditionalweaktablesingletonandobjectmodelstatic
Problem
As always first I have to say that I'm an amateur and not native English speaker, so please have a little patience if I write nonsense ;)
I've worked as an accountant for more than 10 years and since like one year I'm working as a freelancer accountant, so I'm trying to make an accounting app for my personal use (well, lately I wonder this, since the app is getting really big), using c# and WPF.
The thing is that I'm trying to achieve a chrome alike tab-based GUI, with lots of object models that can be duplicated across the different tabs, something that I don't want to happen since some objects can be BIG.
So, googling and reading StackOverflow I've come to think in a sort of singleton that store all the object models, which can be asked for any single object. Since I have several assemblies in my solution, I can not access that singleton from any part of the app, so I made a static messenger located in an assembly that is referenced by all the other assemblies (it's called GeneralHelpers), that launch events to which the singleton, located at the only assembly that references all the other assemblies(called AdConta), is subscribed.
The singleton, note that the type switch is not finished, since I'm not sure of some things yet (that's why I'm here ;D I'm not even sure if this is a real singleton or just something else). At the end the type switches should include all the object models used by the app:
```
namespace AdConta.ModelControl
{
///
/// Control and store ALL object models. Just for internal use, to add object models use static AppModelControlMessenger.
///
public class AppModelControl
{
public AppModelControl()
{
AppModelControlMessenger.ModelAddedEvent += OnModelAddedEvent;
AppModelControlMessenger.ObjModelAskedEvent += OnObjModelAskedEvent;
this._Comunidades = new Dictionary();
this._Personas = new Dictionary();
this._Conceptos = new Dictionary();
I've worked as an accountant for more than 10 years and since like one year I'm working as a freelancer accountant, so I'm trying to make an accounting app for my personal use (well, lately I wonder this, since the app is getting really big), using c# and WPF.
The thing is that I'm trying to achieve a chrome alike tab-based GUI, with lots of object models that can be duplicated across the different tabs, something that I don't want to happen since some objects can be BIG.
So, googling and reading StackOverflow I've come to think in a sort of singleton that store all the object models, which can be asked for any single object. Since I have several assemblies in my solution, I can not access that singleton from any part of the app, so I made a static messenger located in an assembly that is referenced by all the other assemblies (it's called GeneralHelpers), that launch events to which the singleton, located at the only assembly that references all the other assemblies(called AdConta), is subscribed.
The singleton, note that the type switch is not finished, since I'm not sure of some things yet (that's why I'm here ;D I'm not even sure if this is a real singleton or just something else). At the end the type switches should include all the object models used by the app:
```
namespace AdConta.ModelControl
{
///
/// Control and store ALL object models. Just for internal use, to add object models use static AppModelControlMessenger.
///
public class AppModelControl
{
public AppModelControl()
{
AppModelControlMessenger.ModelAddedEvent += OnModelAddedEvent;
AppModelControlMessenger.ObjModelAskedEvent += OnObjModelAskedEvent;
this._Comunidades = new Dictionary();
this._Personas = new Dictionary();
this._Conceptos = new Dictionary();
Solution
OK so your basic issue is that you want to cache model objects that are expensive to create / duplicate. Yes, you could use
I'm not sure I see the need for your static "messenger" class, and I personally really dislike static / singletons because they tend to couple stuff unnecessarily and cause headaches with unit testing. All the type switching is not great. I tend to classify type checking as smelly code
Your basic requirement is that for a given model object type, you want a single place to fetch it from so you can cache instances. This is pretty much a simple Repository.
You could create a generic
WeakReferences, but you're probably better off using something like MemoryCache that will allow you better control of when they get destroyed.I'm not sure I see the need for your static "messenger" class, and I personally really dislike static / singletons because they tend to couple stuff unnecessarily and cause headaches with unit testing. All the type switching is not great. I tend to classify type checking as smelly code
Your basic requirement is that for a given model object type, you want a single place to fetch it from so you can cache instances. This is pretty much a simple Repository.
You could create a generic
Repository class, and create an instance for each type you want to cache. If a VM requires access to a type, then the appropriate repository is supplied to the VM constructor. No singletons, no statics, no switching on type. Just a simple class that is passed around.Context
StackExchange Code Review Q#136218, answer score: 3
Revisions (0)
No revisions yet.