简体   繁体   中英

How can I instantiate and add to a class inside a foreach loop in C#

I have code that I want to use to log error messages. I made two classes like this to hold the messages:

public class HttpStatusErrors
{
    public HttpStatusErrors()
    {
        this.Details = new List<HttpStatusErrorDetails>();
    }
    public string Header { set; get; }
    public IList<HttpStatusErrorDetails> Details { set; get; }
}
public class HttpStatusErrorDetails
{
    public HttpStatusErrorDetails()
    {
        this.Details = new List<string>();
    }
    public string Header { set; get; }
    public IList<string> Details { set; get; }
}

I think this is a good way to do this but I am not 100% sure and would welcome any suggestions.

What I would like to do is in the first class to store the Header which in my example would be "Validation Messages". Then in the Detail object I would like to store another Header that showed the entity with the error and finally in the Details store the validation errors.

What I am confused on is how should I instantiate the classes. I have done this so far. I think this is correct. But how can I add the details that I need to add:

catch (DbEntityValidationException ex)
{
    var msg = new HttpStatusErrors();
    msg.Header = "Validation Error";

    foreach (var eve in ex.EntityValidationErrors)
    {
        var header = string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
            eve.Entry.Entity.GetType().Name, eve.Entry.State);
        foreach (var ve in eve.ValidationErrors)
        {
            var detail = string.Format("- Property: \"{0}\", Error: \"{1}\"",
                ve.PropertyName, ve.ErrorMessage);
        }

    }

}.

Instantiate collections in Details properties. Instantiate new items, and add them to collections in loop:

var msg = new HttpStatusErrors();
msg.Header = "Validation Error";
msg.Details = new List<HttpStatusErrorDetails>();

foreach (var eve in ex.EntityValidationErrors)
{
    var detail = new HttpStatusErrorDetails()
    detail.Header = String.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
        eve.Entry.Entity.GetType().Name, eve.Entry.State);

    detail.Details = new List<string>();
    foreach (var ve in eve.ValidationErrors)
    {
        detail.Details.Add(String.Format("- Property: \"{0}\", Error: \"{1}\"",
            ve.PropertyName, ve.ErrorMessage));
    }

    msg.Details.Add(detail);
}

Btw you can use LINQ for that:

var msg = new HttpStatusErrors();
msg.Header = "Validation Error";
msg.Details =
    ex.EntityValidationErrors
      .Select(eve => new HttpStatusErrorDetails {
                Header = String.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State)
                Details = eve.ValidationErrors
                         .Select(ve => String.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage))
                         .ToList()
       }).ToList();

Also I like extension methods syntax:

 public static HttpStatusErrors ToHttpStatusErrors(
      this DbEntityValidationException ex)
 {
      return new HttpStatusErrors {
          Header = "Validation Error",
          Details = ex.EntityValidationErrors
                      .Select(eve => eve.ToHttpStatusErrorDetails())
                      .ToList()
      };
 }

 public static HttpStatusErrorDetails ToHttpStatusErrorDetails(
      this DbEntityValidationResult eve)
 {
      return new HttpStatusErrorDetails {
        Header = String.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                               eve.Entry.Entity.GetType().Name, eve.Entry.State),
        Details = eve.ValidationErrors
                     .Select(ve => ve.ToErrorDescription())
                     .ToList()
      };
 }

 public static string ToErrorDescription(
      this DbValidationError ve)
 {
      return String.Format("- Property: \"{0}\", Error: \"{1}\"",
                           ve.PropertyName, ve.ErrorMessage);
 }

Usage of such methods will be like:

catch (DbEntityValidationException ex)
{
    var msg = ex.ToHttpStatusErrors();
}

Try the following:

foreach (var eve in ex.EntityValidationErrors)
{
    //New Details Entity
    var errorDetail = new HttpStatusErrorDetails();

    //Fill in Header
    errorDetail.Header = string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
        eve.Entry.Entity.GetType().Name, eve.Entry.State);

    //Fill Add a detail for each validation error
    foreach (var ve in eve.ValidationErrors)
    {
        errorDetail.Details.Add(string.Format("- Property: \"{0}\", Error: \"{1}\"",
            ve.PropertyName, ve.ErrorMessage));
    }

    //Add Detail to header
    msg.Details.Add(errorDetail);
}

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