Interfaces and Contracts
Explicit implementation: make the implementing member Private to expose it only through the interface.
Interface inheritance: Interface IReadWriteRepo : Inherits IReadRepo, IWriteRepo.
Generic variance: Interface IProducer(Of Out T) (covariant), IConsumer(Of In T) (contravariant).
Default interface methods (from C# libraries) can be consumed but not declared in VB.
Interface segregation: small, focused interfaces (IReader, IWriter) beat one large interface.
Public Interface IRepository(Of T)
Function GetById(id As Integer) As T
Function GetAll() As IReadOnlyList(Of T)
Sub Add(entity As T)
Sub Remove(id As Integer)
End Interface
Public Interface INotifier
Sub Notify(recipient As String, message As String)
End Interface
Public Class EmailNotifier
Implements INotifier
Public Sub Notify(recipient As String, message As String) Implements INotifier.Notify
Console.WriteLine($"Email to {recipient}: {message}")
End Sub
End Class
Public Class InMemoryRepository(Of T As IEntity)
Implements IRepository(Of T)
Private ReadOnly _items As New Dictionary(Of Integer, T)
Public Function GetById(id As Integer) As T Implements IRepository(Of T).GetById
Dim item As T = Nothing
_items.TryGetValue(id, item)
Return item
End Function
Public Function GetAll() As IReadOnlyList(Of T) Implements IRepository(Of T).GetAll
Return _items.Values.ToList()
End Function
Public Sub Add(entity As T) Implements IRepository(Of T).Add
_items(entity.Id) = entity
End Sub
Public Sub Remove(id As Integer) Implements IRepository(Of T).Remove
_items.Remove(i
…