I have a select method for ObjectDataSource:
public static IEnumerable<Model.Domain.Theme> Select()
{
var cycleRepo = new RbaCycleRepository(Global.sessionFactory.GetCurrentSession());
RbaCycle lacOpenCycle = cycleRepo.FindLacOpenCycle();
if (lacOpenCycle != null)
{
var themeRepo = new ThemeRepository(Global.sessionFactory.GetCurrentSession());
var result = themeRepo.FindAll(new ThemesForCycle(lacOpenCycle).GetQuery());
return result;
}
return Enumerable.Empty<Model.Domain.Theme>();
}
And here is the scenario:
- I click on the button, some actions are performed and as a result I end up with a few proxy Theme objects in 1st level cache - which is fine.
- Select() method is called and it returns result. The result can contain only Theme objects or a mix of Theme and NHibernate Castle Proxy (for instance). This is a trace from watch window:
{Castle.Proxies.ThemeProxy} Model.Domain.Theme {Castle.Proxies.ThemeProxy} {Model.Domain.Theme} Model.Domain.Theme
- If the first object in the result collection is actual Theme object, then binding of the whole collection succeedes. But if first element in collection is Proxy object, then I end up with exception:
Unhandled exception: System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Reflection.TargetInvocationException: Property accessor 'Title' on object 'CIPNet.Model.Domain.Theme' threw the following exception:'Object does not match target type.' ---> System.Reflection.TargetException: Object does not match target type.
EDIT: This is FindAll implementation:
public IList<T> FindAll(QueryOver<T, T> query)
{
return query.GetExecutableQueryOver(session).List();
}