Inheriting a generated class

Inheriting a generated class

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


kendallsoft posted on Friday, March 02, 2007

Can anyone tell me why this won't work- I get

ProjectTracker.Library.Project.Project is inaccessible due to its protection level?? I want to add some methods to the Project class and not have them stepped on during the next code generation????

using System;

using System.Collections.Generic;

using System.Text;

namespace ProjectTracker.Library

{

public class _Project : Project

{

}

}

Michael Hildner replied on Friday, March 02, 2007

The constructor for Project is private, that's why you're getting that message.

I've never tried to derive from something that has a private constructor, so I'm not sure how to get around it.

JoeFallon1 replied on Sunday, March 04, 2007

You have to change it from Private to Protected in your Gen class.

Then when you inherit from it you do *not* need to add another constructor in your developer level class which simply calls MyBase.New.

The CSLA framework uses this Protected constructor to create an instance of the class and then call DataPortal_xyz for you.

You should standardize your object creation using the DataPortal. It keeps things clear - you know you will always have a Shared method to call DP_xyz and a single constructor used only by CSLA.

When you mix and match DP_Create and other constructors things just get messy.

Joe

 

kendallsoft replied on Monday, March 05, 2007

That did it- thanks to Joe and Michael- I was able to inherit the generated class and create special methods that remain there even after a regen of the project file. The key was to change the protection level of construtor to protected

Also I had to make the new class Serializable

using System;
using System.Collections.Generic;
using System.Text;
namespace ProjectTracker.Library

{

[Serializable()]

public class _Project: Project

{

public string SpecialMethod()

{

return "Still Available after regen";

}

}

}

 

and  also create a new guid so I could save the project: (The constructor in the Project.cs file)

protected Project()

{ /* require use of factory methods */

_id = Guid.NewGuid();

}

 

Now I can make this call-

_Project proj = new _Project();

proj.Name = "Mark's Project";

proj.Description = "My Project";

proj.Started = "1/1/2006";

proj.Save();

Response.Write(proj.SpecialMethod());

Thanks again

ajj3085 replied on Monday, March 05, 2007

Keep in mind that it might not be clear how to use the new class if you have not hidden the factory methods on the original project class.

If you want to codegen, you might want to consider making Project a partial class instead of subclassing it. 

kendallsoft replied on Monday, March 05, 2007

Good point- but my original thought was- a partil class in the app_code directory, but i was getting a persistent error when trying to save a project-

System.Web.Services.Protocols.SoapException: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Runtime.Serialization.SerializationException: Unable to find assembly 'App_Code.xcydwgk9,

Tried to publish so I could get a "HARD" dll but wouldn't go away-

/// <summary>

/// Summary description for _Project

/// </summary>

///

[Serializable()]

public partial class _ProjectExt: Project

{

public _ProjectExt()

{

//

// TODO: Add constructor logic here

//

}

public string SpecialMethod()

{

return "Still Available after regen";

}

}

xal replied on Monday, March 05, 2007

Well, the point is to use a partial class and _NOT_ inheritance... you're still using inheritance there.

Take a look at the approach taken by this codegen tool:

http://groups.google.com/group/CslaGenerator


Andrés

kendallsoft replied on Monday, March 05, 2007

ok- I get it. No Inheritance- In the project library I am able to add

Using System;

using System.Collections.Generic;

using System.Text;

namespace ProjectTracker.Library

{

public partial class Project

{

public string SpecialMethod()

{

return "Still Available after regen";

}

}

}

 

Here I get my special method- I did have to change the original Project class to partial as well- that is the part I didn't get originally.

 

Thanks for all the help working through this!

Bowman74 replied on Tuesday, March 20, 2007

Ajj,

Sorry to bring this thread back from the dead but I was wondering about how you use partial classes.  I'd like to use them in my generator but I found no good way to override them in the custom code piece of the partial class.  For example if I generate the public property declarations how do I later add custom code to the set method?  Is there a particular design that you use to get over this?

I'd like to keep my private attributes, well private, instead of protected but this one issue has kept me from generating partial classes.  The need to override bits and pieces of generated code just comes up too often.  So if you guys have developed a cool design pattern to work around it I'd sure like to know. Smile [:)]

Thanks,

Kevin

JoeFallon1 replied on Wednesday, March 21, 2007

I do not use partial classes - but I recall reading about overriding methods in a thread.

As I recall, the pattern is to set up a series of hooks in the code gen partial class that allow you to handle the proper event in your developer class. I don't recall the details but I am sure someone will share thier findings.

Joe

 

Bowman74 replied on Thursday, March 22, 2007

JoeFallon1:

As I recall, the pattern is to set up a series of hooks in the code gen partial class that allow you to handle the proper event in your developer class. I don't recall the details but I am sure someone will share thier findings.

Thanks Joe.  That was the only solution I could think of a well.  My problem with it is that the hooks are presumably initially generated in the override class which would make it a bit of a pain to add in another hook at a later date and regenerate because you would have to manually add the hook method to all the override partial classes you had previously generated.  Also there is a loss of control on when the hooks happen and probably a lot of unneeded calling into unused hooks.  Of course these are not insurmountable problems, more like annoyances.  Using inheritance has its annoyances as well such as scoping things so they can be accessed by subclasses.  So it sounds like a six of one, half dozen of another type of thing.  Too bad as I like the concept of partial classes but am unwilling to spend time changing my generator to trade one set of annoyances for a different but just as bad set of annoyances.

Smile [:)]

Thanks,

Kevin

 

JoeFallon1 replied on Thursday, March 22, 2007

Kevin,

I reached a similar conclusion once I saw how it needs to be done. I also have a lot invested in the inheritance model. So I really can't justify switching.

Also, I thought the big reason for partial classes was designer support for MS tools. I am not very clear as to the big benefit in other cases. If anyone has some good reasons to use partial classes I would like to hear them and learn something new.

Joe

Copyright (c) Marimer LLC