0

I am beginner in programming and develop my pet project in ASP.NET Core MVC for learning English words. I want to perform calculations based on other properties of the word and get priority of the word for learning using a formula in DTO response object. Currently, I perform calculations in the extension method to UserWords object.

using EnjOffer.Core.Domain.Entities;
using EnjOffer.Core.Services;

namespace EnjOffer.Core.DTO
{
    public class UserWordsResponse
    {
        public Guid UserWordId { get; set; }
        public string? Word { get; set; }
        public string? WordTranslation { get; set; }
        public DateTime? LastTimeEntered { get; set; }
        public int CorrectEnteredCount { get; set; }
        public int IncorrectEnteredCount { get; set; }
        public double Priority { get; set; }
        public Guid UserId { get; set; }

        public override bool Equals(object? obj)
        {
            if (obj == null)
            {
                return false;
            }

            if (obj.GetType() != typeof(UserWordsResponse))
            {
                return false;
            }
            UserWordsResponse userWord_to_compare = (UserWordsResponse)obj;
            return UserWordId == userWord_to_compare.UserWordId &&
                Word == userWord_to_compare.Word &&
                WordTranslation == userWord_to_compare.WordTranslation &&
                LastTimeEntered == userWord_to_compare.LastTimeEntered &&
                CorrectEnteredCount == userWord_to_compare.CorrectEnteredCount &&
                IncorrectEnteredCount == userWord_to_compare.IncorrectEnteredCount &&
                Priority == userWord_to_compare.Priority &&
                UserId == userWord_to_compare.UserId;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Major Bug", "S3249:Classes directly extending \"object\" should not call \"base\" in \"GetHashCode\" or \"Equals\"", Justification = "<Pending>")]
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }

    public static class UserWordsResponseExtensions
    {
        public static UserWordsResponse ToUserWordsResponse(this UserWords userWords)
        {
            return new UserWordsResponse()
            {
                UserWordId = userWords.UserWordId,
                Word = userWords.Word,
                WordTranslation = userWords.WordTranslation,
                LastTimeEntered = userWords.LastTimeEntered,
                CorrectEnteredCount = userWords.CorrectEnteredCount,
                IncorrectEnteredCount = userWords.IncorrectEnteredCount,
                Priority = (userWords.LastTimeEntered is not null) ? ((double)userWords.CorrectEnteredCount / (userWords.IncorrectEnteredCount +
                userWords.CorrectEnteredCount) * (1 - Math.Exp(-(double)(DateTime.Now - userWords.LastTimeEntered).Value.Hours / 3))) : 0,
                UserId = userWords.UserId
            };
        }
    }
}

However, I suppose it isn't good practice. Probably, it's better to create a method GetPriority() in UserWordsService.cs class and call it in the extension method. But, how to do that? Do I need to create an instance of UserWordsService class in ToUserWordsResponse() extension method, or is there a better way?

Would it be a good practice to do like this:

Create GetPriority in UserWordsService:

using EnjOffer.Core.Domain.Entities;
using EnjOffer.Core.DTO;
using EnjOffer.Core.Helpers;
using EnjOffer.Core.ServiceContracts;

namespace EnjOffer.Core.Services
{
    public class UserWordsService : IUserWordsService
    {
        private readonly List<UserWords> _userWords;

        public UserWordsService()
        {
            _userWords = new List<UserWords>();
        }

        public double GetPriority(DateTime? lastTimeEntered, int correctlyEntered, int incorrectlyEntererd)
        {
            return (lastTimeEntered is not null) ? ((double)correctlyEntered / (incorrectlyEntererd +
                correctlyEntered) * (1 - Math.Exp(-(double)(DateTime.Now - lastTimeEntered).Value.Hours / 3))) : 0;
        }
    }
}

Create an instance of UserWordsService in the extension method that returns DTO response:

public static class UserWordsResponseExtensions
    {
        public static UserWordsResponse ToUserWordsResponse(this UserWords userWords)
        {
            UserWordsService userWordsService = new UserWordsService();
            double priority = userWordsService.GetPriority(userWords.LastTimeEntered, userWords.CorrectEnteredCount, userWords.IncorrectEnteredCount);

            return new UserWordsResponse()
            {
                UserWordId = userWords.UserWordId,
                Word = userWords.Word,
                WordTranslation = userWords.WordTranslation,
                LastTimeEntered = userWords.LastTimeEntered,
                CorrectEnteredCount = userWords.CorrectEnteredCount,
                IncorrectEnteredCount = userWords.IncorrectEnteredCount,
                Priority = priority,
                UserId = userWords.UserId
            };
      }
}
  • 1
    Related: https://stackoverflow.com/questions/55213803/use-dependency-injection-in-static-class – Steven Apr 04 '23 at 15:42
  • 1
    Related: https://stackoverflow.com/questions/52325438/how-to-inject-dependency-to-static-class – Steven Apr 04 '23 at 15:43

0 Answers0