HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Dynamic variable - is that best way to dynamically assign?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
dynamicallywaythatdynamicvariableassignbest

Problem

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(source.EmailMessage.User),
                    EmailReplyTo = email.From,
                    ShowReplyButton = true,
                    ThreadUid = source.ThreadUid,
                    IsReply = source.IsReply
                };
            }
...

Solution

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(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(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.

Code Snippets

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

Context

StackExchange Code Review Q#3348, answer score: 3

Revisions (0)

No revisions yet.