Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert 3 methods in one generic (C#)

Tags:

c#

I have 3 methods that do same stuff. One difference, they work with different models.

Here is the methods code:

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;
    }

And I call it like this

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;
        }
    }

How I can rewrite those 3 methods to one gneric method?

like image 721
Eugene Sukh Avatar asked Jan 28 '26 01:01

Eugene Sukh


1 Answers

Typical use case for generic type parameters

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>();
}

call

IEnumerable<PropertyImportDto> result = ImportStandardCsv<PropertyImportDto>(input.FileBase64);
like image 117
fubo Avatar answered Jan 29 '26 15:01

fubo