vrwim
vrwim

Reputation: 14300

How can I optionally include an external library?

I have a PCL that I want to contain a bunch of base classes, so I do not have to make them again for each project. Now I am contemplating adding in Facebook, as I will have to reference an external dll each time I want to use my PCL in a project, even if it's a project with only a few screens, because I would have build errors if I don't.

For those saying that's not an issue: I am planning on adding even more external dll's that I don't need every time.

How can I solve this? I want to include the code to use this dll in my PCL, but I don't want to be forced to include the dll each time I use the PCL.

Upvotes: 0

Views: 133

Answers (1)

C.Evenhuis
C.Evenhuis

Reputation: 26446

The problem here is that you probably want to use types from the external library in your code, and you can't do that without referencing the library.

A way around this problem is by using reflection, but your code will become much more complex and you wish you didn't.

Another solution is to:

  • Create an interface for each external dependency in your "common PCL" (ie. ISocialMediaPlatform for the facebook).
  • Create a new PCL for each external dependency, that references both your "common PCL" and the external library, and has a class that implements one of these interfaces (ie. FacebookSocialMediaPlatform : ISocialMediaPlatform)
    • This implementation can then reference the external dependency and use its types directly
  • Inject the implementation of each interface into your "common PCL" using reflection or a Dependency Injection framework

This does add another layer of complexity, but as a side effect it also makes your common PCL code testable.

Finally, the solution I personally would prefer, is to not have a huge "common PCL" at all, but to split it into a few smaller ones that fulfill one specific role.

Upvotes: 1

Related Questions