Nano ASP.NET SaaS 模板
管理员凭据(所有租户):admin@email.com / Password123!
样本数据每小时重置一次

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

瑞安·刘易斯
12 月 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 模板.

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.服务.应用.产品与服务.DTOs;

命名空间 simpleApp.服务.应用.产品与服务
{
    public 接口 IProductService
    {
        IEnumerable<产品> 获取所有产品();
        产品 GetProductById(int id);
        产品 CreateProduct(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.服务.应用.产品与服务.DTOs;

命名空间 simpleApp.服务.应用.产品与服务
{
    public 类 产品与服务 : IProductService
    {
        私有 只读 ApplicationDbContext _context; // 数据库上下文

        public 产品与服务(ApplicationDbContext context)
        {
            _context = 上下文;
        }

        // 获取所有产品的列表
        public IEnumerable<产品> 获取所有产品()
        {
            var 产品 = _context.产品.ToList();
            返回 产品;
        }

        // 获取单个商品
        public 产品 GetProductById(int id)
        {
            var 产品 = _context.产品.在哪里(x => x.Id == id).FirstOrDefault();
            返回 产品;
        }

        // 创建一个新产品
        public 产品 CreateProduct(CreateProductRequest 请求)
        {
            var 产品 = 新 产品();
            产品.姓名 = 请求.姓名;
            产品.价格 = 请求.价格;

            _context.添加(产品);
            _context.SaveChanges();

            返回 产品;
        }


        // 删除一个产品
        public bool 删除产品(int id)
        {
            var 产品 = _context.产品.在哪里(x => x.Id == id).FirstOrDefault();

            如果 (产品 != null)
            {
                _context.删除(产品);
                _context.SaveChanges();
                返回 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 模板 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 消息 and string subject as parameters.

命名空间 simpleApp.服务.基础设施.Mailer
{
    public 接口 IMailerService
    {
        bool SendEmail(字符串 消息, 字符串 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.服务.基础设施.Mailer
{
    public 类 SendInBlueService : IMailerService
    {
        // send method (required by interface)
        public bool SendEmail(字符串 消息, 字符串 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.服务.基础设施.Mailer
{
    public 类 MailChimpService : IMailerService
    {
        // send method (required by interface)
        public bool SendEmail(字符串 消息, 字符串 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
{
    public 接口 IAuditableEntity
    {
        public 字符串 CreatedBy { get; set; }
        public DateTime CreatedOn { get; set; }
    }
}

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
    public 类 产品 : BaseEntity, IAuditableEntity, ISoftDelete
    {
        public 字符串 姓名 { get; set; }
        public 十进制 价格 { get; set; }
        public 字符串 CreatedBy { get; set; }
        public DateTime CreatedOn { get; set; }
        public bool IsDeleted { get; set; }
    }
}

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 ID 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 SaveChanges 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.

使用 微软.EntityFrameworkCore;
使用 simpleApp.模型.Contracts;

命名空间 simpleApp.模型
{
    public 类 ApplicationDbContext : DbContext
    {

        // convention used by Entity Framework 
        public ApplicationDbContext(DbContextOptions 选项) : 基数(选项)
        {
        }

        // -- create DbSets for all entity types to be managed with EF
        public DbSet<产品> 产品 { get; set; }


        // On Save Changes
        // -- automation with interfaces example
        // -- handle audit fields (createdOn, createdBy, modifiedBy, modifiedOn) and soft delete fields
        public 覆盖 int SaveChanges()
        {
            // Auditable fields / soft delete on tables with IAuditableEntity
            foreach (var 条目 在 ChangeTracker.条目<IAuditableEntity>().ToList()) 
            {
                开关 (条目.州)
                {
                    案例 EntityState.已添加:
                        条目.实体.CreatedBy = "the current user";
                        条目.实体.CreatedOn = DateTime.UtcNow;
                        break;
                    案例 EntityState.已删除:
                        // intercept delete requests, forward as modified on tables with ISoftDelete
                        如果 (条目.实体 是 ISoftDelete softDelete) 
                        {
                            softDelete.IsDeleted = true;
                            条目.州 = EntityState.已修改;
                        }
                        break;
                }
            }

            var 结果 = 基数.SaveChanges();
            返回 结果;
        }
    }
}

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.   

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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 模板项目,为您节省数周甚至数月的开发时间。.
了解更多