Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose way to get simple, human-readable, representation of the model for debugging #6304

Closed
ajcvickers opened this issue Aug 11, 2016 · 2 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@ajcvickers
Copy link
Contributor

There are some internal APIs called ToDebugString that allow a model to be dumped out in a form like this:

Model 1.1.0-csproj
  Game
    Properties: 
      Id (<Id>k__BackingField, int) Required PK ReadOnlyAfterSave RequiresValueGenerator ValueGenerated.OnAdd 0 0 0 -1 0
    Navigations: 
      Items (<Items>k__BackingField, ICollection<Item>) Collection ToDependent Item Inverse: Game 1 -1 1 -1 -1
      Levels (<Levels>k__BackingField, ICollection<Level>) Collection ToDependent Level Inverse: Game 2 -1 2 -1 -1
    Keys: 
      Id PK
  Item
    Properties: 
      Id (<Id>k__BackingField, int) Required PK ReadOnlyAfterSave RequiresValueGenerator ValueGenerated.OnAdd 0 0 0 -1 0
      GameId (<GameId>k__BackingField, int) Required FK Index 1 1 1 -1 1
      LevelId (<LevelId>k__BackingField, int) Required FK Index 2 2 2 -1 -1
    Navigations: 
      Game (<Game>k__BackingField, Game) ToPrincipal Game Inverse: Items 3 -1 3 -1 -1
      Level (<Level>k__BackingField, Level) ToPrincipal Level Inverse: Items 4 -1 4 -1 -1
    Keys: 
      Id PK
    Foreign keys: 
      GameId -> Game.Id ToDependent: Items ToPrincipal: Game
      GameId, LevelId -> Level.GameId, Level.Id ToDependent: Items ToPrincipal: Level
  Level
    Properties: 
      GameId (<GameId>k__BackingField, int) Required PK FK Index ReadOnlyAfterSave 0 0 0 -1 0
      Id (<Id>k__BackingField, int) Required PK ReadOnlyAfterSave 1 1 1 -1 -1
    Navigations: 
      Game (<Game>k__BackingField, Game) ToPrincipal Game Inverse: Levels 2 -1 2 -1 -1
      Items (<Items>k__BackingField, ICollection<Item>) Collection ToDependent Item Inverse: Level 3 -1 3 -1 -1
    Keys: 
      GameId, Id PK
    Foreign keys: 
      GameId -> Game.Id ToDependent: Levels ToPrincipal: Game

This is indented to be a relatively terse and not necessarily complete form useful for debugging the model. It is not intended as a serialization format.

We should consider if something like this should be made public, and if so what form it should take.

@divega divega added this to the 1.1.0 milestone Aug 12, 2016
@divega
Copy link
Contributor

divega commented Aug 12, 2016

Triage: We think this can be useful and @ajcvickers is going to investigate if this can be shown in the debugger automatically without introducing much overhead.

@ajcvickers
Copy link
Contributor Author

My goal was to have the human readable format available to anybody using the debugger, but in such a way that it is only created on-demand, since creating the view for a very large model can be quite expensive resulting in latency in the debugger and memory usage. I was not able to find anything that would do this using VS attributes, etc., without installing third-party stuff. However, I did come up with this: create a level of indirection in code:

// Note that Model is internal
public class Model
{
    ...
    public virtual DebugView<Model> DebugView
        => new DebugView<Model>(this, m => m.ToDebugString());
}

// Internal
public class DebugView<T>
{
    private readonly T _metadata;
    private readonly Func<T, string> _toDebugString;
    private string _view;

    public DebugView(T metadata, Func<T, string> toDebugString)
    {
        _metadata = metadata;
        _toDebugString = toDebugString;
    }

    public virtual string View => _view ?? (_view = _toDebugString(_metadata));
}

So now when I expand Model in the debugger I get this:
image

Note that the debug view of the model has not yet been created, so there is minimal additional overhead. But if I want to see the debug view, then I just expand out the property, at which point the view will be created.
image
image
Stepping over a piece of code that causes the model to be updated causes the view to be re-created:
image

@ajcvickers ajcvickers removed this from the 1.1.0 milestone Aug 19, 2016
@divega divega added this to the 1.1.0 milestone Aug 19, 2016
ajcvickers added a commit that referenced this issue Aug 23, 2016
… for debugging

Issue #6304

Lazy-evaluated detailed representation including annotations as discussed, plus single line representation for standard ToString.
ajcvickers added a commit that referenced this issue Aug 24, 2016
… for debugging

Issue #6304

Lazy-evaluated detailed representation including annotations as discussed, plus single line representation for standard ToString.
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 24, 2016
@ajcvickers ajcvickers modified the milestones: 1.1.0-preview1, 1.1.0 Oct 15, 2022
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

2 participants