したがって、以下に示す 2 つの方法があります。
方法 1
private IEnumerable<object> CreateCentreViewModelForExport(IQueryable<CentreTranslation> centreTranslation)
{
return centreTranslation.Select(s => new
{
id = s.Centre.id,
centreTranslationId = s.id,
name = s.Centre.name,
number = s.Centre.number,
date_opened = s.Centre.date_opened,
address_line_1 = s.address_line_1,
address_line_2 = s.address_line_2,
address_line_3 = s.address_line_3,
city = s.city,
county = s.county,
country = s.Centre.Country.name,
//country_id = s.Centre.country_id,
translatedCountry = s.country,
postcode = s.postcode,
hidden = !(s.Centre.CentreStatus.Where(w => w.environment_id == 4).FirstOrDefault().active),
about = s.about,
virtualTour = s.Centre.virtual_tour,
directions = s.directions,
phone = s.Centre.phone,
fax = s.Centre.fax,
email = s.Centre.email,
lat = s.Centre.position.Latitude,
lng = s.Centre.position.Longitude,
imageCount = s.Centre.image_count,
translatedCentreName = s.name,
amenities = s.amenities ,
features = s.FeatureTranslations.Select(s2 => new FeatureViewModel()
{
id = s2.id,
name = s2.Feature.name,
selected = s2.selected
}),
businessCentreAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.BusinessCentre).FirstOrDefault().about,
officeSpaceAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.OfficeSpace).FirstOrDefault().about,
virtualOfficeAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.VirtualOffice).FirstOrDefault().about,
meetingRoomsAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.MeetingRooms).FirstOrDefault().about,
businessLoungeAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.BusinessLounge).FirstOrDefault().about,
dayOfficeAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.DayOffice).FirstOrDefault().about,
language_group = s.Language.language_group,
culture = s.Language.cuture
});
}
方法 2
private IQueryable<CentreViewModel> CreateCentreViewModel(IQueryable<CentreTranslation> centreTranslation)
{
return centreTranslation.Select(s => new CentreViewModel()
{
id = s.Centre.id,
centreTranslationId = s.id,
name = s.Centre.name,
number = s.Centre.number,
date_opened = s.Centre.date_opened,
address_line_1 = s.address_line_1,
address_line_2 = s.address_line_2,
address_line_3 = s.address_line_3,
city = s.city,
county = s.county,
//country = s.Centre.Country.name,
country_id = s.Centre.country_id,
translatedCountry = s.country,
postcode = s.postcode,
hidden = !(s.Centre.CentreStatus.Where(w => w.environment_id == 4).FirstOrDefault().active),
about = s.about,
virtualTour = s.Centre.virtual_tour,
directions = s.directions,
phone = s.Centre.phone,
fax = s.Centre.fax,
email = s.Centre.email,
lat = s.Centre.position.Latitude,
lng = s.Centre.position.Longitude,
imageCount = s.Centre.image_count,
translatedCentreName = s.name,
amenities = s.amenities,
features = s.FeatureTranslations.Select(s2 => new FeatureViewModel()
{
id = s2.id,
name = s2.Feature.name,
selected = s2.selected
}),
businessCentreAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.BusinessCentre).FirstOrDefault().about,
officeSpaceAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.OfficeSpace).FirstOrDefault().about,
virtualOfficeAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.VirtualOffice).FirstOrDefault().about,
meetingRoomsAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.MeetingRooms).FirstOrDefault().about,
businessLoungeAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.BusinessLounge).FirstOrDefault().about,
dayOfficeAbout = s.ProductTranslations.Where(w => w.Product.id == (int)Products.DayOffice).FirstOrDefault().about
});
}
ご覧のとおり、多くの重複コードがあります。2 番目のメソッドは厳密に型指定されたビュー モデルを返しますが、最初のメソッドは 2 つの追加プロパティ (language_group と culture) が含まれているため、オブジェクトを返します。
2 番目の方法は、MVC ビューの作成に使用され、2 番目の方法は、Excel 関数へのエクスポートに使用されます。重複を最小限に抑えるためにこれをリファクタリングする最良の方法は何ですか?