3
\$\begingroup\$

Normally I could just declare the 'email' variable as the base class for both of EmailInbound & EmailOutbound, then cast-up to the inherited class. However, I don't think when defining EF codefirst classes you can use inherited classes, only POCOs. I could be wrong. Hence I've used the dynamic keyword. Could I use Object() instead? Some direction appreciated here...

             if (source.ActionTypeId == (int)ActionTypes.EmailInbound  ||
                source.ActionTypeId == (int)ActionTypes.EmailOutbound)
            {
                dynamic email;
                if ( source.ActionTypeId == (int)ActionTypes.EmailInbound )
                {
                    email = source.EmailMessage.EmailInbound;
                }
                else
                {
                    email = source.EmailMessage.EmailOutbound;
                }

                return new ActionModel
                {
                    ActionTypeId = (ActionTypes)source.ActionTypeId,
                    Content = email.Body,
                    Created = email.DateSent,
                    GravatarUrl = HtmlHelperExtensions.getGravatarUrl(email.FromAddress, 25),
                    Subject = email.Subject,
                    User = Mapper.Map<User, UserModel>(source.EmailMessage.User),
                    EmailReplyTo = email.From,
                    ShowReplyButton = true,
                    ThreadUid = source.ThreadUid,
                    IsReply = source.IsReply
                };
            }
...
\$\endgroup\$
2
\$\begingroup\$

I think it is quite defendable to use dynamic here. I'm not too familiar with EF, but it could very well be that you can't make the objects type-compatible.

Even if you could, I would not do it if it was only to make this piece of code work.

Declaring email as object will not help in any way.

The only alternative I can think of is to have two code paths for both cases:

            if ( source.ActionTypeId == (int)ActionTypes.EmailInbound )
            {
                email = source.EmailMessage.EmailInbound;
                return new ActionModel
                {
                    ActionTypeId = (ActionTypes)source.ActionTypeId,
                    Content = email.Body,
                    Created = email.DateSent,
                    GravatarUrl = HtmlHelperExtensions.getGravatarUrl(email.FromAddress, 25),
                    Subject = email.Subject,
                    User = Mapper.Map<User, UserModel>(source.EmailMessage.User),
                    EmailReplyTo = email.From,
                    ShowReplyButton = true,
                    ThreadUid = source.ThreadUid,
                    IsReply = source.IsReply
                };
            }
            else
            {
                email = source.EmailMessage.EmailOutbound;
                return new ActionModel
                {
                    ActionTypeId = (ActionTypes)source.ActionTypeId,
                    Content = email.Body,
                    Created = email.DateSent,
                    GravatarUrl = HtmlHelperExtensions.getGravatarUrl(email.FromAddress, 25),
                    Subject = email.Subject,
                    User = Mapper.Map<User, UserModel>(source.EmailMessage.User),
                    EmailReplyTo = email.From,
                    ShowReplyButton = true,
                    ThreadUid = source.ThreadUid,
                    IsReply = source.IsReply
                };
            }

But I think the solution with dynamic is better. You can't catch all errors at compile time. I would reccomend a unit test (two really) so that if anybody changes the database schema, this is detected at build time.

\$\endgroup\$
  • \$\begingroup\$ Sorry didn't get back to you sooner & thanks for the answer. I was also thinking of using a IEmailBase interface which both classes implement. Then I can set the interface to point to the correct object. Is this overhead? \$\endgroup\$ – jaffa Jul 26 '11 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.