tags:

views:

155

answers:

4

If I have a method that does something with multiple Subsonic ActiveRecords and doesn know what type exactly it is easy thanks to interfaces.

public void DoSomething(IActiveRecord item)
{
   // Do something
}

But what if you have a method and you don't know what Collection (e.g. ProductCollection) you get? How do I have to declare my Parameter? There is no IActiveList interface.

I tried it with an generic approach, but that doesn't compile.

public void Add<Titem, Tlist>(ActiveList<Titem, Tlist> list)
{
    foreach(IActiveRecord item in list)
    {
        // Do something
    }
}
+1  A: 

Maybe this will help you?

http://msdn.microsoft.com/en-us/library/d5x73970

public void Add<Titem, Tlist>(ActiveList<Titem, Tlist> list) where Titem : IActiveRecord

Dunno, I don't know anything about SubSonic. But I'm guess generic class constraints would do the trick :)

cwap
+1 for the link
SchlaWiener
+2  A: 

You could limit the parameter to be a BindingListEx (the base class for AbstractList) and which would would give you an enumerable list:

public void <T>(T list) where T : BindingListEx<IActiveRecord>
{
    foreach(IActiveRecord item in list)
    {

    }
}
Adam
A: 

Ok,

thanks to Adam an Meeh, here is how it works:

I have a generic

List<MyClass>

MyClass needs an IActiveRecord in the constructor. I want to be able to Add MyClass objects, IActiveRecord objects and Subsonic's ...Collections to the List:

public class MyList : List<MyClass>
{

    public void Add(IActiveRecord item)
    {
        this.Add(new MyClass(item));
    }

    public void Add<T>(BindingListEx<T> list) where T : IActiveRecord
    {
        list.ToList().ForEach(x => Add(new MyClass(x)));
    }

}
SchlaWiener
A: 

Use SubSonic.IAbstractList all kind of subsonic collections implement this interface.

Cheers

Could work too, but since IAbstractList does not provide an enumerator I still would have to cast it to a BindingListEx<T>, so I prefer the solution mentioned in my last post (using BindingListEx<T> with where...)
SchlaWiener