Search code examples
c#db4o

DB4O: Need to use GenericList<T> outside of using block of connection


I am using DB4O to store my objects. Please find below code to retrieve objects from DBO4 DB.

public IList<T> GetList<T>()
        {
            IList<T> list = null;
            using (IObjectContainer db = Db4oEmbedded.OpenFile(Db4oEmbedded.NewConfiguration(), fullFilePath))
            {
                list = db.Query<T>(typeof(T));
            }
            return list;
        }

The question is, I CAN NOT apply C# 'foreach' loop outside of using block. It gives me an error "Exception of type 'Db4objects.Db4o.Ext.DatabaseClosedException' was thrown." once I start traversing my List outside of Using block

I am able to apply C# 'foreach' loop inside using block, but I wants to use my generic list to another code layer. So, I must need my generic list object outside of using block.

Please give me solution for this.

Thanks in advance.


Solution

  • You have two possibilities:

    1. handle the disposal of the db object yourself
    2. grab all data you need from the database (via ToList for example), which iterates the IEnumerable<T> and return that.