CSLA inheritance problem

CSLA inheritance problem

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


kucing posted on Friday, May 26, 2006

I have inheritance that looks like this:
Base class: animal
Children classes: cat, dog

My animal class looks like this (some codes omitted):

public class Animal<T> : BusinessBase<T> where T : Animal<T>
{
    ...
}

Cat looks like this:
public class Cat : Animal <Cat>
{
    [Serializable()]
    private class Criteria {}

    [RunLocal()]
    private void DataPortal_Create(Criteria criteria){}

    private void DataPortal_Fetch(Criteria criteria) {}
        
    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_Insert() {}

    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_Update() {}
    
    [Transactional(TransactionalTypes.TransactionScope)]
    private void DataPortal_Delete(Criteria criteria) {}
}

So the only functions that Cat has are the data access ones.

Class Animal contains:

[RunLocal()]
public static T NewAnimal()
{
    if (!CanAddObject())
    {
        throw new System.Security.SecurityException("User not authorized to breed an animal");
    }
    return DataPortal.Create<T>();
}

public static T GetAnimal(Guid id)
{
    if (!CanGetObject())
    {
        throw new System.Security.SecurityException("User not authorized to view an animal");
    }
    return DataPortal.Fetch<T>(new Criteria(id));
}

public static void DeleteAnimal(Guid id)
{
    if (!CanDeleteObject())
    {
        throw new System.Security.SecurityException("User not authorized to kill an animal");
    }
    DataPortal.Delete(new Criteria(id));
}

public override T Save()
{
    ...
    return base.Save();
}

Whenever I call Animal.NewAnimal to create a new animal, it works nicely. Saving also works.

However, if I try to fetch or delete, I always get this error message:
Cannot create an instance of Animal`1[T] because Type.ContainsGenericParameters is true.
on DataPortal.Fetch and DataPortal.Delete.

What is happening?

glenntoy replied on Friday, May 26, 2006

Hmm.. try removing your constraints in your Animal class first and see what happens if you have the same error.

xal replied on Friday, May 26, 2006

Your criteria is in your animal class? Dataportal gets the type you want to load from the criteria. Although it should pay attention to the generic param in Fetch<whatever>(). I think Rocky said he'd fix that.

Also, if you're trying to create an instance of the animal class (even unvoluntarily), you can't because it has generic parameters that you need to provide. Do your subclasses define criteria and their own dataportal methods, or you're using the base class's criteria and methods?

Are you by any chance doing something like Animal.GetAnimal(id) and expecting it to return the correct type?

Andrés



RockfordLhotka replied on Friday, May 26, 2006

If you want Criteria to be in your base class, you need to have it inherit from CriteriaBase. This allows you to specify the specific type of object the data portal should create.

kucing replied on Friday, May 26, 2006

Glenntoy: I tried removing the constraint before and I am sure it will throw some errors about not being able to use BusinessBase functions since T is not restricted.

xal: I never try creating animal object. The subclasses does not define their own criteria but they have the data portal methods. I always create cat and dog by: Cat obj = Cat.NewAnimal();

xal + Rocky: Yes, my criteria is in my base class. I had suspicion that somehow criteria is causing the problem but I did not know how to fix it.

So to fix my problem I have to make sure that Criteria inherits from CriteriaBase (since it is located in class Animal)? I'll try that next week! :)

Thanks for the replies guys!

tsaltd replied on Monday, January 08, 2007

I suppose the answer is "it depends on the Use Case", but I'll ask anyway .... are there pros / cons re: maintaining an Animal.Type in the Animal class ?? 

The Type would be a literal "cat" or a code that would reference a lookup table of Animal Types.

Any debate on the subject or an accepted best practice ???

Thanks,


Steve

ajj3085 replied on Tuesday, January 09, 2007

It does depend on the use case.  If a Cat object will have different behaviors than a Fish object (for example, they both implement Move, but differently) then you'll want subclasses or possibly seperate classes (an Animal would becoming IAnimial).  If the behavior of your Cat and Fish class will be the same then a type property + NVL would be more appropriate.

thornfield_he replied on Thursday, July 21, 2011

I am confused on how to implement generic inheritance in csla.net, since read your code, I understood how.Thank you!

Copyright (c) Marimer LLC