hi guys:)
i have a big problem:-/
now i have a program that have a client class and model (in mvc)
then the problem is that
1-i want the client when something comes from server to inform the model
2-i want the model if any change has been made in it to inform the client
well i think it is a command pattern
but i don't know where to put these statements ?:?:

Receiver receiver = new Receiver();
      Command command = new ConcreteCommand(receiver);
      Invoker invoker = new Invoker();
 
      // Set and execute command
      invoker.SetCommand(command);
      invoker.ExecuteCommand();

please if anyone have any idea please share it and thanks a lot

Dani AI

Generated

pointed in the right direction: keep responsibilities separate and use events for notifications. For an ASP.NET client/server setup the practical approach is this: have the client tell the server via a controller/service endpoint, let the controller update the model, and let the model raise events that the controller or a service layer observes and forwards to connected clients. Use the Command pattern only when you need to encapsulate operations (undo/redo, queuing, replay). Do not put networking or UI wiring inside the model.

A compact server-side example (C#) showing a model that raises an event and a service that forwards that change to connected web clients via a hub:

public class MyModel
{
    public event EventHandler Changed;

    public void Update(string value)
    {
        // change state...
        Changed?.Invoke(this, EventArgs.Empty);
    }
}

public class ModelService
{
    private readonly IHubContext<NotifyHub> _hub;
    public ModelService(IHubContext<NotifyHub> hub, MyModel model)
    {
        _hub = hub;
        model.Changed += (s,e) => _hub.Clients.All.SendAsync("ModelChanged");
    }

    public void HandleClientUpdate(string payload) => /* validate */ /* call model.Update(payload) */;
}

Client-side (browser) receives push and then calls server if it needs to change the model:

const conn = new signalR.HubConnectionBuilder().withUrl("/notifyHub").build();
conn.on("ModelChanged", () => fetch("/api/model/current").then(r => r.json()).then(render));
conn.start();

Notes and cautions: wire the subscriptions in the controller/service layer, not in the model. For desktop clients subscribe directly to model events and marshal callbacks to the UI thread. For web apps prefer WebSockets/SignalR (or SSE/long-poll) over polling for server-to-client pushes. Throttle or batch frequent model events to avoid flooding clients.

Recommended Answers

All 2 Replies

As I read it, you already mentioned MVC. View tells controller, controller updates model (or vice versa).
MVC is the pattern.
In addition, Observer can be used, where client subscibes to model and model notifies client(s).
There are java and php samples at

As I read it, you already mentioned MVC. View tells controller, controller updates model (or vice versa).
MVC is the pattern.
In addition, Observer can be used, where client subscibes to model and model notifies client(s).
There are java and php samples at

thanks padtes alot but how the client can notify the model???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.