The Best Way to Create an Interface in AspNet 6 (C-Sharp)

1. Create your generic Interface like this and you will never regrete

public interface IMyGenericName<T>{
public Task<bool> DoesExist(int id);
public Task<T> AddAsync(T item);
public Task<T> GetById(T item);

}


//Then in your Startup.cs register the Interface as

services.AddScope<IMyGenericName<MyClassNameHere>,MyRepositoryClass>();


//Then in your Respository Class do
public classs MyRepositoryClass : IMyGenericName<MyClassNameHere>{
//Then Implement our functions here.

}

This way you don't duplicate Interfaces everywhere when you only need standard functions across multiple DTO or Modals.

- Enjoy


Edit this Article
System Design Polymorphism Inheritance in C Interfaces Abstract Classes .NET 6 published
Was this page helpful?
Yes Yes! this solution to the problem was helpful. No No! this solution to the problem was not helpful.. Leave ErnesTech Feedback.Feedback
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.

Your Session is Ending
Login to Continue

© 2021 - ErnesTech - Privacy