2016-04-18 6 views

Antwort

3

Schnittstellen zur Rettung!

eine Schnittstelle wie folgt definieren:

// Why IEquatable<T>? Because you don't want identifiers that may not 
// be able to prove that they're equal or not. Most commonly used 
// types used as identifiers already implement IEquatable<T>. For example: 
// int, Guid... 
public interface ICanBeIdentifiable<TId> where TId : IEquatable<TId> 
{ 
    TId Id { get; } 
} 

... und Repository-Schnittstelle Signatur ändern, wie auch folgt:

public interface IRepository<T> where T : class, ICanBeIdentifiable<Guid> 
... 

... oder wenn Sie wollen unbedingt die Tür öffnen, um Beliebiger Bezeichner Typ:

public interface IRepository<TId, T> 
      where TId : IEquatable<TId> 
      where T : class, ICanBeIdentifiable<TId> 

Der Hauptnachteil ist Ihre Domain-Objekte müssen implementieren ganz neue Schnittstelle, aber es ist die Mühe wert.

1

Sie können zusätzliche Schnittstelle hinzufügen, die jede Entität vorantreiben wird Id Eigenschaft haben

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

public interface IRepository<T> where T : class, IEntity 
{ 

} 
Verwandte Themen