简体   繁体   中英

Return false if type properties equals null or 0

I tried following the method as follows here: Checking if Object has null in every property . However, when instantiating Order newOrder = new Order(); . I cannot simple just implement bool props = newOrder.ArePropertiesNotNull() . What am I supposed to add to my Order class? And where do I implement the function for ArePropertiesNotNull<T>(this T obj) ? I would like to know if there is a way to return false if value returned equals 0 or null?

Here is my code:

OrderProdRepository.cs

...
public bool ReadFromFile(string _date)
        {
            taxesFile.ReadFile();
            productsFile.ReadFile();

            string orderFileName = $"C:\\tempfolder\\Orders_{_date}.txt";

            List<string> lines = File.ReadAllLines(orderFileName).ToList();

            foreach (var line in lines.Skip(1)) //?? new List<string>(0)
            {
                List<string> entry = line.Split(',').ToList();

                Order newOrder = new Order();               
                int.TryParse(entry[0], out int orderNumber);
                newOrder.OrderNumber = orderNumber;
                newOrder.Date = _date;
                newOrder.CustomerName = entry[1];
                newOrder.State = taxesFile.StateAbbreviation(entry[2]);
                newOrder.StateName = taxesFile.StateName(newOrder.State);
                decimal.TryParse(entry[3], out decimal taxRate);
                newOrder.TaxRate = taxesFile.TaxRate(taxRate);
                newOrder.ProductType = productsFile.ProductType(entry[4]);
                decimal.TryParse(entry[5], out decimal area);
                newOrder.Area = area;
                decimal.TryParse(entry[6], out decimal costPerSquareFoot);
                newOrder.CostPerSquareFoot = productsFile.CostPerSquareFoot(costPerSquareFoot);
                decimal.TryParse(entry[7], out decimal laborCostPerSquareFoot);
                newOrder.LaborCostPerSquareFoot = productsFile.LaborCostPerSquareFoot(laborCostPerSquareFoot);
                decimal.TryParse(entry[8], out decimal materialCost);
                newOrder.MaterialCost = materialCost;
                decimal.TryParse(entry[9], out decimal laborCost);
                newOrder.LaborCost = laborCost;
                decimal.TryParse(entry[10], out decimal tax);
                newOrder.Tax = tax;
                decimal.TryParse(entry[11], out decimal total);
                newOrder.Total = total;


                orderList.Add(newOrder);
            }
            return true;
        }
...

You need to create this method an extension method. It should be defined in static class:

public static class ObjectExtensions
{        
    public static bool ArePropertiesNotNull<T>(this T obj)
    {
        return typeof(T).GetProperties().All(propertyInfo => propertyInfo.GetValue(obj) != null);
    }
}

I think you need a function to check each line for null and/or 0 values:

private bool IsValidLine(string line)
{
    if (line == null)
        return false;

    var arr = line.Split(',');   

    //Uncomment this if splitting the line will always return 11 items array.
    //if (arr.Length < 11)
    //    return false;      

    return arr.Aggregate(0, (n, s) => 
    (decimal.TryParse(s, out decimal d) && d == 0) || 
    string.IsNullOrWhiteSpace(s) ? n + 1 : n) == 0;
}

You can use it in your code as follows:

public bool ReadFromFile(string _date)
{
    var orderFileName = $"C:\\tempfolder\\Orders_{_date}.txt";
    var lines = File.ReadAllLines(orderFileName);

    foreach (var line in lines.Skip(1))
    {
        //If parsing any line returns false.
        if (!IsValidLine(line))
            return false;

        //Or if you need to create a list of the valid Order entries.
        if (IsValidLine(line))
        {
            var order = new Order();

            //...

            orderList.Add(newOrder);
        }
    }
    return true;
}

Alternatives:

  • Add a static function in the Order class to parse a given line and return a new object of Order type if the line is valid. Something like this .
  • If its not too late, then consider using a local database or serialization. Something like this and maybe this if you don't mind a vb.net example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM