concept agent in category c#

appears as: gents, n agent, An agent, agent, Agent, agents, Agents, The agent
Functional Programming in C#: How to write better C# code

This is an excerpt from Manning's book Functional Programming in C#: How to write better C# code.

  • From the point of view of the client code consuming an agent, you can only tell it messages, so by using the interface we avoid exposing the type parameter for the state. After all, the type of the state is an implementation detail of the agent.
  • You can envisage other implementations, such as stateless agents, or agents that persist their state.
  • Finally, here are some convenience methods for easily creating agents:

    public static class Agent
    {
       public static Agent<Msg> Start<State, Msg>
          ( State initialState
          , Func<State, Msg, State> process)
          => new StatefulAgent<State, Msg>(initialState, process);
    
       public static Agent<Msg> Start<Msg>(Action<Msg> action)
          => new StatelessAgent<Msg>(action);
    }

    The first overload simply creates an agent with the given arguments. The second takes an action and is used to create a stateless agent: an agent that processes messages sequentially but doesn’t keep any state. (The implementation is trivial, as it just creates an ActionBlock with the given Action.) We can also define agents with an asynchronous processing function/action; I’ve omitted the overloads for brevity, but the full implementation is in the code samples. Next, we’ll get started using agents.

    Agents and actors are closely related. In both cases, a single thread processes messages sequentially and communicates with other actors/agents by sending them messages. There are also important differences:

  • Agents run within a single process, whereas actors are designed for distributed systems. In the examples we’ve looked at so far, a reference to an agent refers to a specific instance in the current process. References to actors, on the other hand, are location-transparent; that is, when you have a reference to an actor, that actor may be running in the same process or on another process, possibly on a remote machine. So a reference to an agent is a pointer, whereas a reference to an actor is an ID that the actor model implementation will use to route messages across processes as appropriate.
  • Most implementations of the actor model cater for error handling through supervisor actors that monitor a supervised actor and take action when it faults. This allows us to nicely separate happy-path logic from error handling and makes it possible to create very robust systems. There’s no counterpart of this with agents.
  • An agent’s state should be immutable and never shared outside the scope of the agent. In our implementation, however, there’s nothing stopping an inexperienced developer from creating an agent whose state is mutable, and from passing that mutable state to other functions, thus allowing that state to be changed from outside the scope of the agent. With actors, messages are serialized, so this should never occur.
  • As you can see, although the fundamental idea behind agents and actors is the same, the actor model is richer and more complex. You should only consider using the actor model if you require coordination of concurrent operations across different applications or machines; otherwise, the operational and setup cost would be unjustified, and you should rely on agents instead.

    15.4.3. An agent is not an object

    This is the kind of schoolboy error that’s common when you’re getting used to programming with agents or actors. Although agents and actors are similar to objects, you can’t think of them as such. The error in listing 15.8 is that we’re conceptually giving the agent the “responsibility” of providing the caller with the requested AgentProcess, and this has given us a suboptimal solution.

    Instead, agents should only have the responsibility of managing some state. The agent in question manages a dictionary, so we can call it to look up an item, or to add a new item, but going to the DB to retrieve data is a relatively slow operation that’s not directly pertinent to managing the cache of AgentProcesses.

    All that’s left to show is the definition of the agent in the AccountRegistry’s constructor.

    Listing 15.10. Revised definition of the agent for storing a registry of AccountProcesses

    This implementation is slightly more complex, but more efficient, and it has given us the chance to see a common pitfall when programming with agents; namely, performing an expensive operation in the body of an agent that doesn’t strictly require synchronized access to the agent’s state.