How to use interfaces like DbSet's in the Entity Framework in C #

2

Hello, I'm doing a solution where my entire business layer is in a separate project and in this project I created the relational layer structure from interfaces as shown in the example below:

// interfaces de definição de características 
public interface IHasIndex {
    int Id { get; set; }
}
...
// estrutura da aplicação
public interface IEmpresa: IHasIndex {
    string RazaoSocial { get; set; }
}
public interface IUsuario: IHasIndex {
    string Login { get; set; }
    string Senha { get; set; }
    IEmpresa Empresa { get; set; }
}
...
The intention is to take advantage of the multiple inheritance that the interfaces allow me and to standardize the nomenclature of the fields as well as to allow the use of the characteristic definition interfaces as specific selectors, generalizing the application, reducing code, standardizing methods, etc. .

My problem is the time to generate the templates for this solution. I'm working with Entity Framework Core and, even though it allows the use of interfaces in the definition of DbSet<T> (as shown #

[Table("Empresa")]
public class EmpresaModel: IEmpresa {
    [Key]
    public int Id { get; set; }
    public string RazaoSocial { get; set; }
}
[Table("Usuario")]
public class UsuarioModel: IUsuario {
    [Key]
    public int Id { get; set; }
    public string Login { get; set; }
    public string Senha { get; set; }

    public int EmpresaId { get; set; } // FK para Empresa
    [ForeignKey("EmpresaId")]
    public EmpresaModel Empresa { get; set; }

}

Defining that I'm going to use EmpresaModel as IEmpresa in class UsuarioModel , C # tells me that the interface has not been implemented. In my opinion, if EmpresaModel implements IEmpresa , then (theoretically) I could use the class instead of the interface. How to solve?

  

EDIT: To make the situation clearer based on what @MarconcilioSouza said about multiple inheritance, I will expand the IE interface with all the more attributes and inheritances ...

// interfaces de definição de características 
public interface IHasVisibility {
    bool Visivel { get; set; }
}
public interface IHasDefault {
    bool Padrao { get; set; }
}
public interface IHasCreationEvent {
    bool CriadoEm { get; set; }
}

// estrutura da aplicação
public interface IPessoaJuridica {
    string RazaoSocial { get; set; }
    string NomeFantasia { get; set; }
    string Cnpj { get; set; }
    string InscricaoEstadual { get; set; }
    string InscricaoMunicipal { get; set; }
}
public interface IPessoaFisica {
    string Nome { get; set; }
    string Sobrenome { get; set; }
    string Cpf { get; set; }
    string Rg { get; set; }
}
public interface IEndereco : IHasIndex {
    string Logradouro { get; set; }
    string Numero { get; set; }
    string Complemento { get; set; }
    string Bairro { get; set; }
    string Localidade { get; set; }
    string Uf { get; set; }
    string Pais { get; set; }
    string Cep { get; set; }
}
public interface IContato: IHasIndex {
    string Tipo { get; set; }
    string Descricao { get; set; }
    string Observacao { get; set; }
}
public interface IContatoWithDefault: IContato {
}
public interface IEmpresa : IHasIndex, IHasDefault, IHasCreationEvent, IHasVisibility {
    IPessoaJuridica PessoaJuridica { get; set; }
    IPessoaFisica PessoaFisica { get; set; }
    IEndereco Endereco { get; set;}
    ICollection<IContatoWithDefault> Contatos { get; set; }
}
    
asked by anonymous 17.10.2018 / 14:14

0 answers