Implement State Design Pattern with EF core and dotNet

Amr elshaer
4 min readMar 25, 2023

--

The State design pattern is a behavioral design pattern that allows an object to change its behavior at runtime by changing its internal state. This pattern is especially useful when an object’s behavior depends on its state, and when it is necessary to change an object’s behavior dynamically.

The State pattern consists of four main components:

  1. Context: the object whose behavior changes depending on its internal state. The context maintains a reference to the current state object and delegates a request to it.
  2. State: an interface or abstract class that defines the behavior associated with a particular state of the context. Each state class implements the same set of methods, but the implementation of these methods varies depending on the state.
  3. Concrete state: a class that implements the State interface or extends the State abstract class. A concrete state class represents a specific state of the context and provides the implementation for the methods defined in the State interface.
  4. Client: the object that interacts with the context object. The client sends requests to the context object, which delegates the requests to the current state object.

Implementation

Let’s take a look at an example of how the State pattern can be used to implement a simple Order State.

Our Order Has 4 State

Our Context is an Order object whose behavior changes depending on its internal state (State).

State: an interface or abstract class that defines the behavior associated with a particular state of the context. Each state class implements the same set of methods, but the implementation of these methods varies depending on the state.

Concrete state: a class that implements the State interface or extends the State abstract class. A concrete state class represents a specific state of the context and provides the implementation for the methods defined in the State interface.

1- DraftState order can be Confirmed or Cancel But can’t be Process

2- CofirmedState order can be processed or cancel

In the ProcessState the state can’t be changed.

also in CanceledState can’t be changed.

Our DbContext will look like this

Our Controller and all actions

Test

1- Create an Order

2- If you try to make an order process before confirming it

3- Confirm the order

4- Process the order

Note

The closest design pattern to the state design pattern is the strategy design pattern. While the state design pattern is used to change an object’s behavior based on its internal state, the strategy design pattern is used to change an object’s behavior based on a strategy or algorithm that is selected at runtime.

In the strategy design pattern, the object that changes its behavior is called the “context”, and the different strategies or algorithms that it can use are represented by separate “strategy” objects. Each strategy object defines a different algorithm that the context can use to achieve its goal.

Here are the key components of the strategy design pattern:

  1. Context: The object that has a variable strategy that can change at runtime. The context object delegates behavior to the current strategy object.
  2. Strategy: An interface or abstract class that defines the behavior of the context when it is using a particular strategy. Each strategy object implements this interface and defines the behavior of the context when it is using that strategy.
  3. Concrete strategies: Concrete implementations of the Strategy interface that define the behavior of the context when it is using a particular strategy.
  4. SetStrategy method: A method in the context object that allows it to change its strategy at runtime. This method updates the strategy variable to the new strategy object.

The source code GitHub

--

--

Amr elshaer
Amr elshaer

Written by Amr elshaer

Software engineer | .Net ,C# ,Angular, Javascript

No responses yet