Has anyone experience problems when inheriting from another custom class? Specifically, I have two classes (Customer and Person) that work when each is directly inheriting from BusinessBase. However, if I change Customer to inherit from Person then I loose all the properties when the object is back on the server.
Any help would be appreciated!
[
Serializable()]#region
Factory Methods public static Customer NewCustomer(){return DataPortal.Create<Customer>();} private Customer() : base(){
/* Require use of factory methods */ }#endregion
protected override object GetIdValue(){return this.Id;} private static PropertyInfo<Guid> IdProperty = RegisterProperty<Guid>(typeof(Customer), new PropertyInfo<Guid>("Id", "Id")); public Guid Id#region
Data Access[
RunLocal()]
[
Serializable()]WOW! Found two solution.
1. Override OnDeserialized in my base class OR
2. Replace the ManagedProperties with local variables (on the base class only)
Or
3. Implement a pattern to force the static fields of the base class to initialize. There are several techniques, I like this one the best (put this code in your base class):
private static int _dummy;
private BaseClassConstructor()
{
_dummy = 0;
}
This ensures that the static fields of the base class (the RegisterProperty() calls) are executed before any properties are manipulated on the business object.
I tried the static dummy method and it didn't work. After my object is sent to the Webservice/WcfService, my properties all return an Invalid cast exception. Uncommenting the OnDeserialized fixes the problem.
Here's my updated base class:
[Serializable()]
public abstract class Person
:BusinessBase<Person>
{
#region
Factory Methods#endregion
protected override object GetIdValue(){return this.Name;} private static PropertyInfo<string> NameProperty = RegisterProperty<string>(typeof(Person), new PropertyInfo<string>("Name", "Name")); public string Name{#region
Data Access protected override void DataPortal_Insert()Oh yeah, I forgot about that part – you need to set _dummy
in the OnDeserialized() override too.
Rocky
I didn't know this would work at all, since the Customer type isn't known to BusinessBase<T> and if you fetch using a Person.Criteria, you'll get a Person, not a Customer. (Of course, I suspose you could implement Customer.Criteria, etc.)
Is inheriting from BusinessBase<T> derived classes considered an OK/recommended practice?
That is not a guess. If you inherit from BusinessBase<T>
more than 1 level (in other words, you have your own custom base class) then
you MUST put the _forceInit code in every one of your classes.
Rocky
Copyright (c) Marimer LLC