Шаблон Nano ASP.NET для SaaS
Учетные данные администратора (для всех арендаторов): admin@email.com / Password123!
Пример данных обнуляется каждый час
Контакты

Using Interfaces in ASP.NET Core (.NET 8). Tutorial and Guide

Райан Льюис
Декабрь 10, 2022

So, what’s the deal with interfaces? To be honest, I never used them when I worked in ASP.NET MVC 5 projects. Anyone who worked on larger enterprise applications probably used them regularly, but for me it just seemed like needless extra code. By the way, if you are looking to develop enterprise grade applications that are maintainable with solid architecture, please check out the Nano ASP.NET Boilerplate.

It wasn’t until I moved on to .NET Core and learned about architecture and Инъекция зависимостей, that I really fell in love with them.

Here are the main reasons to use interfaces, ranked by most useful:

  1. Swapping class implementations / improving maintainability (Dependency Injection)
  2. Enforcing contracts
  3. Tagging and automation

Скачайте sample project for this tutorial from Github here.

Essentially, an interface is like a blueprint of a class. The interface doesn’t contain ‘the meat’ of any code, just the signatures.

Let’s imagine that we have a web application where we can perform CRUD operations on a entity called Продукт. And for that functionality, we’ll create a service. Our service will consist of an интерфейс and a класс. Let’s look at an example.

This is the интерфейс for the service.

с использованием simpleApp.Модели;
с использованием simpleApp.Услуги.Application.Продукты и услуги.DTO;

пространство имён simpleApp.Услуги.Application.Продукты и услуги
{
    общедоступный интерфейс IProductService
    {
        IEnumerable<Продукт> GetAllProducts();
        Продукт GetProductById(int id);
        Продукт Создать продукт(Запрос CreateProductRequest);
        bool Удалить продукт(int id);
    }
}

Notice that the structure looks similar to a class.

  • The first difference is the declaration of public interface instead of public class.
  • Second difference is that there is no code, only method signatures.

It’s convention to preface the name of the interface with a capital ‘I’. In this case, IProductService.

Now let’s take a look at what a класс for this interface might look like.

с использованием simpleApp.Модели;
с использованием simpleApp.Услуги.Application.Продукты и услуги.DTO;

пространство имён simpleApp.Услуги.Application.Продукты и услуги
{
    общедоступный класс Продукты и услуги : IProductService
    {
        частный только для чтения ApplicationDbContext _context; // контекст базы данных

        общедоступный Продукты и услуги(ApplicationDbContext context)
        {
            _контекст = контекст;
        }

        // получить список всех товаров
        общедоступный IEnumerable<Продукт> GetAllProducts()
        {
            var продукты = _контекст.Продукция.ToList();
            возврат продукты;
        }

        // получить один товар
        общедоступный Продукт GetProductById(int id)
        {
            var товар = _контекст.Продукция.Где(x => x.Идентификатор == id).FirstOrDefault();
            возврат товар;
        }

        // создать новый товар
        общедоступный Продукт Создать продукт(Запрос CreateProductRequest)
        {
            var товар = новый Продукт();
            товар.Имя = запрос.Имя;
            товар.Цена = запрос.Цена;

            _контекст.Добавить(товар);
            _контекст.Сохранить изменения();

            возврат товар;
        }


        // удалить товар
        общедоступный bool Удалить продукт(int id)
        {
            var товар = _контекст.Продукция.Где(x => x.Идентификатор == id).FirstOrDefault();

            если (товар != null)
            {
                _контекст.Удалить(товар);
                _контекст.Сохранить изменения();
                возврат true;
            }
            возврат false;
        }
    }

}

This probably looks familiar, as its just a regular class. This is a CRUD style service class with methods to get a list of products, find by ID, add, or delete.

The usage of the interface comes into play right after public class ProductService. By using a colon : after the name of the class followed by the interface name IProductService, we are ‘implementing’ an interface.

So what does this mean? Well it means that, we are now bound to follow what has been outlined in the interface. Our class must have methods for get a list of products, find by ID, добавить, и удалить and those methods must have the same signature (aka – method name, input parameters, and return output).

If we were to add a new method signature in the IProductService interface such as an update product method, our Продукты и услуги class won’t be fulfilling the specifications of that blueprint. We would need to add that method in our class and until we do so, we will get an error. We could however, have other methods in the Продукты и услуги class and that wouldn’t cause any compilation issues.

Ok so what’s the point of this? All we have done so far is write more code for no apparent benefit.  

The benefit is that we can add new classes to carry out the same objective as the original Продукты и услуги class. We can have multiple versions of ‘the same class’ and as long as they implement the same interface, we can easily swap them out. This prevents us from having to edit the currently implemented class when we want to make improvements on a live application.

Imagine that months later, our application has new business requirements for the product service. In that case, we could create a ProductServiceV2 class with some totally reworked code.  As long as the new class implements the same IProductService interface, it can be used by the application to fulfill the same role, like interchangeable parts. The only thing we will need to to when we are ready to implement the new ProductServiceV2 class, is change one line of code in the top level of our application (the service registration) to tell ASP to use the new class everywhere its implemented. That is in essence what Инъекция зависимостей is. (More on that later).

This swap-ability principle is what allows Инъекция зависимостей to work in .NET applications.

Perhaps you may not have to drastically rework code your business logic/CRUD services very often, but using interfaces is required for Инъекция зависимостей, and it will make things more maintainable. Maintainability is very important in applications that evolve over time, so having an understanding of .NET architecture is essential before you hammer out the next application you build. The Nano ASP.NET Boilerplate can quickly advance your understanding of .NET architecture and provide a highly maintainable framework from the start. I encourage anyone at any level of .NET ability to check it out, review the документация or try the live demo.

The need to swap in and out classes does happen quite often with infrastructure-style services which rely on third party packages like:

  • Mail services
  • Image / file upload services
  • Export to PDF, Excel, etc.

Let’s take a look at a more practical example.

You might have a web application which contains a mailer service. For simplicity, let’s say our application is only concerned about sending mail and the mail service has one method SendEmail, which returns true or false. It takes in a string message and string subject as parameters.

пространство имён simpleApp.Услуги.Infrastructure.Mailer
{
    общедоступный интерфейс IMailerService
    {
        bool SendEmail(строка message, строка subject);
    }
}

Let’s say that we have been using Send In Blue, which is an email marketing platform with a .NET SDK, and our class looks like this. We can pretend there is a lot of code specific to how Send In Blue handles sending mail in this class. This class would likely have using statements from the SDK.

// --using statements from SendInBlue SDK

пространство имён simpleApp.Услуги.Infrastructure.Mailer
{
    общедоступный класс SendInBlueService : IMailerService
    {
        // send method (required by interface)
        общедоступный bool SendEmail(строка message, строка subject)
        {
            // --
            // -- code specific to SendInBlue
            // --

            возврат true;
        }
    }
}

A service class could have more methods than just SendEmail. That’s totally fine, you can have more methods, properties, or whatever in your class as long as it fulfills the requirements of the interface. As long as a class has a SendEmail method which accepts the same parameters and returns a boolean value, its eligible to be used as the implementation of the IMailerService interface.

Now let’s say that Send In Blue decides to increase their prices and we want to use some alternative. Thanks to our interface, we can leave the SendInBlueService class as it is and create a new class alongside it called MailChimpService which contains code is specific to how Mail Chimp sends mail. As long as it has a method called SendEmail with a signature which matches that in the interface, we can use it.

// --using statements from Mailchimp SDK

пространство имён simpleApp.Услуги.Infrastructure.Mailer
{
    общедоступный класс MailChimpService : IMailerService
    {
        // send method (required by interface)
        общедоступный bool SendEmail(строка message, строка subject)
        {
           
            // --
            // -- code specific to MailChimp
            // --

            возврат true;
        }
    }
}

Thanks to the pluggability interfaces grant us, we can build our new MailChimpService in a new class alongside the SendInBlueService class and when it’s ready, we’ll just change one line of code to tell our application to use MailChimpService wherever we are using the IMailService to send mail in our application.

So where is this one line of code? What is this magical little line which we have now spoken about twice?? It’s the service registration with Инъекция зависимостей, typically happening in the top level of the application. This registration is where we specify the link between интерфейс и класс to implement, as well as the lifetime (transient, scoped, singleton) of the service. The injection points are in the constructors in various other classes scattered everywhere.

But before we go completely wild on Dependency Injection, lets look at two other ways interfaces are useful.

Interfaces are useful for enforcing requirements on classes. Actually, the two previous two examples already demonstrated this, but here is an example where the interfaces are used only for that purpose.  

Let’s say we have an application with lot of entity classes, and on some of these classes, we want to make sure that their creation date is persisted along with who created them (CreatedBy). For that we can create an интерфейс примерно так:

пространство имён simpleApp.Модели.Contracts
{
    общедоступный интерфейс IAuditableEntity
    {
        общедоступный строка CreatedBy { get; набор; }
        public DateTime CreatedOn { get; набор; }
    }
}

Now for any entity class that we want to adhere to that contract, we can implement the IAuditableEntity interface on them. For example, we have a Продукт entity type:

с использованием simpleApp.Модели.Abstract;
с использованием simpleApp.Модели.Contracts;

пространство имён simpleApp.Модели
{
    // sample business entity
    общедоступный класс Продукт : BaseEntity, IAuditableEntity, ISoftDelete
    {
        общедоступный строка Имя { get; набор; }
        общедоступный десятичный Цена { get; набор; }
        общедоступный строка CreatedBy { get; набор; }
        общедоступный DateTime CreatedOn { get; набор; }
        общедоступный bool IsDeleted { get; набор; }
    }
}

By implementing the interface, we are now obligated to have a CreatedBy (String) and CreatedOn (Datetime) field in this class.

In .NET, classes can be inherited from higher level classes. In this example, we inherit from a BaseEntity which simply has an Идентификатор field. In these cases, add a comma and specify the interfaces to implement after the inherited class. By the way, .NET classes can only inherit from one higher level class at a time.

We can implement multiple interfaces though at the same time in the same class. Let’s say that for some of our entity types, we don’t want to actually delete the record from our database but rather just mark them as deleted. For that we might also have an ISoftDelete interface, with a Boolean field called IsDeleted.

Going back to our Продукт entity type, lets implement the ISoftDelete interface here as well.

We just add it with a comma after the first interface. With the interface implemented, we must add a new field to the class, IsDeleted and it must be a Boolean.

By the way, if you place the cursor in the ISoftDelete text, you can hold down control/command and press period to get a helper menu. Select implement interface and Visual Studio will scaffold the code signatures for you, which is useful if you have a lot of methods and properties in your interface.

And just like that we are using interfaces to enforce contracts. By why you ask? What is the point? Well aside from maintaining consistency, you can do some really cool things.

В Entity Framework for example, in our ApplicationDBContext, we can override the Сохранить изменения method and add in some automation code to stamp the date and the current user whenever an entity is saved, as long as it implements IAuditableEntity.

Same with ISoftDelete, we can tell our app to not delete the record if this entity is ‘of the group’ that is ‘tagged with’ ISoftDelete, and instead update the boolean field.

с использованием Microsoft.EntityFrameworkCore;
с использованием simpleApp.Модели.Contracts;

пространство имён simpleApp.Модели
{
    общедоступный класс ApplicationDbContext : DbContext
    {

        // convention used by Entity Framework 
        общедоступный ApplicationDbContext(DbContextOptions options) : база(варианты)
        {
        }

        // -- create DbSets for all entity types to be managed with EF
        общедоступный DbSet<Продукт> Продукция { get; набор; }


        // On Save Changes
        // -- automation with interfaces example
        // -- handle audit fields (createdOn, createdBy, modifiedBy, modifiedOn) and soft delete fields
        общедоступный переопределить int Сохранить изменения()
        {
            // Auditable fields / soft delete on tables with IAuditableEntity
            foreach (var запись в ChangeTracker.Записи<IAuditableEntity>().ToList()) 
            {
                переключатель (запись.Штат)
                {
                    случай Состояние объекта.Добавлено:
                        запись.Объект.CreatedBy = "the current user";
                        запись.Объект.CreatedOn = DateTime.UtcNow;
                        перерыв;
                    случай Состояние объекта.Удалено:
                        // intercept delete requests, forward as modified on tables with ISoftDelete
                        если (запись.Объект is ISoftDelete softDelete) 
                        {
                            softDelete.IsDeleted = true;
                            запись.Штат = Состояние объекта.Изменено;
                        }
                        перерыв;
                }
            }

            var результат = база.Сохранить изменения();
            возврат результат;
        }
    }
}

Don’t worry if you aren’t familiar with Entity Framework, the point is that you can use interfaces as groupings and perform automation on them. Which brings us to our last use case for interfaces.

The previous example did this actually, as you can see, these concepts are a bit overlapping

Where we had ChangeTracker.Entries<InterfaceName> we told Entity Framework to create a list of all entities that implemented IAuditableEntity and we looped over them to stamp some dates и usernames.

So, can we use interfaces for just their grouping power? Yes, and its common to do so. You can create a ‘blank’ interface and use it like a tag system. There are many occasions in .NET where you can pass an interface type to a registration or filter method and have whatever it is that you are doing, done to all those marked classes.

For example, with Инъекция зависимостей, we will need to register all of our services in the top level of our application (the Service Container). That might go a lot smoother if we create blank interface markers like ITransientService, и IScopedService, and implement those interfaces in any service class we create. With that we could loop over and ‘auto-register’ our services. That is something fancy that the Nano ASP.NET boilerplate does, and if that sounds interesting go check it out (final shameless plug).

Now with a good understanding of interfaces, you can start using them in your code to make your application more flexible and easier to maintain.

The next article is about Инъекция зависимостей, and we will see how interfaces serve as the building blocks in making that work.   

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

2 comments on “Using Interfaces in ASP.NET Core (.NET 8). Tutorial and Guide”

  1. jp2code:

    I still don’t understand how I’m supposed to get the interface to pass to the constructor. Every example I see already has the code magically defined, like in `public ProductService(ApplicationDbContext context)`, but I never see anyone creating a new instance of ProductService, ie `var service = new ProductService(makeBelieveApplicationDbContextObj);` So, where does that “make believe application DB Context Object” come from? Please tell me! I’ve been searching for months and I can’t find it.

    1. Ryan:

      Hi, thanks for your comment. I should append the article to show how you can use the ProductService in another class. For now though here is an example: Lets say you have another service, or controller called Invoice, and the invoice needs several services, one of which is our product service. In the constructor of the InvoiceController class, you would use the interface for the product service. So it would look something like this:

      public InvoiceController(IProductService productService)
      {
      _productService = productService
      }

      And then above the constructor, private readonly IProductService _productService;

      Now you have an instance of _productService for use within this invoice controller class.
      That’s how you use services. You always initialize them in a constructor using their interface.

Вам нужен шаблон ASP.NET для создания вашего следующего MVP?
Ознакомьтесь с шаблоном проекта Nano ASP.NET для многопользовательского SaaS и сэкономьте недели или месяцы времени на разработке.
Узнать больше