简体   繁体   English

避免堆栈溢出异常

[英]Avoiding Stack Overflow Exception

I am stuck with a stack overflow exception in this section of code, it is obviously occurring because the Customer object calls a list of CustomerBackgroundLevel objects, each one of which creates a new customer object. 我在这段代码中遇到了堆栈溢出异常,显然是因为Customer对象调用了CustomerBackgroundLevel对象列表,每个对象都创建一个新的customer对象。 I am trying to find a way around the issue, any help would be appreciated.. 我试图找到解决问题的方法,任何帮助将不胜感激..

Customer Constructor - 客户构造函数 -

public CustomerVO(Customer item)
    {
        CustomerID = item.CustomerID;
        CustomerName = item.CustomerName;
        ECNNumber = item.ECNNumber;

        CustomerBackgroundLevels = item.CustomerBackgroundLevels.Select(c => new CustomerBackgroundLevelVO(c)).ToList();
    }

Customer Background Level Constructor - 客户背景级别构造函数 -

        public CustomerBackgroundLevelVO(CustomerBackgroundLevel item)
    {
        CustomerBackgroundLevelID = item.CustomerBackgroundLevelID;
        CustomerID = item.CustomerID;
        BackgroundLevelID = item.BackgroundLevelID;
        StartDate = item.StartDate;
        EndDate = item.EndDate;
        Customer = new CustomerVO(item.Customer);
        BackgroundLevel = new BackgroundLevelVO(item.BackgroundLevel);
    }

Customer Get Method - 客户获取方法 -

        public CustomerVO GetByID(int id)
    {
        var item = repository.AsQueryable().Where(x => x.CustomerID == id).FirstOrDefault();
        if (item == null)
            return null;

        return new CustomerVO(item);
    }

Yeah, as you've stated creating new objects in a loop like that is gonna lead to nothing good. 是的,正如你所说的那样在一个循环中创建新对象会导致没什么好处。

Instead of creating all these wrapper objects in your constructors, why don't you wrap them on demand? 而不是在构造函数中创建所有这些包装器对象,为什么不根据需要包装它们? That is, when you execute some code that needs a CustomerVO object, create the CustomerVO object within that function and then let it go out of scope when the function ends. 也就是说,当您执行一些需要CustomerVO对象的代码时,在该函数中创建CustomerVO对象,然后在函数结束时让它超出范围。

You can solve your loop like this: 你可以这样解决你的循环:

public CustomerVO(Customer item)
{
    CustomerID = item.CustomerID;
    CustomerName = item.CustomerName;
    ECNNumber = item.ECNNumber;

    **CustomerBackgroundLevels = item.CustomerBackgroundLevels.Select(c => new CustomerBackgroundLevelVO(c,this)).ToList();
}

**public CustomerBackgroundLevelVO(CustomerBackgroundLevel item, CustomerVO vocustomer)
{
    CustomerBackgroundLevelID = item.CustomerBackgroundLevelID;
    CustomerID = item.CustomerID;
    BackgroundLevelID = item.BackgroundLevelID;
    StartDate = item.StartDate;
    EndDate = item.EndDate;
    **Customer = vocustomer;
    BackgroundLevel = new BackgroundLevelVO(item.BackgroundLevel);
}

Is that a copy constructor? 那是一个复制构造函数吗? If so, you need to create a custom constructor for copying the item rather than using it in both scenarios where you new up an object and copy it. 如果是这样,您需要创建一个自定义构造函数来复制项目,而不是在新建对象并复制它的两种情况下使用它。

return new CustomerVO(item);

The above is unnecessary and the problem line is: 以上是不必要的,问题在于:

Customer = new CustomerVO(item.Customer);

Change the above line to this: 将上面的行更改为:

Customer = item.Customer;

Unless you are having reference problems, which means you need to design a new constructor. 除非你有参考问题,这意味着你需要设计一个新的构造函数。

And if the item.Customer object is not a CustomerVO object, then you will need to pass the reference of the current CustomerVO object into the constructor of the CustomerBackgroundLevelVO. 如果item.Customer对象不是CustomerVO对象,则需要将当前CustomerVO对象的引用传递给CustomerBackgroundLevelVO的构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM