Is there a better way to write it? I need the best way to write it. It is this principle? Please show me a better way
it breaks SRP?
public class PlacementLocation
{
public int Row { get; set; }
public int Column { get; set; }
public int Location { get; set; }
}
public class PlacementLocations : PlacementLocation
{
private void SetLocationInList()
{
_placementLocations = new List<PlacementLocation>
{
new PlacementLocation {Column = 1, Row = 9, Location = 1},
new PlacementLocation {Column = 2, Row = 9, Location = 2},
new PlacementLocation {Column = 3, Row = 9, Location = 3},
new PlacementLocation {Column = 4, Row = 9, Location = 4},
new PlacementLocation {Column = 5, Row = 9, Location = 5},
};
}
private List<PlacementLocation> _placementLocations;
public PlacementLocations()
{
_placementLocations = new List<PlacementLocation>();
SetLocationInList();
}
public Tuple<int, int> this[int location]
{
get { return new Tuple<int, int>(_placementLocations[location].Row, _placementLocations[location].Column); }
}
public int this[int row, int column]
{
get
{
PlacementLocation singleOrDefault =
_placementLocations.SingleOrDefault(d => d.Column == column && d.Row == row);
return singleOrDefault != null ? singleOrDefault.Location : 0;
}
}
}
Update :
public class PlacementLocations
{
#region Variables
private readonly List<PlacementLocation> _placementLocations;
#endregion
#region Constructor
public PlacementLocations(List<PlacementLocation> placementLocations)
{
_placementLocations = placementLocations;
}
#endregion
#region Public Methods
public Tuple<int, int> FindRowColumn(int location)
{
return GetRowColumn(location);
}
public int FindLocation(int row, int column)
{
return GetLocation(row, column);
}
#endregion
#region Indexes
public Tuple<int, int> this[int location]
{
get { return GetRowColumn(location); }
}
public int this[int row, int column]
{
get { return GetLocation(row, column); }
}
#endregion
#region Private Methods
private Tuple<int, int> GetRowColumn(int location)
{
return new Tuple<int, int>(_placementLocations[location].Row, _placementLocations[location].Column);
}
private int GetLocation(int row, int column)
{
PlacementLocation singleOrDefault =
_placementLocations.SingleOrDefault(d => d.Column == column && d.Row == row);
return singleOrDefault != null ? singleOrDefault.Location : 0;
}
#endregion
}
public class LocationsFactory
{
public static PlacementLocations GetPredefinedLocations()
{
var placementLocationList = new List<PlacementLocation>
{
new PlacementLocation {Column = 1, Row = 9, Location = 1},
new PlacementLocation {Column = 2, Row = 9, Location = 2},
new PlacementLocation {Column = 3, Row = 9, Location = 3},
new PlacementLocation {Column = 4, Row = 9, Location = 4},
new PlacementLocation {Column = 5, Row = 9, Location = 5},
};
return new PlacementLocations(placementLocationList);
}
}