c# - How should one implement the communication between classes in an MVC pattern? -
preamble: question not asp.net mvc framework.
edit: more clear application game written in c# various web frameworks not applicable.
how should best connect classes events when building using mvc pattern?
i'm wanting write mvc code can learn more design pattern. understand why each of these 3 elements of pattern should separate, i'm not how implement connecting them together. i'm writing in c#, i'm new with, , strikes me events should used. i'm unsure of how should subscribe/unsubscribe events, , need sanity check.
for example i'll post view/controller classes. in example view wants send datarequest event, , controller wants subscribe pass along model. on other side of things controller @ point raise dataupdated event view should subscribe to. way i've implemented how they're subscribing each other make sense?
//////////////////////////////////////////////////// public class uiscreenview { public event system.eventhandler<system.eventargs> datarequestedevent; //==================================== private void dataupdatedeventhandler(object sender, system.eventargs args); //==================================== //==================================== public void subscribe(uiscreencontroller controller) { controller.dataupdatedevent += dataupdatedeventhandler; controller.subscribe(this); } //==================================== //==================================== public void unsubscribe(uiscreencontroller controller) { controller.dataupdatedevent -= dataupdatedeventhandler; controller.unsubscribe(this); } }
and controller:
public class uiscreencontroller { private uiscreenview view; public event system.eventhandler<system.eventargs> dataupdatedevent; private void datarequestedeventhandler(object sender, system.eventargs args); public void init(uiscreenview v) { view = v; if (view != null) { view.subscribe(this); } } ~uiscreencontroller() { if (view) { view.unsubscribe(this); view = null; } } public void subscribe(uiscreenview view) { view.datarequestedevent += datarequestedeventhandler; } public void unsubscribe(uiscreenview view) { view.datarequestedevent -= datarequestedeventhandler; } private void datarequestedeventhandler(object sender, system.eventargs args) { //request data model } }
what have done , tried make is mvp pattern, not mvc. in short, mvc pattern used request-response architecture , not event driven. flow is:
- ui request
- controller accept request
- controller process request (such accessing model, etc)
- controller send response ui
if want implement in c#, assume winform, request handled inside events. lets in game choose menu load game, afterwards need show saved game data.
the following example illustration, expect non-real life logic apply. controller like:
public class savedatacontroller { public savedata[] getsavedata() { return model.getsavedata(); } }
and ui like:
public void loadgamebutton_click(object sender, eventargs e) { savedatacontroller controller = new savedatacontroller(); savedata[] savedata = controller.getsavedata(); savedatalist.datasource = savedata; }
this example shows request controller being initiated in button click event.
as other said, mvc architecture not suited non-web application (or event driven) not handle event methods. of ui logic handled in event.
Comments
Post a Comment