LoadProperty not "loading" the property.

LoadProperty not "loading" the property.

Old forum URL: forums.lhotka.net/forums/t/7808.aspx


Dane posted on Saturday, October 17, 2009

I'm sure I'm missing something but I'm not sure what it is.

 

Business.dll

[Serializable()]

[Csla.Server.ObjectFactory("DataAccess.MenuItemFactory, DataAccess")]

public class MenuItem : BusinessBase<MenuItem>

{

public static PropertyInfo<string> DescriptionProperty = 

    RegisterProperty<string>(m => m.Description);

private string _description = DescriptionProperty.DefaultValue;

public string Description{

get { return GetProperty(DescriptionProperty, _description);}

      set { SetProperty(DescriptionProperty, ref _description, value); }}

}

 

DataAccess.dll

public class MenuItemFactory : ObjectFactory

{

 

public MenuItem Fetch(SingleCriteria<MenuItem, int> criteria)

{

      var efMenuItem = context.MenuItem... // Entity Framework to SQL DB query

MenuItem menuItem = (MenuItem)Activator.CreateInstance(typeof(MenuItem), true);

LoadProperty(menuItem, MenuItem.DescriptionProperty, efMenuItem.Description);

MarkOld(menuItem);

return menuItem

}

}

 

From a console app that references both these projects

MenuItem menuItem = MenuItem.GetMenuItem(1);

 

At this point menuItem.Description is empty string.  It should not be.

 

Using debug I’ve confirmed that in MenuItemFactor.Fetch efMenuItem.Description has a value at the time the LoadProperty method is called however GetMenuItem returns a MenuItem with an empty Description.  What am I missing?

 

 

JonnyBee replied on Sunday, October 18, 2009

Hi,

LoadProperty is only intended for ManagedProperties and does not work with private backing fields. Your DAL must set the private variable, preferrably by using:

using (BypassPropertyChecks(menuItem) {
      menuItem.Description = efMenuItem.Description;
}

BypassProprtyChecks makes the Set Property in your BO work similar to LoadProperty.

So your code loads a managed property but the property get returns a private backingfield that is still empty or null.

AKaplan replied on Wednesday, December 09, 2009

Can you explain this further? I'm having the same problem, but I don't have this "BypassPropertyChecks" method. What version of csla is this referring to?

ajj3085 replied on Wednesday, December 09, 2009

Well, BypassPropertyChecks is a property on the BO instance you're trying to load.  So usually its using ( menuitem.ByPassPropertyChecks ) { // load here }.

AKaplan replied on Monday, December 14, 2009

actually what fixed it was making the variable private _ID as guid=guid.empty for example a nullable variable. Like so, private _ID as Nullable(of GUID)=Guid.Empty

Copyright (c) Marimer LLC