Today I have coded a simple function that will get a room model, if its already been loaded in to the dictionary then it will just return it, if its not in the dictionary if will try its best to load it from the database. I just wondered is this the best way I can do ths? or is there a better way...
public bool TryGetModel(string modelName, out RoomModel model)
{
if (_roomModels.ContainsKey(modelName))
{
_roomModels.TryGetValue(modelName, out model);
return true;
}
using (var mysqlConnection = Sahara.GetServer().GetMySql().GetConnection())
{
mysqlConnection.SetQuery("SELECT id,door_x,door_y,door_z,door_dir,heightmap,public_items,club_only,poolmap,`wall_height` FROM `room_models` WHERE `custom`");
var modelRow = mysqlConnection.GetRow();
if (modelRow == null)
{
model = null;
return false;
}
model = new RoomModel(Convert.ToInt32(modelRow["door_x"]), Convert.ToInt32(modelRow["door_y"]),
Convert.ToDouble(modelRow["door_z"]), Convert.ToInt32(modelRow["door_dir"]),
Convert.ToString(modelRow["heightmap"]), Convert.ToString(modelRow["public_items"]),
Convert.ToInt32(modelRow["club_only"]).ToString() == "1", Convert.ToString(modelRow["poolmap"]),
Convert.ToInt32(modelRow["wall_height"]));
_roomModels.Add(Convert.ToString(modelRow["id"]), model); // save it for next time!!
return true;
}
}
modelRow? There is a (extension?) method onDataRowcalledField<T>that can be used in lieu of all theConvertcalls, assuming that the data isn't purely strings. Not sure if that applies here because I don't know your actual data and I don't know if the corresponding mysql data type (if not DataRow) supports this. \$\endgroup\$