Teimo
Teimo

Reputation: 113

CodeFirst - How do I create an Interface from multiple Classes with relations to each other?

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

Upvotes: 0

Views: 132

Answers (2)

Liel
Liel

Reputation: 2437

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()
    { 
    }
}

Upvotes: 1

Sharpy
Sharpy

Reputation: 71

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

Upvotes: 0

Related Questions