Hello DevExpress team!
I would like to ask you whether is is possible to fill CurrentObject and SelectedObjects in any event before an action is executed. This question was already asked and answered before , but I would like to make a step further. Since there is a lot of code in our ViewControllers depending on CurrentObject or SelectedObjects property, I would like to make sure that there is no other method than writing a method and replacing the occurrences of SelectedObjects and CurrentObject by my methods below instead of filling CurrentObject or SelectedObjects properties.
My methods are:
C#public static IEnumerable<object> GetSelectedObjects(this View view)
{
List<object> result = new List<object>(view.SelectedObjects.Count);
foreach (object obj in view.SelectedObjects)
{
result.Add(view.EnsureBusinessObject(obj, true));
}
return result;
}
public static object GetCurrentObject(this View view)
{
return view.EnsureBusinessObject(view.CurrentObject, true);
}
public static object EnsureBusinessObject(this View view, object obj, bool throwExceptionIfObjectByDataViewRecordCantBeFound)
{
object result = null;
if (obj != null)
{
if (obj is XpoDataViewRecord)
{
Type objectType = ((XpoDataViewRecord)obj).ObjectType;
XPClassInfo classInfo;
if (view != null && objectType != null && view.GetSession() != null)
{
classInfo = view.GetSession().GetClassInfo(objectType);
}
else
{
classInfo = null;
}
object persistentObject = null;
string keyPropertyName = null;
object keyPropertyValue = null;
if (classInfo != null)
{
keyPropertyName = classInfo.KeyProperty.Name;
keyPropertyValue = ((XpoDataViewRecord)obj)[classInfo.KeyProperty.Name];
if (keyPropertyValue != null)
{
persistentObject = view.ObjectSpace.GetObjectByKey(view.ObjectTypeInfo.Type, keyPropertyValue);
}
}
if (persistentObject != null)
{
result = persistentObject;
}
else if (throwExceptionIfObjectByDataViewRecordCantBeFound)
{
string exceptionMessage = "Record not found.";
throw new InvalidOperationException(exceptionMessage);
}
}
else
{
result = obj;
}
}
return result;
}