2

我有一个扩展方法,它检查对象的类型,然后填充它的成员属性

public static void LoadMeeting<T>(this T entity, IMeetingRepository meetingRepository) 
where T: MyEntity
            {
                var agenda = entity as Agenda;

                if (agenda != null)
                {
                    agenda.Meeting = meetingRepository.GetMeetingById(agenda.MeetingId);
                }

                var participant = entity as Participant;

                if (participant != null)
                {
                    participant.Meeting = meetingRepository.GetMeetingById(participant.MeetingId);
                }

            }

我可以进一步将其重构为这样的东西以使其更通用吗?

 public static void LoadMeeting<T>(this T entity, IMeetingRepository meetingRepository) where T : MyEntity
        {
            var obj = entity as Agenda || entity as Participant;

            if (obj != null)
            {
                obj.Meeting = meetingRepository.GetMeetingById(obj.MeetingId);
            }
        }
    }

PS:我不想把对象的属性Meeting放在基类(MyEntity)中

4

2 回答 2

6

我个人会使用重载:

public static void LoadMeeting(this Agenda agenda, IMeetingRepository meetingRepository) 
{
    if (agenda != null)
    {
         agenda.Meeting = meetingRepository.GetMeetingById(agenda.MeetingId);
    }
}
public static void LoadMeeting(this Participant participant, IMeetingRepository meetingRepository) 
{
    if (participant != null)
    {
        participant.Meeting = meetingRepository.GetMeetingById(participant.MeetingId);
    }
}

Meeting除非您有一些提供该属性的共享合同(实现了基类或接口),否则您无法使用单个通用方法来做到这一点。

另一种方法是创建一个共享接口,即:IMeeting,然后限制为:

public interface IMeeting
{
   public Meeting Meeting { get; set; }
   public int MeetingId { get; }
}

然后你可以写:

public static void LoadMeeting<T>(this T entity, IMeetingRepository meetingRepository) 

其中 T:IMeeting { if (entity != null) { entity.Meeting = meetingRepository.GetMeetingById(entity.MeetingId); } }

当您使用 EF 时,您可以在部分类中实现此接口:

public partial class Agenda : MyEntity, IMeeting
{
}
于 2013-10-30T00:13:39.557 回答
1

您可能希望尽早退出该方法,以防您必须处理一个类及其基类。

public static void LoadMeeting<T>(this T entity, IMeetingRepository meetingRepository) where T: MyEntity
        {
            var agenda = entity as Agenda;

            if (agenda != null)
            {
                agenda.Meeting = meetingRepository.GetMeetingById(agenda.MeetingId);
                return;
            }

            var participant = entity as Participant;

            if (participant != null)
            {
                participant.Meeting = meetingRepository.GetMeetingById(participant.MeetingId);
                return;
            }
        }

您也可以考虑使用表格方法:

    public static void LoadMeeting<T>(this T entity, IMeetingRepository meetingRepository) where T: MyEntity {
        var name = entity.GetType().Name;
        if (Table.ContainsKey(name)) {
            Table[name](entity, meetingRepository);
            }
        }

表在哪里:

static Dictionary<String, Action<MyEntity, IMeetingRepository>> Table = new Dictionary<String, Action<MyEntity, IMeetingRepository>>();

它的初始化如下:

        Table.Add("Agenda", (agenda, meetingRepository) => { 
            ((Agenda)agenda).Meeting = meetingRepository.GetMeetingById(((Agenda)agenda).MeetingId); }); 
        Table.Add("Participant", (participant, meetingRepository) => { 
            ((Participant)participant).Meeting = meetingRepository.GetMeetingById(((Participant)participant).MeetingId); }); 

显然,您的表必须是静态的并且可用于您的扩展方法(例如在包含类中)。

于 2013-10-30T01:10:27.657 回答