c# - 重构重复的 Lambda 表达式

标签 c# asp.net-mvc lambda

所以我们有如下所示的两种方法:

方法一

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

方法二

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

可以看出有很多重复的代码。第二种方法返回强类型 View 模型,而第一种方法由于包含两个额外属性(language_group 和 culture)而返回一个对象。

第二种方法用于填充 MVC View ,第二种用于导出到 Excel 函数。 重构它以最大限度地减少重复的最佳方法是什么?

最佳答案

我会创建一个 DTO 类并在其上设置一个接受 IQueryable centerTranslation 的 setter 方法。然后,您将对象传递给类并在该类中设置所有这些值,然后将 dto 传递回您在那里的原始方法。

    public class SomeDto
    {
        //All of the properties your setting in the other method

        public void SetDto(IQueryable<CentreTranslation> centreTranslation)
        {
            //call methods that set all the properties
        }

        private SetAddress(IQueryable<CentreTranslation> centreTranslation)
        {
            //set only address properties
        }

我还会为类型创建更小的 setter 方法,例如与地址有关的所有内容,在名为 SetAddress 的 dto 对象上创建一个私有(private)方法,然后继续下去。

拥有 DTO 对象后,您可以使用 Automapper 等工具直接从 DTO 对象映射到 ViewModel 对象。这将为您提供最大的灵 active ,以便在整个应用中进行更多重构。

 private ViewModel createViewModel(Dto)

 {
     return Mapper.Map(Dto, ViewModel);    
 }

关于c# - 重构重复的 Lambda 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14752369/

相关文章:

c# - 如何在运行时加载 Blazor 页面?

c# - 获取特定页面类型的所有子级

c# - 如何从 session 列表中获取特定信息

asp.net-mvc - 如果使用协议(protocol) "https"的安全页面,则不会从 CDN URL 加载 js 和 css

c# - 有了区域,我可以轻松地将我的应用程序分解成不同的模块吗?

c# - 如何根据目标属性名称在 Automapper 中执行字符串查找?

c# - vs2010 .net 4.0 到 .net 3.x

c# - ASP.NET MVC : Verify that editing record is allowed (ownership)

java - 将函数引用作为参数传递

java - 使用 lambda 表达式按降序对二维数组进行排序