0

I have the following class that suppose to create iterate through a string array to check if a code exists. However the .value always returns null when using Lazy Initialization.

public class LazyInclusionList
{
    private string docCopyCode;
    private Lazy<LazyInclusionList> _docCopyCodeList = null;
    public LazyInclusionList()
    { }

    public bool GetDocCodes(string docCopyNumber)
    {
        docCopyCode = new string(docCopyNumber.Where(Char.IsLetter).ToArray());
        _docCopyCodeList = new Lazy<LazyInclusionList>();
        bool docCopyCheck = false;
        int last = _docCopyCodeList.Value.GetDocCodesDB.Count();
        int i = 0;

        foreach (string code in _docCopyCodeList.Value.GetDocCodesDB)
        {
            if(docCopyCode == code)
            {
                docCopyCheck = true;
            }
            else if (docCopyCode != code && ++i == last)
            {
                docCopyCheck = false;
            }
        }
        return docCopyCheck;
    }

    private string[] codes;
    public string[] GetDocCodesDB
    {
        set
        {
            codes = value;
        }
        get { return codes; }
    }

}

I have the following test method that I use to check this code.

[TestMethod]
public void CheckURLList()
    {
        var list = new LazyInclusionList();
        string[] array = new string [3] { "CB", "DB", "T" };
        list.GetDocCodesDB = array;
        string myTest = "CB10/00/1";
        Assert.IsTrue(list.GetDocCodes(myTest));
    }

This is the first time I use this method and dont understand it fully.

Lulutho Mgwali
  • 823
  • 1
  • 11
  • 33
  • I don't see constructor or field/property initializer are doing anything, so how do you expect `GetDocCodes()` method is executed (not talking about your test, but about something what will use that type)? You are misunderstanding [`Lasy<>`](https://msdn.microsoft.com/en-us/library/dd997286(v=vs.110).aspx) pattern. Maybe if you explain in words what are you trying to achieve then it would be easier to answer. – Sinatr Mar 16 '17 at 12:31
  • The string array will be stored in the DB and do not want to make the trip every time it is needed, if it is at all (GetDocCodesDB). I am expecting for GetDocCodes to do the work(return true/false), and the lazy pattern to manage the call to GetDocCodesDB. I hope that clear.. – Lulutho Mgwali Mar 16 '17 at 12:53

1 Answers1

0

I am not able to recognize known patterns in your example and I decide to explain the idea in simple words.

The string array will be stored in the DB and do not want to make the trip every time it is needed

That's basically this

string[] _codes;
public string[] Codes
{
    get
    {
        if (_codes == null) // check if not initialized yet
        {
            _codes = ... // fill from database
        }
        return codes;
    }
}

As soon as you read Codes value for the first time its obtained and result is cached. null is used as special value to run initialization once (another bool field can be used if null is expected as result of _codes).


Lazy<> is doing the same (see this question for insights) and its used like this:

readonly Lazy<string[]> _codes = new Lazy<string[]>(() =>
{
    return ... // return here string[] which you fill from database
});
public string[] Codes => _codes.Value; // property used to get value

Note: initialization of Lazy<> (that lambda used to calculate its Value) will only run once, same as with caching example above. For initialization to occurs you only have to access Codes property once, any further call will return cached result.

Community
  • 1
  • 1
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • I am still somehow faced with the same issue where `_codes` is null even though I have given `string[] codes` values – Lulutho Mgwali Mar 16 '17 at 14:12
  • Then `null` is a result of what you read from database (the code what returns `string[]` returns `null`). Either set a breakpoint to see it or make some debug output. My concerns (and answer) were mostly of how you implement `Lazy<>`. – Sinatr Mar 16 '17 at 14:15