Mehdi
Mehdi

Reputation: 598

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

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

Upvotes: 0

Views: 33

Answers (1)

David Browne - Microsoft
David Browne - Microsoft

Reputation: 89406

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)
{
  ...
}

Upvotes: 1

Related Questions