John John
John John

Reputation: 1

Better way to use model class helper methods

Inside my model class I am using a method call IsAlreadyAssigned, as follow:-

public partial class DataCenter 
{
     public bool IsAlreadyAssigned()
     {
         return (TMSRacks.Any() ||  TMsRouters.Any() || Zones.Any());
     }
}

The main aim of this helper method is to hide the delete button on the view if the object have child records; as follow:-

<td>
    @Html.ActionLink("Edit", "Edit", new { id= item.ID}) 
    @if (!item.IsAlreadyAssigned()) 
    { 
        <text>|</text> 
        @Ajax.ActionLink("Delete",
                         "Delete", "DataCenter",
                         new { id = item.ID },
                         new AjaxOptions
                         { 
                             Confirm = "Are You sure You want to delete (" + item.Name + ")",
                             HttpMethod = "Post",
                             OnSuccess = "deletionconfirmation",
                             OnFailure = "deletionerror"
                         })

    }
</td>

But on my index view which shows 10 records at a time,I will hide or display the delete link accordingly ,so I have to include all the navigation properties, in my query as follow:-

public IQueryable<DataCenter> AllFindDataCenters(string q, bool forautocomplete = false)
{
    return from datacenter in tms.DataCenters.Where(a=> (String.IsNullOrEmpty(q)) || ( a.Name.ToUpper().StartsWith(q.ToUpper())))
        .Include(a=>a.Zones)
        .Include(a=>a.TMsRouters)
        .Include(a=>a.TMSRacks)
        select datacenter;
}

Other wise each record on my index view might make at most three queries to the DB to check if there is any child records. So I ended up including all the navigation properties as shown above, just to implement the requirement of hiding /displaying a delete link . so is there a better way to manage my logic, as I do not need to display any of the navigation properties data (tmsrouter,tmsfirewalls,zonea), I just want to know if atleat one record exists or not ? Thanks

Upvotes: 0

Views: 78

Answers (1)

James
James

Reputation: 82136

Instead of working directly with your business models in your view, return a view model and expose the information you need e.g.

public class DataCenterViewModel
{
    ...
    public bool HasZones { get; set; }
    public bool HasTmsRouters { get; set; }
    public bool HasTmsRacks { get; set; }

    public bool AlreadyAssigned { get { return HasZones || HasTmsRouters || HasTmsRacks; } }
}

Then in your query construct your view models. Also, just an FYI, it's best to work with a more generic construct like IEnumerable<T> when passing stuff to the view, IQueryable<T> suggests you want to further query the data source once at the view.

public IEnumerable<DataCenterViewModel> AllFindDataCenters(string q, bool forautocomplete = false)
{
    return tms.DataCenters.Where(...)
                          .Select(x => new DataCenterViewModel
                                       {
                                           ...
                                           HasZones = x.Zones.Any(),
                                           HasTmsRouters = x.TMSRouters.Any(),
                                           HasTmsRacks = x.TMSRacks.Any()
                                       })
                          .ToList();

}

Then finally in your view

<td>
@Html.ActionLink("Edit", "Edit", new { id= item.ID}) 
@if (!item.AlreadyAssigned) 
{
    ...
}
</td>

Upvotes: 2

Related Questions