user1757483
user1757483

Reputation: 87

Convert Entity data model to Data Transfer Object

I have these two classes

enum CustomerType {
 CitizenBank = 0,
  Wellsfargo = 1
}




public abstarct class CustomerDto {
 int customerId {
  get;
  set;
 }
 string customerName {
  get;
  set;
 }
 string CustometAddress {
  get;
  set;
 }
 int CustomerTypeId {
  get;
  set;
 }
}
public CitizenBank: CustomerDto {}


public Wellsfargo: CustomerDto {}


Public Class CustomerEntity {

int customerId {
  get;
  set;
 }
 string customerName {
  get;
  set;
 }
 string CustometAddress {
  get;
  set;
 }
 int CustomerTypeId {
  get;
  set;
 }
}

I wrote a class to convert from entity to DTO

public class EntityModelToModel {
 ///Method to convert 
 public CustomerDto ToDto(CustomerEntity customerEntity) {
  /// returns type customerDto based on customertypeid
  switch (CustomerType) {
   case Wellsfargo
   return New Wellsfargo()  
   case citizen
   return new citizen()  //calls method that converts from customer Entity to citizen
  }

 }

I have method to check if my types match

 public bool CanExecute(CustomerEntity customerEntity) {
   foreach(var customerType in Enum.GetValues(typeof(Enums.customerType) if (customerEntity.CustomerType == customerType)
      return true
     else
      false
    }
   }

Now my calling code I have array of CustomerEntity[] which have two items of customerid for wellsfargo and citizens. I want to do this

   var res = CustomerEntity[].where(x => EntityModelToModel.CanExecute(x).Select(x => EntityModelToModel.ToDto(x)) 

My problem is:

If my array has two items this only checks the first items and returns.

What I want is it should check for two items and return them.

Upvotes: 0

Views: 289

Answers (1)

Const Phi
Const Phi

Reputation: 75

I think that you should change your CanExecute method like this:


public static class EntityModelToModel
{

    // ...

    public static bool CanExecute(CustomerEntity customerEntity)
    {
        foreach (var customerType in Enum.GetValues(typeof(CustomerType)))
        {
            if (customerEntity.CustomerTypeId == (int)customerType)
            {
                return true;
            }
        }

        return false;
    }
}

Because your method break execution flow after first check.

Upvotes: 1

Related Questions