Net Dev
Net Dev

Reputation: 416

Implementing Interfaces with Collections and Generics

I am trying to do some abstraction in classes generated by entity framework class and have a setup like the following:

EntityClassA (Generated)
{
   EntityCollection<EntityClassB> EntityClassBs;
}

EntityClassB (Generated)
{
   (...)
}

Partial EntityClassA : InterfaceA
{
   (...)
}


Partial ClassB : InterfaceB
{
   (...)
}

InterfaceA
{
   IEnumerable<InterfaceB> EntityClassBs;
}

but I keep getting issues saying EntityClassA does not implement properly because the return types don't match on EntityClassBs.

UPDATE: My apologies, I did not intend to submit this question in this state. Updated example to include proper interfaceA property name and more detailed explanation. Keep in mind this is only an example and the nomenclature does not represent actual names.

What I am trying to do is I have a class library of wpf controls and a library for data. The WPF library references the Data library for one class that it uses to construct a custom table. So what I was trying to do was take the reliance of the data package by using Interfaces. Is there a way I can proceed like this or is there a more recommended way?

What I am seeing is that I need to match the signature of the interface property exactly and I cannot implement interfaces like that.

Upvotes: 0

Views: 128

Answers (2)

Legolas12
Legolas12

Reputation: 26

I think I understand what you are trying to do. Perhaps instead of altering the signature (Can't do) you can try to add another property that just returns the same property but with the signature you looking for.

Partial EntityClassA : InterfaceA
{
    IEnumerable<InterfaceB> CollectionEntityClassBs
    {
        get{ return (some cast or somthin)EntityClassBs;  }
    }
}

InterfaceA
{
   IEnumerable<InterfaceB> CollectionEntityClassBs;
}

Upvotes: 1

D Stanley
D Stanley

Reputation: 152596

You don't ask a question, nor do you give any details about what return types you're using, so based on your statement I'll pretend you're trying to do this:

Partial EntityClassA : InterfaceA
{
   IEnumerable<ClassB> IClassBs {get; set;}
}

Partial ClassB : InterfaceB
{
   (...)
}

InterfaceA
{
   IEnumerable<InterfaceB> IClassBs;
}

which is not valid. The InterfaceA interface specifies that IClassBs (poorly named property, by the way) returns a collection if InterfaceBs. Changing the method signature to return a collection of ClassBs does not match the interface definition.

Note that you can return an actual list of ClassBs without changing the return type:

Partial EntityClassA : InterfaceA
{
   IEnumerable<InterfaceB> IClassBs 
   {
       get
       {
           return new List<InterfaceB>() {new ClassB()};
       }
   }
}

Upvotes: 1

Related Questions