4.5: Adding Extension Method
Our DTOs are returning an Age
property instead of a DateOfBirth
property.
Unfortunately, DateTime
does not have a method to calculate age from a given date.
So, we'll have to add our own method on the DateTime
class to do it for us!
Add Helpers Project
Navigate to the root of your project in your command line and run the following to create a new project called Helpers:
Create Extensions Class
Inside of the new .Helpers
project, add a new static class called Extensions.cs
Inside of this class, we'll add a static method that calculates the age based on the DateOfBirth
:
Adding a Project Reference
Next, we need to add a project reference in the .Services
project to the .Helpers
project.
In your EFConnect.Services.csproj
file add a new reference to the .Helpers
project:
Last updated