Circular dependency with generics

c#, circular-dependency, generics, oop

Solution

The circular reference you've pointed out is not a problem. For your code to compile, you will need to amend your `IAction` interface definition though:

public interface IAction<State, Action>
    where State : IState
    where Action: IAction<State, Action>
{
    IStateSpace<State, Action> StateSpace { get; }
}

How's that for circular reference :) Generally the compiler will handle them by using placeholders. In the case of generic type constrains, this probably isn't even necessary. One small note: if you define a circular reference between classes that aren't in the same assembly, it will become a problem.

Problem

I have defined the following interface: ``` public interface IStateSpace<State, Action> where State : IState where Action : IAction<State, Action> // <-- this is the line that bothers me { void SetValueAt(State state, Action action); Action GetValueAt(State state); } ``` Basically, an `IStateSpace` interface should be something like a chess board, and in each position of the chess board you have a set of possible movements to do. Those movements here are called `IAction`s. I have defined this interface this way so I can accommodate for different implementations: I can then define concrete classes that implement 2D matrix, 3D matrix, graphs, etc. ``` public interface IAction<State, Action> { IStateSpace<State, Action> StateSpace { get; } } ``` An `IAction`, would be to move up(this is, if in `(2, 2)` move to `(2, 1)`), move down, etc. Now, I'll want that each action has access to a StateSpace so it can do some checking logic. Is this implementation correct? Or is this a bad case of a circular dependence? If yes, how to accomplish "the same" in a different way? Thanks

Original source