0
votes

Using Castle Windsor to manage Linq to SQL DataContext with lifetime of per web:

Component.For<MyDataContext>()
                    .ImplementedBy<MyDataContext>()
                    .LifestylePerWebRequest()

Problem is that I need to eager load some child collections and from time to time and cannot set the DataLoadOptions once the context has returned some results.

Setting load options is not allowed after results have been returned from a query.

Only option I can see is to set the options in the constructor:

public SomeController(MyDataContext context)
{
_context = context;

var options = new DataLoadOptions();
            options.LoadWith<MyEntity>(x => x.Children);
            _context.LoadOptions = options;
}

Any tips or advice on how to handle this situation greatly appreciated.

1

1 Answers

0
votes

L2S does not allow for modification of the fetch strategy after the context has been opened. No matter how you will have to work around this issue. Your solution will work, however another thing you can do is to derive the context and apply your eager loading in the derived one. This way you can inject an already set up instance in your controller.

You can also consider using a different ORM tool, one that supports modifying the load behaviour at a later stage (EF, OpenAccess, NHibernate). It shouldn't be too hard to convert if you have a small model.