So basically I have an object with a few properties in it:
public MyObject
{
public string Name {get;set;}
public bool Complete {get; set;}
}
List<MyObject> myList = new List<MyObject>();
myList.Add(new MyObject("Page1", true));
myList.Add(new MyObject("Page2", false));
myList.Add(new MyObject("Page3", false));
Imagine a webpage. Load the list of pages above from the database and then render a link to each one.
The pages must be completed in order. So if a page is not complete then the next page cannot be edited.
So in this case page 1 is already complete. Page2 can be edited/completed. Page3 cannot be edited until Page2 has been completed.
I'm trying to decide the best way to implement - I have 3 options I am trying to decide between:
1) Change database query - The current query contains the name and the complete flag. I was thinking to just add a new flag which will pull the value from the previous row, like so:
SELECT Name, Complete, LAG(Complete) OVER (ORDER BY 1) as Editable
FROM @MyPages ORDER BY 1
But that is bordering on business logic in the database. I'm told that is bad.
2) Change the webpage View (MVC page)
Simplest way. When looping through the list, check the previous row.
But I'm also told business logic in the view is bad.
3) Some sort of new view model. But since the property is dependant on the previous object in a list I'm trying to find a 'nice' way of doing it.
Best I managed so far...
public class MyViewModel
{
public List<MyObject> _list;
public bool IsEditable(int index)
{
if (index > 0)
{
if (this._list[index-1].Complete)
return true;
return false;
}
else
return true;
}
}
All 3 of those will work.
But recently I've been making more of an effort to do things 'properly' rather than just hacking together the quickest or the first thing i think of.
-
Or am I better scrapping the whole object and starting from scratch?
I already have the list of pages taken from the database which was fine when we wanted to show everything and treat them equally. Now it's all screwed up..