0

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.

Net Dev
  • 416
  • 4
  • 21
  • 2
    What are the return types? Interfaces must match types exactly, not use sub-types. – D Stanley Mar 05 '13 at 14:34
  • That's because in your `InterfaceA` the `IEnumerable` contains an `interface`, but `TEntity` must be a `class`. – John Willemse Mar 05 '13 at 14:36
  • Here is your answer: http://stackoverflow.com/questions/2606461/problem-with-interface-implementation-in-partial-classes – Steve's a D Mar 05 '13 at 14:37
  • @JohnWillemse Why must TEntity be a class? – D Stanley Mar 05 '13 at 14:55
  • The original Class is a TEntity because I save the data to a database using entity framework. – Net Dev Mar 05 '13 at 15:21
  • @DStanley I looked up the definition of `EntityCollection` on MSDN, and there is says `... where TEntity : class`. [(Source)](http://msdn.microsoft.com/en-us/library/bb354106(v=vs.100).aspx) – John Willemse Mar 05 '13 at 15:28
  • I think this would be a good read. http://stackoverflow.com/questions/11512095/how-can-i-utilize-entitycollection-of-type-interface-using-a-partial-class-wit?rq=1 –  Mar 05 '13 at 16:22

2 Answers2

1

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()};
       }
   }
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240
1

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;
}
Legolas12
  • 26
  • 2
  • This seems to work but do you think this is a good way to go about abstracting? – Net Dev Mar 05 '13 at 15:28
  • The way I did this was to make a new collection of my desired signature and add the items i had in the old list. I couldnt do anything like collection = new collection( collection). – Net Dev Mar 05 '13 at 15:40
  • Is there even a way to do something like collection = new collection( collection) if Class b implements interfaceB? – Net Dev Mar 05 '13 at 15:54