CSLA 3.6 Abstract Class

CSLA 3.6 Abstract Class

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


lazaroms posted on Wednesday, June 30, 2010

Hi:

Once again, I am a rookie, please, need help.

I need to implement a csla abstract class Person, inheriting from Businessbase. Then I need to implement two derived classes inheriting from Person (User and Client).

How must I do that?

Thanks,

L.M.Santin

 

 

 

ajj3085 replied on Wednesday, June 30, 2010

First, I think it's important to step back and ask why you need to create a Person and have User and Client inherit it.  You'd want to inherit if you have some common behaviors (and not properties or other data) you wish to inherit.  Even if you have some common business rules, these can be shared without the need for inheritence.  Inheriting is something that must be considered carefully, as is a form of tight coupling.

You can do what you want, and its fairly easy.  Simply declare your Person class like this:

[Serializable] public abstract class Person<T> : BusinessBase<T> where T : Person<T> { } 

[Serializable] public sealed class Client : Person<Client> { }

That will enable your Save method to return the most derived type, and everything should work as you expect it to.  When doing this, if you're using the PropertyInfo<T> style of properties, remember to NOT use the RegisterProperty calls which take a Type as the first parameter. You'll only encounter problems hard to track down problems (which you can find by searching on the forum) , and that parameter is not required.

Also, you'll not that your final class isn't a generic one; that will be necessary for you to be able to use your classes in WinForms.  I'm not sure how or if other UIs care about binding to generic classes, but WinForms doesn't like it.

HTH

Andy

lazaroms replied on Thursday, July 01, 2010

Hi Andy:

First of all, thank you veru much for your response.

Could you please write that declaration code in VB.NET?

Why must be carefylly considered to implement the inheriting?  Will the inheritance bring some complication with CSLA?

 

Do you know if is there a CSLA Forum in spanish?

 

Thank you very much

 

 

ajj3085 replied on Thursday, July 01, 2010

I believe it's something like this:

<Serializable()>  _
Public MustInherit Class Person(Of T as {Person(Of T)}
    
Inherits BusinessBase
End Class
<Serializable()>  _
Public NotInheritable Class Client
    
Inherits Person(Of Client)
End Class

The decision to consider inherting is important because inheritience is a form of very tight coupling.  Tight coupling can reduce maintainablity, which is contrary to your goal (which is increasing maintainability).  That's why you should tend to favor composition over inheritence.

I thought some time ago there was a mention that there was a group in South America which was spanish speaking and focused on Csla.  I believe it was posted in the forum, so some searching may turn it up.

Andy

lazaroms replied on Friday, July 02, 2010

Thanks again Andy.

I'll go through the easier way. I'll don't define a class for person, just define a class for client and another one for user.

 

Thank you again.

 

jindalsyogesh replied on Tuesday, December 18, 2012

I am using CSLA 3.7. I have a similar scenario as above. In addition to the above, I need to create a BusinessListBase with Person as child. So, in the list class, I will either cast the child object to access the properties/methods of Client/User class or I can directly call the properties/methods if Person class has them. But with the above implementation I am unable to implement the list class because of <T>.

If the above is not possible, the other option I have is to create 2 separate list classes for Client and User (I actually have 3 classes in my case) and don't use inheritance. I actually didn't want to create 3 separate list classes and that was my sole reason to look into inheritance, so that I have one list class, one abstract class, and 3 classes which derive from the abstract class.

Thanks
Yogesh

JonnyBee replied on Friday, December 21, 2012

First - Please start a new thread rather than posting new questions on a tread that already has a verified answer.!!!!

Look at the Samples\Net\Cs\Polymorphism on how to achieve this.

Basically - what you need to do is add an Interface for the abstract generic class and use the interface as constraint on the BusinessList class.

jindalsyogesh replied on Friday, December 21, 2012

Thanks, I also figured that out. In future I will start a new thread.

aadseducation2 replied on Tuesday, December 25, 2012

Online .Net Classes - Build professional career in Asp.Net platform,with exclusive Asp.net training in hyderabad on international real time projects in .net and become professional web developer *** programmer in .Net technologies.

Copyright (c) Marimer LLC