在某些条件下再次回调相同的方法

时间:2013-07-16 15:32:27

标签: c# asp.net-mvc

我有这个对象:

public class ObjectDisplay
{
    public int mMasterObjectID { get; set; }
    public ObjectInfo mMasterObject { get; set; }

    public int mChildObjectID { get; set; }
    public ObjectInfo mChildObject { get; set; }

    public ObjectDisplay()
    {
        mMasterObject = new ObjectInfo();

        mChildObject = new ObjectInfo();
    }
}

Object Display是一个分组主对象(始终存在的主要对象)的类,以及ObjectInfo.ObjectType不正常的子对象。

我有这种方法可以为我取这些物品:

public Object GetObjectDisplayByID(int _objectID)
{
    Object objectToReturn = new Object();

    if (objectToReturn.mMasterObject == null)
    {
        return null;
    }

    if (objectToReturn.mMasterObject.ObjectType != "Normal")
    {
        if (objectToReturn.mMasterObject == null && objectToReturn.mChildObject == null)
        {
            mObjectDAL.LinkObjects();

            return GetObjectDisplayByID(objectToReturn.mMasterObject.mObjectID);
        }

        if (objectToReturn.mMasterObject.mMasterObjectID != null)
        {
            // If we are here then the primary object is in fact a child object.
            // We need to swap the objects and fetch the primary object which will become the child object.

            ObjectInfo child = objectToReturn.mMasterObject;

            objectToReturn.mMasterObject = mObjectDAL.GetObjectByID((int)child.mMasterObjectID);

            objectToReturn.mMasterObjectID = objectToReturn.mMasterObject.mObjectID;

            objectToReturn.mChildObject = child;
            objectToReturn.mChildObjectID = child.mObjectID;
        }
        else
        {
            if (objectToReturn.mMasterObject.mChildObjectID == null)
            {
                // The links are not made yet and we would need to redo them.
                mObjectDAL.LinkObjects();

                return mObjectDAL.GetObjectDisplayByID(objectToReturn.mMasterObject.mObjectID);
            }

            objectToReturn.mChildObject = mObjectDAL.GetObjectByID((int)objectToReturn.mMasterObject.mChildObjectID);
            objectToReturn.mChildObjectID = objectToReturn.mChildObject.mObjectID;
        }
    }

    return objectToReturn;
}

我的主要问题是,通过这些线路,我已经做到了,如果还没有ID,则意味着链接尚未制作。我调用内部方法来做链接:

if (objectToReturn.mMasterObject.mChildObjectID == null)
{
    // The links are not made yet and we would need to redo them.
    mObjectDAL.LinkObjects();

    return mObjectDAL.GetObjectDisplayByID(objectToReturn.mMasterObject.mObjectID);
}

我想要的是再次实际调用相同的方法,以“重启”方法。我做得对吗?这就是我实际上所谓的“递归调用”相同的方法吗?

1 个答案:

答案 0 :(得分:1)

是您应该调用您的方法recursively如果您的代码中存在一个条件,如果满意,您希望再次执行方法中定义的相同步骤,除非满足退出条件。