Business base inheritanceBusiness base inheritance
Old forum URL: forums.lhotka.net/forums/t/5429.aspx
tchimev posted on Thursday, September 18, 2008
I'm trying to create an abstract class which inherits BB, like this:
public abstract class Abstr<T> : BusinessBase<Abstr<T>>
{
protected static PropertyInfo<int> _id = RegisterProperty<int>(new PropertyInfo<int>("ID"));
public int ID
{
get { return GetProperty<int>(_id); }
}
}
public class Test : Abstr<Test>
{
private static PropertyInfo<string> _name = RegisterProperty<string>(new PropertyInfo<string>("Name"));
public string Name
{
get { return GetProperty<string>(_name); }
set { SetProperty<string>(_name, value); }
}
}
But when I create a new instance of type Test the ID property of the abstract class does not load with default value.
When I try to use the ID property I get an exception( something like RegisterProperty() func does not work correctly).
What am I missing?
ajj3085 replied on Thursday, September 18, 2008
Off the top of my head, shouldn't it be:
public abstract class Abstr<T> : BusinessBase<T>
tchimev replied on Thursday, September 18, 2008
may be like this
public abstract class Abstr<T> : BusinessBase<T> where T : Abstr<T>
but that does not help
The error comes from the RegisterProperty() method.
tchimev replied on Thursday, September 18, 2008
Ok, I replaced my generics classes and it is working fine I think.
public abstract class Abstr : BusinessBase<Abstr>
{ }
public class Test : Abstr
{ }
But can someone explain why it is not working with generics?
thank you.
stefan replied on Friday, September 19, 2008
What you still need to implement is the 'dummy-workaround' as I call it.
Search the forum for '_dummy', especially in recent posts from Rocky.
In short:
If you have managed properties (e.g. using RegisterProperty) in any base classes,
then, in order to force RegisterProperty to be called in the right order (e.g. down-top),
you have to do the following in every base class using 'RegisterProperty:
- declare a static/shared dummy variable
private static int _dummy;
Private Shared _dummy As Integer
- initialize the dummy variable in the constructor
protected MyClass() {
_dummy = 0;
}
Protected Sub New()
_dummy = 0
End Sub
- override OnDeserialized, and initialize _dummy there too
protected override void OnDeserialized(System.Runtime.Serialization.StreamingContext context) {
base.OnDeserialized(context);
_dummy = 0;
}
Protected Overrides Sub OnDeserialized(ByVal context As System.Runtime.Serialization.StreamingContext)
MyBase.OnDeserialized(context)
_dummy = 0
End Sub
This is how I see it at the moment.
Stefan
Copyright (c) Marimer LLC