0

I'm writing a custom library and some interfaces in c# and it will be reused by other developers. Some of the methods of my library always require certain lines to work so I'd like to automatically write these lines when other developers implement my interface. I've been looking for snippets, however I've seen that they must be imported by the snippet manager tool, but I'd like to step over this process and write them automatically when implementing the interface with ALT+CR.

public interface IFaceTranslator
{
    List<byte[]> StartConfigOrder(Message msg_to_send)
    {
        //I'd like to automatically write these two lines when developer implements this function.
        List<byte[]> commands = new List<byte[]>();
        /*CODE FROM OTHER DEVELOPER*/
        return commands;
    }
}

Thanks.

6
  • Have you considered using Source-Generators ? Commented Jul 11, 2022 at 7:25
  • @Fildor I'd prefer to display the lines to the developer in plain text for an easier understanding. Commented Jul 11, 2022 at 7:41
  • I doubt you can have the cake and eat it. For one: If it were possible to do that, nothing would stop the implementor from deleting those lines again. So if your implementation depends on them being present, you better use something different, even if it is less verbose. Commented Jul 11, 2022 at 8:05
  • And secondly: If that's actually like it is in the snippet in the question: Why does it need to have those lines? Why do you care? You define the interface, the contract, the implementation is none of your business, really unless the contract is broken. But it's the implementors' responsibility to implement it. If the client has a different method to create that List<byte[]> , he'll be super annoyed by your "utility". Commented Jul 11, 2022 at 8:08
  • @Fildor yeah, I was trying to write a really easy guided library, but I will leave it for a second version in a future. Commented Jul 11, 2022 at 8:12

1 Answer 1

2

You could also change your design. For example, you could provide an abstract class that takes care of common setup and completion logic. Then use protected abstract methods that the implementers define (effectively following the template method design pattern).

public abstract class AbstractFaceTranslator: IFaceTranslator
{
    public List<byte[]> StartConfigOrder(Message msg_to_send)
    {
        List<byte[]> commands = new List<byte[]>();
        return StartConfigOrderUsing(commands, msg_to_sent);
    }

    protected abstract StartConfigOrderUsing(List<byte[]> commands, Message message);
}

However, you're assuming that the implementer would need to use a List<byte[]>. As per the Interface Segregation Principle, you'd want to only expose only what's necessary, so if IEnumerable<byte[]> works just as well, then that's preferable.

Then the implementer might choose some other data-structure, for example, a byte[][] or List<byte[]>(commandSize) if they know the length up front.

The other benefit of using the template method pattern is that if your boilerplate code ever changes, the consumers of your library simply update to the latest version of your library instead of having to update code in many different places. Of course that advantage might not actually be useful to you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.