0

There is an DLL named DbContract that contains, inter alia, these interface (code is mostly in polish):

public interface IKategoria
{
  string Nazwa { get; set; }
  IGrupa Grupa { get; set; } 
  ICollection<IKategoriaUzytkownik> Uzytkownicy { get; set; }
  ICollection<IZdarzenie> Zdarzenia { get; set; }
}

Then I want use Entity Framework repositories in another DLL (its not a web application, just a library that knows how to save objects to a database via facede of repositories). So I’m implementing concrete classes for it:

public class Kategoria : BaseObject, IKategoria
{
    private string _Nazwa; 
    private IGrupa _Grupa; 
    private ICollection<IKategoriaUzytkownik> _Uzytkownicy;
    private ICollection<IZdarzenie> _zdarzenia;

    public string Nazwa
    {
        get { return _Nazwa; }
        set { _Nazwa = value; }
    public Guid GrupaId { get; set; }
    public virtual IGrupa Grupa
    {
        get { return _Grupa; }
        set { _Grupa = value; }
    }
    public virtual ICollection<IKategoriaUzytkownik> Uzytkownicy
    {
        get { return _Uzytkownicy; }
        set { _Uzytkownicy = value; }
    }


    public virtual ICollection<IZdarzenie> Zdarzenia
    {
        get { return _zdarzenia; }
        set { _zdarzenia = value; }
    }

}

So as you can see i need to use IKategoria, IKategoriaUzytkownicy or IZdarzenie (these are all interfaces) so I can’t use this class for Entity Framework because EF need concrete classes to save them in DB (it needs navigation properties).

So my question is: How can I deisgn my architecture so I can use interfaces in applications (web, desktop) and still be able to use Entity Framework as ORM? I don’t want to bend all my Interfaces just because of Entity Framework.

Here is very simplified UML diagram of this how it should look like.How it should be

2 Answers 2

1

Type casting saved the hard work. Repository.Save((Kategoria)IKategoria);

If not like type casting or it is not that simple, convert that interface first into a class then save it. Please note I have long forgotten about the EF syntax though.

public void Save(IKategoria iKategoria){
    Kategoria kategoria = new IKategoriaToKategoriaConverter().Convert(iKategoria);
    Repository.Save(kategoria);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Finally i ended up with this sollution:

public partial class GrupaEF : BaseObjectEF
{


    private ICollection<KategoriaEF> _Kategorie;
    private ICollection<UzytkownikGrupaEF> _Uzytkownicy;


    public virtual ICollection<KategoriaEF> KategorieEF
    {
        get { return _Kategorie; }
        set { _Kategorie = value; }
    }
    public virtual ICollection<UzytkownikGrupaEF> UzytkownicyEF
    {
        get { return _Uzytkownicy; }
        set { _Uzytkownicy = value; }
    }


}

another file:

 public partial class GrupaEF : IGrupa
{

    public string Nazwa
    {
        get
        {
            return this.Nazwa;
        }
        set
        {
            this.Nazwa = value;

        }
    }

    public string Opis
    {
        get { return Opis; }
        set { Opis = value; }
    }
    [NotMapped]
    public ICollection<IKategoria> Kategorie
    {
        get
        {
            return (ICollection<IKategoria>)this.KategorieEF;
        }
        set { KategorieEF = (ICollection<KategoriaEF>)value; }
    }
    [NotMapped]
    public ICollection<IUzytkownikGrupa> Uzytkownicy
    {
        get { return (ICollection<IUzytkownikGrupa>)this.UzytkownicyEF; }
        set { UzytkownicyEF = (ICollection<UzytkownikGrupaEF>)value; }
    }

}

So now i can use right property in code by using not mapped properties and still let EF map whole object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.