views:

114

answers:

1

I have a generic class that all my DAO classes derive from, which is defined below. I also have a base class for all my entities, but that is not generic.

The method GetIdOrSave is going to be a different type than how I defined SabaAbstractDAO, as I am trying to get the primary key to fulfill the foreign key relationships, so this function goes out to either get the primary key or save the entity and then get the primary key.

The last code snippet has a solution on how it will work if I get rid of the generic part, so I think this can be solved by using variance, but I can't figure out how to write an interface that will compile.

public abstract class  SabaAbstractDAO<T> :ISabaDAO<T> where T:BaseModel

    {
      ...

    public K GetIdOrSave<K>(K item, Lazy<ISabaDAO<BaseModel>> lazyitemdao) 
    where K : BaseModel
    {
         ...
    }

I am getting this error, when I try to compile:

 Argument 2: cannot convert from 'System.Lazy<ORNL.HRD.LMS.Dao.SabaCourseDAO>' to 'System.Lazy<ORNL.HRD.LMS.Dao.SabaAbstractDAO<ORNL.HRD.LMS.Models.BaseModel>>'

I am trying to call it this way:

        GetIdOrSave(input.OfferingTemplate,
            new Lazy<ISabaDAO<BaseModel>>(
                () =>
                {
                    return (ISabaDAO<BaseModel>)new SabaCourseDAO() { Dao = Dao };
                })
        );

If I change the definition to this, it works.

    public K GetIdOrSave<K>(K item, Lazy<SabaCourseDAO> lazyitemdao) where K : BaseModel

    {

So, how can I get this to compile using variance (if needed) and generics, so I can have a very general method that will only work with BaseModel and AbstractDAO<BaseModel>? I expect I should only need to make the change in the method and perhaps abstract class definition, the usage should be fine.

UPDATE: With a very helpful response I have a slightly improved example, but an interesting dilemna:

I have this defined now, and I don't have any in or out on T here because I get errors that contradict, if out T then I get that it must be contravariantly valid and if in T then covariantly valid, so I made it invariant, since it appears VS2010 can't figure it out.

public interface ISabaDAO<T> where T:BaseModel
{
    string retrieveID(T input);
    T SaveData(T input);
}

I get this error, though it did compile:

System.InvalidCastException: Unable to cast object of type 'ORNL.HRD.LMS.Dao.SabaCourseDAO' to type 'ORNL.HRD.LMS.Dao.ISabaDAO`1[ORNL.HRD.LMS.Models.BaseModel]'.

I fixed two code snippets above, but it appears that variance won't work as I hoped here.

I had tried this:

public delegate K GetIdOrSave<out K>(K item, Lazy<ISabaDAO<BaseModel>> lazyitemdao)
where K : BaseModel;

but I get the same problem as with the interface, if I put out it complains, so I put in and the opposite complaint.

I think I could get this to work if this was legal:

public delegate K GetIdOrSave<K>(in K item, out Lazy<ISabaDAO<BaseModel>> lazyitemdao)
where K : BaseModel;
+1  A: 

C# 4.0 support for covariance and contravariance when working with delegates and interfaces. http://stackoverflow.com/questions/245607/how-is-generic-covariance-contra-variance-implemented-in-c-4-0

So if you can use the generic delegate Lazy with a Interface as parameter so try somthing like this:

//covariance then you can save Giraffe as SicilianGiraffe but you cannot save Giraffe as Animal; contr-variance realization is not imposible in your case(just theoreticaly)
public interface ISabaDAO<out T> where T: BaseModel{
  int retrieveID(BaseModel);
  T SaveData(BaseModel);
}
public abstract class  SabaAbstractDAO<T> : ISabaDAO<T>{
  ...
  // in this case Lazy should be covariance delegate too
  // delegate T Lazy<out T>();
  public K GetIdOrSave<K>(K item, Lazy<ISabaDAO<BaseModel>> lazyitemdao) where K : BaseModel
  {
    ...
    return (K)itemdao.SaveData(item);// is not safe
    ...
  }
}
public class Course : BaseModel{}
public class SabaCourseDAO : SabaAbstractDAO<Course>{}

//so you can cast SabaCourseDAO to ISabaDAO<Course> and ISabaDAO<Course> to ISabaDAO<BaseModel>
// then next invoking should be valid
GetIdOrSave(new Course (), new Lazy<ISabaDAO<Course>>(() =>

                {

                    return new SabaCourseDAO() { Dao = Dao };

                })

Cannot check it. I have not got VS2010.

chapluck
You got me closer, thank you, but I am getting conflicting responses from the compiler. It will state T must be contravariantly valid, but if I change T to be 'in' then it states it must be covariant.
James Black
I have read about Lazy<T>. I saw it is a class from .NET Framework. So you have to use constructor of Lazy<ISabaDAO<BaseModel>> to invoke GetIdOrSave.
chapluck
And sorry i cant check all my code and move forward with your notice. So these are only my notes.
chapluck
No problem, you got me closer, but this may be a case where variance won't work, as I want to have one generic method, to reduce the code repetition, so it should work with any enitity of BaseModel and return any dao of type AbstractDAO, so it will work with all my classes. C# may not be the correct language for what I am trying to do. It would work I expect in IronPython and IronRuby, and perhaps F#.
James Black