0

I am a student making ASP NET Core 3.1.1 WebAPI on top of NorthwindDB. Tables ID properties have unique names. Is there a way to apply generic repository pattern in such case without it becoming too complicated?

public interface IEntity
{
   int id {get; set;}     //won't work, Id: s have unique names like 'int ProductId'
}

public interface IEntity
{
   int ProductId {get; set;}  //allows only 'Products' class to inherit
}


public interface IEntity
{
   int ProductId {get; set;}
   string EmployeeId {get; set;}   //won't work, every class should have props listed here then 
}
Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
TommiAL
  • 29
  • 3
  • You should definitely read Gunnar Peipman's [No need for repositories and unit of work with Entity Framework Core](https://gunnarpeipman.com/ef-core-repository-unit-of-work/), which explains the problems (serious *correctness* problems, not just performance issues) caused by generic repositories – Panagiotis Kanavos Mar 16 '20 at 21:06
  • In all ORMs, you can specify the Primary Key property by convention, through attributes or configuration (even fluent configuration). They are specifically built to *not* need extra interfaces. – Panagiotis Kanavos Mar 16 '20 at 21:08
  • The "generic" repository is actually an *anti*-pattern when ORMs like NHibernate or EF Core are available – Panagiotis Kanavos Mar 16 '20 at 21:16
  • Agree with the sentiments here. I've gone down the rabbit hole of trying to build a generic repository in EF Core and it ended up being a pointless waste of time and resources. – Valuator Mar 16 '20 at 21:25
  • Have a look at [this](https://stackoverflow.com/a/51781877/5779732) answer for more discussion about Generic Repository with full ORMs. – Amit Joshi Mar 17 '20 at 05:36
  • 1
    This is great! I can't even imagine how long it would have taken me to figure out all those points of view mentioned in discussions by myself. This got me thinking that the database should be configured for repository pattern in mind from beginning for to get real benefits out of it. Also "Relying to much to ORM leads to poorly designed databases." – TommiAL Mar 17 '20 at 19:37

2 Answers2

0

You can implement your interface even if the class has a different property name for itself.

public interface IEntity
{
    int Id { get; set; }
}

public class Product : IEntity
{
    public int Id
    {
        get => this.ProductId;
        set => this.ProductId = value;
    }
    public int ProductId { get; set; }
}
Chronicle
  • 1,565
  • 3
  • 22
  • 28
  • This doesn't offer anything, never mind that ASP.NET Core isn't an ORM. ORMs like Entity Framework work with POCOs- plain old C# objects, they don't need *extra* interfaces – Panagiotis Kanavos Mar 16 '20 at 21:03
  • Thanks, this works. Even though Northwind is not an optimal DB to use generic repository pattern with, which i know now, i can test it with this. – TommiAL Mar 17 '20 at 19:43
0

If you use Ef core in your ASP.NET Core project, you can map table ID column to Id property of Entity in DbContext.

modelBuilder.Entity<Product>().ToTable("Products").HasKey(k => k.ID);
modelBuilder.Entity<Product>().Property(p=> p.Id).HasColumnName("ProductID");
chichiton
  • 33
  • 7
  • This doesn't work. Error is "Products.cs does not implement IEntity.Id" It would probably affect foreign key constraints too. Thanks for help anyway, i learned something new with this too. – TommiAL Mar 17 '20 at 19:49