Skip to content

Conversation

@AbdoMahfoz
Copy link
Contributor

Consider the following case

public class Category : BaseModel
{
    [Required] public string Name { get; set; }

    public int? ParentId { get; set; }
    public virtual Category Parent { get; set; }
    
    public virtual ICollection<Category> Children { get; set; }
    public virtual ICollection<Product> Products { get; set; }

    public override string ToString() => $"{Name} - {(ParentId == null ? "root" : "child")}";
}

Currently, navigation properties Children and Products cause the code to crash.
After inspecting the code I found that there is no support for rendering such properties.
So, I made the controller eager load the entities before sending them to the View to avoid the crash, then I added code in the view that should render these properties as <ul>

{
foreach (var raw in (IEnumerable)entityProperty.GetValue(value)!)
{
finalString += $"<li><a href=\"{Url.Action("Index", new { id = raw.GetType().Name })}\">{raw}</a></li>";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strings behave like value variables and each time you add to them create another variable and its not optimum, better to use stringbuilder

finalString += $"<li><a href=\"{Url.Action("Index", new { id = raw.GetType().Name })}\">{raw}</a></li>";
}
}
catch(NullReferenceException) {}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's up with that? Consuming exceptions like that is an anti-pattern. Shouldn't there be a way to avoid an NRE in the first place?

@AkhmedovSanjar
Copy link

When this changes will be realesed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants