在一个泛型 (C#) 中转换 3 个方法

Eug*_*ukh 2 c#

我有 3 种方法可以做同样的事情。一个区别,它们适用于不同的模型。

下面是方法代码:

public IEnumerable<PropertyImportDto> ImportStandardCsv(string fileData)
    {
        List<PropertyImportDto> data;
        using (var memoryStream =
            new MemoryStream(Convert.FromBase64String(fileData.Substring(fileData.IndexOf(',') + 1))))
        {
            using (var streamWriter = new StreamReader(memoryStream))
            using (var csvReader = new CsvReader(streamWriter))
            {
                var records = csvReader.GetRecords<PropertyImportDto>();
                data = records.ToList();
            }
        }

        return data;
    }

    public IEnumerable<ArthurPropertiesImportDto> ImportArthurCsv(string fileData)
    {
        List<ArthurPropertiesImportDto> data;
        using (var memoryStream =
            new MemoryStream(Convert.FromBase64String(fileData.Substring(fileData.IndexOf(',') + 1))))
        {
            using (var streamWriter = new StreamReader(memoryStream))
            using (var csvReader = new CsvReader(streamWriter))
            {
                var records = csvReader.GetRecords<ArthurPropertiesImportDto>();
                data = records.ToList();
            }
        }

        return data;
    }


    public IEnumerable<LandlordVisionImportDto> ImportLandlordVision(string fileData)
    {
        List<LandlordVisionImportDto> data;
        using (var memoryStream =
            new MemoryStream(Convert.FromBase64String(fileData.Substring(fileData.IndexOf(',') + 1))))
        {
            using (var streamWriter = new StreamReader(memoryStream))
            using (var csvReader = new CsvReader(streamWriter))
            {
                var records = csvReader.GetRecords<LandlordVisionImportDto>();
                data = records.ToList();
            }
        }

        return data;
    }
Run Code Online (Sandbox Code Playgroud)

我这样称呼它

public async Task ImportProperties(PM101ImportDto input)
    {
        switch (input.fileTypeId)
        {
            case 1:
            {
                var properties = _csvAppService.ImportStandardCsv(input.FileBase64);
                foreach (var item in properties)
                {
                    var property = new Property
                    {
                        PropertyTypeId = 1,
                        BuildingTypeId = 12,
                        PhoneNumber = item.PhoneNumber,
                        PropertyTitleId = 1,
                        AccountManagerId = AbpSession.TenantId,
                        Addresses = new List<PropertyAddress>
                        {
                            new PropertyAddress
                            {
                                Line1 = item.Line1,
                                Line2 = item.Line2,
                                Line3 = item.Line3,
                                PostCode = item.PostalCode,
                                PostTown = item.Town,
                                Country = item.Country,
                                County = item.County,
                                Latitude = item.Latitude,
                                Longitude = item.Longitude
                            },
                        },
                        Sites = new List<Site>
                        {
                            new Site
                            {
                                NumberOfWindows = 0,
                                NumberOfBedRooms = 0,
                                ApproximateYearBuilt = 1970,
                                PropertyAge = 50,
                                FuelTypeId = 1,
                                ParkingTypeId = 1,
                                FurnishingTypeId = 1
                            }
                        },
                        MarketingInformation = new List<PropertyMarketingInformation>
                        {
                            new PropertyMarketingInformation
                            {
                                MarketingDescription = "", IsBillsIncluded = false
                            }
                        }
                    };
                    await _propertyRepository.InsertAsync(property);
                }

                break;
            }
            case 2:
            {
                var properties = _csvAppService.ImportLandlordVision(input.FileBase64);
                foreach (var item in properties)
                {
                    var property = new Property
                    {
                        PropertyTypeId = 1,
                        BuildingTypeId = 12,
                        PropertyTitleId = 1,
                        AccountManagerId = AbpSession.TenantId,
                        Addresses = new List<PropertyAddress>
                        {
                            new PropertyAddress
                            {
                                Line1 = item.Line1,
                                Line2 = item.Line2,
                                PostCode = item.PostCode,
                                PostTown = item.Town,
                                County = item.County
                            }
                        },
                        Sites = new List<Site>
                        {
                            new Site
                            {
                                NumberOfWindows = 0,
                                NumberOfBedRooms = 0,
                                ApproximateYearBuilt = 1970,
                                PropertyAge = 50,
                                FuelTypeId = 1,
                                ParkingTypeId = 1,
                                FurnishingTypeId = 1
                            }
                        },
                        MarketingInformation = new List<PropertyMarketingInformation>
                        {
                            new PropertyMarketingInformation
                            {
                                MarketingDescription = "", IsBillsIncluded = false
                            }
                        }
                    };
                    await _propertyRepository.InsertAsync(property);
                }

                break;
            }
            case 3:

            {
                var properties = _csvAppService.ImportArthurCsv(input.FileBase64);
                foreach (var item in properties)
                {
                    var property = new Property
                    {
                        PropertyTypeId = 1,
                        BuildingTypeId = 12,
                        PropertyTitleId = 1,
                        AccountManagerId = AbpSession.TenantId,
                        Addresses = new List<PropertyAddress>
                        {
                            new PropertyAddress
                            {
                                Line1 = item.Line1,
                                Line2 = item.Line2,
                                Line3 = item.Line3,
                                PostCode = item.PostCode,
                                PostTown = item.City,
                                County = item.County,
                                Country = item.Country,
                                Latitude = item.Latitude,
                                Longitude = item.Longitude
                            }
                        },
                        Sites = new List<Site>
                        {
                            new Site
                            {
                                NumberOfWindows = 0,
                                NumberOfBedRooms = 0,
                                ApproximateYearBuilt = 1970,
                                PropertyAge = 50,
                                FuelTypeId = 1,
                                ParkingTypeId = 1,
                                FurnishingTypeId = 1
                            }
                        },
                        MarketingInformation = new List<PropertyMarketingInformation>
                        {
                            new PropertyMarketingInformation
                            {
                                MarketingDescription = "", IsBillsIncluded = false
                            }
                        }
                    };
                    await _propertyRepository.InsertAsync(property);
                }
            }

                break;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我如何将这 3 种方法重写为一种通用方法?

fub*_*ubo 10

泛型类型参数的典型用例

public IEnumerable<T> ImportStandardCsv<T>(string fileData)
{
    var bytes = Convert.FromBase64String(fileData.Substring(fileData.IndexOf(',') + 1));
    using (var memoryStream = new MemoryStream(bytes))
    using (var streamReader = new StreamReader(memoryStream))
    using (var csvReader = new CsvReader(streamReader))
        return csvReader.GetRecords<T>();
}
Run Code Online (Sandbox Code Playgroud)

称呼

IEnumerable<PropertyImportDto> result = ImportStandardCsv<PropertyImportDto>(input.FileBase64);
Run Code Online (Sandbox Code Playgroud)