# How to break Dependency Cycles?

{% tabs %}
{% tab title="Problem" %}
{% code title="Main.elm" %}

```
import User exposing (User)

type Model =
    Maybe User

type Msg =
    UserSpecific User.Msg
    Login String String

update : Msg -> Model -> (Model,Cmd Msg)
update msg =
    case msg of
        UserSpecific userMsg ->
            User.update userMsg
        Login name pass ->
            Debug.todo "login user"
```

{% endcode %}

{% code title="User.elm" %}

```
import Main exposing (Msg)

type User =
    ..

type Msg =
    ..
    
updateUser : Msg -> User -> (Model,Cmd Msg)

viewUser : User -> Html Msg
```

{% endcode %}
{% endtab %}

{% tab title="Solution" %}
{% code title="Main.elm" %}

```
import User exposing (User)

type Model =
    Maybe User

type Msg =
    UserSpecific User.Msg
    Login String String

update : Msg -> Model -> (Model,Cmd Msg)
update msg =
    case msg of
        UserSpecific userMsg ->
            User.update Just UserSpecific userMsg 
        Login name pass ->
            Debug.todo "login user"
```

{% endcode %}

{% code title="User.elm" %}

```
type User =
    ..

type Msg =
    ..
    
updateUser : (User -> model) -> (Msg -> msg) -> Msg -> User -> (model,Cmd msg)
```

{% endcode %}
{% endtab %}

{% tab title="Alternative Solution" %}
{% code title="Main.elm" %}

```
import User exposing (User)

type Model =
    Maybe User

type Msg =
    UserSpecific User.Msg
    Login String String

update : Msg -> Model -> (Model,Cmd Msg)
update msg =
    case msg of
        UserSpecific userMsg ->
            let
                (user,msg) = User.update userMsg
            in
            (Just User,UserSpecific msg)
        Login name pass ->
            Debug.todo "login user"
```

{% endcode %}

{% code title="User.elm" %}

```
type User =
    ..

type Msg =
    ..
    
updateUser : Msg -> User -> (User,Cmd User.Msg)
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Question

How to break out of the dependency cycles?

## Answer

1. Isolate the elements that both of the source modules need access to.
2. Move the shared elements into a new module.
3. Make the source module depend on the new Module.

## Further Reading

* 📄**Article:** [High-Level Dependency Strategies in Elm](https://medium.com/@matthew.buscemi/high-level-dependency-strategies-in-elm-1135ec877d49) by Matthew Buscemi


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://orasund.gitbook.io/elm-cookbook/frequently-asked-questions-1/how-to-break-dependency-cycles.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
