I just wrote that without StackOverflow

In the life of many developers comes the moment, when you write something non-trivial and it just works. And you don’t have to google anything! It’s quite thrilling!

I have been creating a mock object for testing, that could store several graphs at the same time. The graph was very simple, it could have only root (portfolio) and leaves of several types. Linking was done from leaf to root, by setting two properties (a little redundant): parentId and parentName.

I figured that writing a function that would link it automatically without the need to repeat that every time could improve code quality. However, leaves were of different types. They just happen to have the same properties, but no way I could write a generic method that could use it.

Neither of them was inheriting from any other object, but I figured I should go with the interface anyway since I just required for two properties to be there and I know that some databases don’t like when types share a common ancestor:

public interface IPortfolioLinked
    string PortfolioId { get; set; }
    string PortfolioName { get; set; }

I had been using Ploeh.Autofixture before, so I just went with whatever it was providing me and I have created an extension method that would take builder, attach parent symbol to it and return builder of the same type

public static IPostprocessComposer WithLinkedPortfolio(
    this ICustomizationComposer builder,
    PortfolioDTO parent) where T : IPortfolioLinked
    var parentId = PortfolioDTO.CreateId(parent);
    return builder
        .With(x => x.PortfolioId, parentId)
        .With(x => x.PortfolioName, parent.Name);

Overall it’s not impressive. But I’m quite excited about it because:

  1. I’ve just written it and everybody is excited about things they wrote
  2. I haven’t used any external sources. I just knew all of that quite intuitively. Yey!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s