0

please assume to have to classes, implementing their interfaces:

public interface ICube
{
    ICollection<IColor> sideColors {get; set; }
}

public interface IColor
{
    float getHue ();
}

public class Cube : ICube
{
    public ICollection<IColor> sideColors {get; set; }
}

public class Color : IColor
{
    public Color (float r, float g, float b)
    {
        ...
    }

    public float getHue ()
    {
        ...
    }
}

Now i want to expose the interface to another assembly, this assembly must not know anything about the implementation of the model. Beside that, i want to use the model for definition of database design with CodeFirst. In this case, EF can not use the definition of the ICollection, but needs a definition like ICollection. What is the best practice to satisfy both requirements: - Masking of implementation of class Cube - Use the model for EF

Best regards, Teimo

Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
Teimo
  • 113
  • 7
  • 1
    EF can and should handle ICollection of Objects, but it cannot handle ICollection of interfaces. It needs to know how to create the columns in the DB. – Liel Jun 13 '13 at 14:13
  • 1
    abstract classes instead of interfaces should do the trick – hubson bropa Jun 13 '13 at 15:04
  • Possible duplicate: http://stackoverflow.com/questions/9805329/how-to-use-interface-properties-with-codefirst?rq=1 – Liel Jun 13 '13 at 15:56

2 Answers2

1

Your basic entity classes should not implement or be used as interfaces.
Instead, they should be simple POCOs:

public class Cube
{
    public ICollection<Color> sideColors {get; set; }
}
public class Color
{
    public int SomeProperty {get; set;}
    public Color(float r, float g, float b)
    {
    }
}

Note that ICollection<Color> is valid for EF, but ICollection<IColor> isn't.

If you want to share properties between classes, use Abstract Base Classes instead of interfaces.

If you want some to perform some kind of actions and calculations on the basic entities, write separate classes, which can inherit from interfaces

public class HandleColor : IHandleColor
{
    public float getHue()
    { 
    }
}
Liel
  • 2,407
  • 4
  • 20
  • 39
0

If you mark the interface with the Public modifier and the implementation with the Internal Keyword only the Interface will be visible to another assembly referencing this one. e.g.

public interface ICube
{
    ICollection<IColor> sideColors { get; set; }
}

public interface IColor
{
    float getHue();
}

internal class Cube : ICube
{
    public ICollection<IColor> sideColors { get; set; }
}

internal class Color : IColor
{
    public Color(float r, float g, float b)
    {
    }

    public float getHue()
    { 
    }

}

Unless I am misunderstanding your requirements

Sharpy
  • 71
  • 5
  • My intention is to hide the implementation of Color from another assembly. Interface and implementation are in two different assemblies in my case. The problem is to expose the function via the interface but use its implementation on the other hand for code first. Lial wrote: "Note that ICollection is valid for EF, but ICollection isn't.". To expose function in interface, i need the second alternative, while for CodeFirst the first one is valid. This is exactly my problem and is still do not know how to solve this. – Teimo Jun 14 '13 at 06:22