English 中文(简体)
补习方法检查的持久性
原标题:Last iteration of recursive method check

我有一种可再生的方法,即建立一个资源及其相关资源的类似树木的结构。

对于我与我一道工作的每一资源,我还要在一组成员名单内加一,我核对每个空缺,以确保我们不会明确地依靠相互依赖的资源。

每次我第一次提出这种休养方法时,我都需要清楚地列出该类成员名单。

此时,我有适当方法,在呼吁采用补习方法之间可以呼吁这样做。

我现在要删除这一方法,并每次自动重新编制清单。

目前,我可以找到两个办法解决这一问题:

  1. Test whether the calling method is the same as the currently executing method and if not, reset the list
  2. Get rid of the recursion and queue items instead, dequeueing and enqueueing as we go. At the end of the method call I can reset the list.

www.un.org/Depts/DGACM/index_spanish.htm 如何解决这一问题? 你们会采取什么办法?

我的法典目前如何看待:

public class GetAllRelatedResourcesByParentGuidQuery : IGetAllRelatedResourcesByParentGuidQuery
    {
        private readonly IList<Guid> _itemsCheckedForRelations = new List<Guid>();

        public IEnumerable<IDependency> Invoke(Guid parentCiId, 
                                                  IResoucesByIdQuery getResources)
        {
            if (!_itemsCheckedForRelations.Contains(parentCiId))
            {
                var relatedResources = getResources.Invoke(parentCiId);

                _itemsCheckedForRelations.Add(parentCiId);

                if (relatedResources.Count() > 0)
                {
                    foreach (var relatedResource in relatedResources)
                    {
                        relatedResource.Resource.DependentResources = Invoke(
                                                         relatedResource.Resource.Id, 
                                                         getResources);

                        yield return relatedResource;
                    }
                }
            }
        }

        public void ResetCheckedItemsCollection()
        {
            _itemsCheckedForRelations.Clear();
        }
    }
最佳回答

我将采用public方法,进行制作,但使recursive方法不谨慎,并将其作为参数。

public List<string> DoSomething(int input)
{
    List<string> results = new List<string>();
    DoSomethingImpl(input, results);
    return results;
}

private void DoSomethingImpl(int input, List<T> results)
{
    // For example...
    if (input == 0)
    {
        return results;
    }
    results.Add("Foo");
    DoSomethingImpl(input - 1, results);        
}
问题回答

Just make a method calling an inner method like this:

public class GetAllRelatedResourcesByParentGuidQuery : IGetAllRelatedResourcesByParentGuidQuery 
{ 
        private readonly IList<Guid> _itemsCheckedForRelations = new List<Guid>(); 

        public IEnumerable<IDependency> Invoke(Guid parentCiId,  
                                                  IResoucesByIdQuery getResources) 
        {
            Reset();
            return InternalInvoke(parentCiID, getResources);
        }

        private IEnumerable<IDependency> InternalInvoke(Guid parentCiId,  
                                                  IResoucesByIdQuery getResources) 
        {
             //actual implementation, when going recursive, call this internal method
        }
}

It seems to me that the List shouldn t be a class member but a parameter for the method you call... any direct call happens with null for this parameter (could even be a default!)... the method allocates the List in that case and on the recursive calls it just passes the allocated List...

您可以尝试利用回传的伊斯兰教表达方式:

public class GetAllRelatedResourcesByParentGuidQuery :
                    IGetAllRelatedResourcesByParentGuidQuery
{
    public IEnumerable<IDependency> Invoke(
            Guid parentCiId,
            IResoucesByIdQuery getResources)
    {
        var checkedItems = new List<Guid>();
        Func<Guid, IResoucesByIdQuery, IEnumerable<IDependency>> invoke = null;
        invoke = (pcid, gr) =>
        {
            if (!checkedItems.Contains(pcid))
            {
                checkedItems.Add(pcid);
                var drs = gr.Invoke(pcid).ToArray();
                foreach (var relatedResource in drs)
                {
                    relatedResource
                        .Resource
                        .DependentResources =
                            invoke(relatedResource.Resource.Id, gr);
                }
                return drs;
            }
            return Enumerable.Empty<IDependency>();
        };
    }
}

采用这种办法,可以同时发出多次呼吁,而不必有任何特别逻辑来澄清名单。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签