Non-Generic Base class

Non-Generic Base class

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


Jeff Carley posted on Monday, June 01, 2009

I'm sure there have been plenty of posts about this, but I can't seem to find exactly what I'm looking for. The situation is, I have a abstract base class that uses managed backing fields. From that, I have several derived classes that use that same base class. For example:

Course --> ProjectBase

Program --> ProjectBase

The two derived classes (in this example, Course and Program) also use managed backing fields. I need the ProjectBase base class to be non-generic so that I may reference it in any given situation. For example.

ProjectBase prj = null;
if(expession)
prj = Course.NewCourse();
else
prj = Program.NewProgram();


The problem I'm having is that on one instance expression may be true and a new Course class is instantiated. No problem there. But on the next iteration of this code, expression is false, and I'll need to instantiate a new Program class. Thats where the problem happens. I get an error saying that the type initializer failed for the class Program. It won't let me Register the properties for Program because the base class ProjectBase has already been initialized. The exact code I'm executing to get this error is simple:

ProjectBase proj2 = Course.NewCourse();

ProjectBase proj1 = Program.NewProgram();

The error I get after executing the above code is:

{"Can not register property ProgramID after containing type (ProjectBase) has been instantiated"}

ProgramID is a field from the Program class.

I've included the StackTrace as well if it is any help.

at Csla.Core.FieldManager.PropertyInfoManager.RegisterProperty[T](Type objectType, PropertyInfo`1 info)
at Csla.BusinessBase`1.RegisterProperty[P](PropertyInfo`1 info)
at MyProject.Entities.Program..cctor() in Program.cs:line 15


Jeff Carley replied on Monday, June 01, 2009

Update: Overriding GetIdValue on ProjectBase causes the error. Why does this happen? But there is still something else going on. I comment that code out, and I still get the error.

More: Here's how to duplicate the error.

ProjectBase proj2 = OrgInfo.NewOrgInfo(); // works fine

ProjectBase proj1 = Program.NewProgram(); // works fine

proj2.Description = "Project description"; // comment this line out and the error goes away

ProjectBase proj3 = Course.NewCourse(); // error

Jeff Carley replied on Monday, June 01, 2009

Solution.

Thought I'd share the solution. I had to add the typeof(...) to each Register property for the concrete classes. For example:

public class Course : ProjectBase
{
private static PropertyInfo CourseIdProperty = RegisterProperty(typeof(Course), new PropertyInfo("CourseId", "Course Id"));
public Guid CourseId
{
get { return GetProperty(CourseIdProperty); }
}

}

This fixed the issue. Another approach I found was to make ProjectBase generic and add an IProject interface. Then I could reference the IProject interface. But this approach was much more advasive and then presented problems like not having the BusinessBase methods available to you. Anyway, I've included a link to the post that finally got me going in the right direction.

http://forums.lhotka.net/forums/thread/31674.aspx

ajj3085 replied on Tuesday, June 02, 2009

You may want to revisit the interface approach. You can have an interface "inherit" another interface so you get most of the BB functionality. So if you did this, you might get what you want:

public interface IProject : IEdtiableBusinessObject { // Other Csla interfaces, if needed
// your definition here
}

Copyright (c) Marimer LLC