Returning ViewModel works fine in EF query returning list of entities but not while returning single entity

asp.net-core c# entity-framework-core

Question

I have a method in my Asp.Net Core Api for insert new entity which get a ViewModel as an argument and returns another ViewModel as result.

I have added Set() method as one of its properties to return list of CreditUserViewModel and it's working fine.

    public async Task<IEnumerable<CreditUserViewModel>> GetAll()
    {
        return await Task.Run(() =>
            _context.Credit
                .Select(CreditUserViewModel.Set));
    }

But while I want to return single object, I have no access to Set() method. Why?

Result ViewModel :

public class CreditUserViewModel
{
    public string PlanTitle { get; set; }
    public string CardSerial { get; set; }
    public DateTime PurchaseDate { get; set; }
    public int ValidationDays { get; set; }
    public int PaidAmount { get; set; }
    public string PurchaseCode { get; set; }
    public static CreditUserViewModel Set(Credit credit)
    {
        return new CreditUserViewModel
        {
            PlanTitle = credit.CreditPlan.Title,
            CardSerial = credit.Card.SerialNumber,
            PurchaseDate = DateTime.UtcNow,
            ValidationDays = credit.CreditPlan.ValidationDays,
            PaidAmount = credit.CreditPlan.Cost,
            PurchaseCode = credit.PurchaseCode
        };
    }
}

Add() method which I expect to return single CreditUserViewModel:

    public async Task<CreditUserViewModel> Add(CreditRegisterViewModel credit)
    {
        var newCredit = new Credit
        {
            CardId = credit.CardId,
            CreditPlanId = credit.CreditPlanId,
            PurchaseCode = credit.PurchaseCode,
            RechargeDate = DateTime.UtcNow
        };

        _context.Entry(newCredit).State = EntityState.Added;

        try
        {
            await _context.SaveChangesAsync();
        }
        catch
        {
            throw new Exception();
        }

        return new CreditUserViewModel.Set(newCredit); **// Error is in this line: The type name Set does not exist in the type CreditUserViewModel**
    }

Error message : The type name Set does not exist in the type CreditUserViewModel

1
0
3/27/2020 7:40:19 PM

Accepted Answer

Shouldn't that be something like:

public async Task<IEnumerable<CreditUserViewModel>> GetAll()
{
    var data = _context.Credit
                       .Select( c => CreditUserViewModel.Set(c) )
                       .ToList();

    return Task.FromResult(data);
}

And this

 return new CreditUserViewModel.Set(newCredit);

should be

 return CreditUserViewModel.Set(newCredit);

Since CreditUserViewModel.Set is a method, not a type, and by convention would normally be called Create as it's in lieu of a constructor. Or implemented as an Extension method called ToViewModel() targeting the entity type, something like:

public static CreditUserViewModel ToViewModel(this CreditUser entity)
{
  ...
}
1
3/27/2020 7:46:11 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow