Loading child objects based on a property value

Loading child objects based on a property value

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


msrs_it posted on Thursday, November 10, 2011

I have an Object "Company" having child objects "AutoDealer" and "BankAccounts".

The company object have some properties. In those, there are two properties "CompanyType" and "AccountType".

Both the child objects are of RelationTypes.Child | RelationTypes.Lazy

Now based on the companytype property the AutoDealer object should be instantiated.

In the same way the BankAccounts child object. Whenever the AccountType is set to Preferred Account then only the BankAccounts should be instantiated.

How can I form the BusinessRules so that the child objects are only available whenever the proper values are set on the related properties (CompanyType and AccountType)

StefanCop replied on Thursday, November 10, 2011

This sounds odd to me: loading through business rules. I fear having misunderstood your question.

Usually, while you fetch you have the values of CompanyType and AccountType read and can put that logic in the DataPortal_Fetch. And if you use lazy loading, you have UoW (ChildLoader) which do the load, and you can add your property there.

What do you mean with "only available"? Is not available an empty list or null. If you really want to load through BusinessRules, I would defined one on CompanyType as the primary property, getting the list through factory methods of AutoDealer and AddOutput to this affected/output property.

And a second BusinessRule for AccountType and BankAccounts.

msrs_it replied on Sunday, November 13, 2011

Hi Stefan,

stefan cop

This sounds odd to me: loading through business rules. I fear having misunderstood your question.

Usually, while you fetch you have the values of CompanyType and AccountType read and can put that logic in the DataPortal_Fetch. And if you use lazy loading, you have UoW (ChildLoader) which do the load, and you can add your property there.

What do you mean with "only available"? Is not available an empty list or null. If you really want to load through BusinessRules, I would defined one on CompanyType as the primary property, getting the list through factory methods of AutoDealer and AddOutput to this affected/output property.

And a second BusinessRule for AccountType and BankAccounts.

Thanks for your reply.

I'm sorry if my question is confusing. In our requirements we have a user registration use case. In that, there exists mandatory information that user has to provide at the time of registration. If the user selects the CompanyType as AutoDealer, then he has to enter the auto dealer information oherwise, that information is not required. In the same way he has to select the AccountType. (We have two types of accounts: Standard and Preferred). If the user selects the Preferred account then it's mandatory to enter at least One bank account information. This is the brief of the situation. Suggest me how can I implement the Business Rules/Events /any thing else that satisfies my requirement.

I'm unable to find the examples of UoW in Project Tracker. I hope you could you provide me the links.

Thanks & Regards,

SreeRam.

StefanCop replied on Monday, November 14, 2011

Jonny provided a good example of an AuthorizationRule, which depends on a property, in this post: http://forums.lhotka.net/forums/p/10868/50695.aspx#50695 .

 

And for 4.1 (I don’t know if it’s still true for 4.2), you should override CachResult to return false, because your rule should check a property’s value of the instances.

public override bool CacheResult { get { return false; } }

The Authorization is enforced in Get/SetProperty(). Your property can test this in advance; see ebook 2 Objects p. 123.

bool canRead = this.CanReadProperty(ProductEdit.CityProperty);

bool canWrite = this.CanWriteProperty(ProductEdit.CityProperty);

 

And the for example:

 

public AutoDealer AutoDealer

{

   get

   {

      if (CanReadProperty(Company.AutoDealerProperty))

         return GetProperty(Company.AutoDealerProperty);

      else

         return null;  // or whatever you want

   }

}

 

Another option is to Load the appropriate value in DataPortal_Fetch.

JonnyBee replied on Sunday, November 13, 2011

In that case you should probably not use RealationShipTypes.Lazy.

Typicall - Lazy loading means that your child objects are loaded when UI (or any other code) needs to get the list/child.

That is not quite what you need it seems as you only want to create the child object when a certain condition is met.

I would consider to use AuthorizationRules as an alternative so that you can "hide" the objects unless the specific condition is met.

IE, my recommendation

1. Create the objects whenever necessary (lazy loaded in the Property getter)

2. Use Authz rules to hide the actual BO unless state is correct. When an Authz rule says you can not read property the default value (should be an empty list/child) will be returned to the UI.

msrs_it replied on Sunday, November 13, 2011

Hi Jonny,

 I'm leaning towards the authorization rules. In that case I required an example of writing custom authorization rules. It would be help full to suggest the references where I can get the required info or examples. 

Thanks for your support.

Thanks & Regards,

SreeRam.

Copyright (c) Marimer LLC