Hey peeps :-) (...sorry this is a bit long)
I'm developing an application using C# Winforms and an MVP pattern, it's only a relatively simple app - although it is doing a fair amount of stuff, so I consider the MVP pattern to be a big help in organising the code.
I've created an instance of the Models to be used - a STATIC instance - in the Program class before the WinForm is launched. (The Model classes are NOT static classes mind)
I then create the main form (View) which creates a reference to its Presenter. (The View/Presenter classes are all non-static).
My reasoning behind the static instance is to allow the entire app to access the same model information. And then when the app closes I can access the up-to-date model data from the Program class and save anything that needs persisting. Which is nice as it keeps all the loading/saving in one place.
There will only be one instance of each Model class in any case, there are no DB connections and what have you.
And from what I can tell only one thing will need access to any Model at any one time.
I do have 'BackgroundWorker' Threads doing various things - but I am not referencing any of the Models in those Threads. I pass the value of the data from the Model to the Thread. The Thread then passes back information which, if required, is propagated to the relevant Model via the main thread.
I just thought creating the Model instance as Static was a neater way then forever passing around references...(y'know, pass a ref to the main form, which then passes it to another form etc etc)
This way the code inside the view just looks like:
// this = ref to the view
PresenterA p = new PresenterA( this, Program.ModelA ); ...
(Obviously the class names aren't just PresenterA, ModelA etc, that would be rather silly)
Now, I'm pretty sure that someone will shout at me and tell me that is a horrific way of doing things.
If so, could those who oppose this please speak now or forever hold your peace.
TIA
(BTW, I'm just a noob, I don't develop c# for a living, it's a hobby thing)
:-)
see Implementing MVC with Windows Forms for lots of pointers to your given options
Also consider using Dependency injection to avoid you static classes
I would use a framework, like MVCSharp.
精彩评论