简体   繁体   English

如何更改类的构造函数以接收已创建的类和构造函数的对象的通用列表? (C#)

[英]How to change constructor of a class to recieve generic list of objects of already created class and constructor? (C#)

I have the following class: 我有以下课程:

public class OrderArticlesAdapter : RecyclerView.Adapter
{
    public List<OrderArticleViewModel> listOfOrderArticles;
    .....
    .....
    //constructor
    public OrderArticlesAdapter(List<OrderArticleViewModel> orderArticles, ....., .....)
    {
        listOfOrderArticles = orderArticles;
        ......
    }
}

I want the class to be able to work not only with list of OrderArticleViewModel but also with list of type Invoices and any other type. 我希望该类不仅可以与OrderArticleViewModel列表一起使用,而且还可以与Invoices类型和任何其他类型的列表一起使用。 OrderArticleViewModel class looks like that: OrderArticleViewModel类如下所示:

public class OrderArticleViewModel
{
    public string ArticleId { get; set; }

    public string LotId { get; set; }

    public string ArticleName { get; set; }

    public string PriceDiscount { get; set; }

    public string ArticlePrice { get; set; }

    public string ArticleQuantity { get; set; }

    public string ArticleTotalPrice { get; set; }

    public string Barcode { get; set; }

    public string ExpireDate { get; set; }

    public string LotName { get; set; }

    public string ArticlePriceAfterDiscount
    {
        get
        {
            decimal priceDiscount;
            if (!Decimal.TryParse(PriceDiscount, out priceDiscount))
            {
                priceDiscount = 0;
            }
            decimal articlePrice = Convert.ToDecimal(ArticlePrice);
            decimal discountAmount = Math.Round(articlePrice * (priceDiscount / 100), 4);
            decimal articlePriceAfterDiscount = articlePrice - discountAmount;
            return articlePriceAfterDiscount.ToString();
        }
    }
}

Invoices class looks like that: Invoices类如下所示:

public class Invoices
{
    public string ArtId { get; set; }
    public string Name { get; set; }
    public string Quantity { get; set; }
    public string Price { get; set; }
    public string Sum { get; set; }
    public string Discount { get; set; }
    public string PriceWodiscount { get; set; }
    public string Id { get; set; }
}

ArticleId , ArticleName , ArticleQuantity , PriceDiscount , ArticlePrice , Discount , ArticlePriceAfterDiscount from class OrderArticleViewModel correspond to properties ArtId , Name , Quantity , Discount , Price , Sum from class Invoices . ArticleIdArticleNameArticleQuantityPriceDiscountArticlePriceDiscountArticlePriceAfterDiscount从类OrderArticleViewModel对应的属性ArtIdNameQuantityDiscountPriceSum从类Invoices How do I make OrderArticlesAdapter constructor to be able to recieve generic list of OrderArticleViewModel or Invoices or any other type without breaking the functionality of the code where I already have used instance of OrderArticlesAdapter ? 如何使OrderArticlesAdapter构造函数能够接收OrderArticleViewModelInvoices或任何其他类型的通用列表,而又不破坏我已经使用OrderArticlesAdapter实例的代码的功能?

Create another constructor in which you convert the invoice list to an ArticleViewModel list: 创建另一个构造函数,在其中将发票清单转换为ArticleViewModel清单:

public class OrderArticlesAdapter : RecyclerView.Adapter
{
    public List<OrderArticleViewModel> listOfOrderArticles;

    public OrderArticlesAdapter(List<OrderArticleViewModel> orderArticles, ....., .....)
    {
        listOfOrderArticles = orderArticles;
    }

    public OrderArticlesAdapter(List<Invoice> invoices)
    {
        listOfOrderArticles = invoices.Select(MapToArticleVM).ToList();
    }

    private OrderArticleViewModel MapToArticleVM(Invoice invoice)
    {
        return new OrderArticleViewModel
        {
            ArticleId = invoice.ArtId,
            // ...
        };
    }
}

Do note the resulting list will miss some properties, because Invoice doesn't contain Barcode , for example. 请注意,结果列表将缺少某些属性,例如,由于Invoice不包含Barcode

One option would be to write an extension method to convert an invoice to view model 一种选择是编写扩展方法以将发票转换为视图模型

public static class InvoicesExtensions
{
    public static OrderArticleViewModel ToOrderArticleViewModel(this Invoices i)
    {
        return new OrderArticleViewModel { ArticleId = i.ArtId, ... };
    }
}

... and then you can call the constructor like ...然后您可以像这样调用构造函数

var invoices = new List<Invoices>();

var adapter = new OrderArticlesAdapter(invoices.Select(i => i.ToOrderArticleViewModel()).ToList());

I would also recommend 我也建议

  • renaming Invoices to Invoice as it seems to represent an actual invoice not multiple invoices Invoices重命名为Invoice因为它似乎代表实际发票而不是多个发票
  • use IEnumerable<OrderArticleViewModel> instead of List<OrderArticleViewModel> for the constructor parameter as this makes the constructor more versatile 使用IEnumerable<OrderArticleViewModel>代替List<OrderArticleViewModel>作为构造函数参数,因为这使构造函数更加通用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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