Reputation: 29658
I have a situation where I've exposed a plugin architecture to my class library, and I want others to be able to implement the interfaces I've created and have their custom implementations be used by the class.
I'm using dependency injection (specifically constructor injection) to accomplish this.
This works fine, except for the fact that I've tried to design the application with two conflicting goals:
internal
on the setters and such to ensure no consumers can modify itThese two goals are conflicting because there is no way for my API (or C# for that matter) to grant internal
access (via InternalsVisibleTo
or similar) to the plugin assemblies, but not to general consumers.
Suppose we have three assemblies. My class (Class
), a plugin assembly (Plugin
), and a consumer assembly (Consumer
).
An interface, IReturnValue
specifies a return value of the API (some method or class in the Class
assembly). The problem is this: I want Plugin
to be able to set read-only (internal
) properties and indexers on any given instance of IReturnValue
, but I don't want Consumer
code to be able to do this.
Since a plugin system is in play, I can't know ahead of time what assemblies to grant InternalsVisibleTo
to.
In this situation, should I really care about consumers modifying return types? It seems like it's their own fault if they do. And I can't think of any decent solution to solving this problem.
Upvotes: 2
Views: 72
Reputation: 174329
As I already said in my comment, I am not sure I understood your question correctly.
I understood your question like this:
You have a class A that is used by Consumers and by Implementors. Consumers should only be able to get data, implementors should additionally be able to set data.
In this case, I would create one interface that is modeled to the consumers needs with only read access and one with read/write access for the implementors.
UPDATE after question update:
The read only interface for the consumer already exists: IReturnValue
and should be used for this purpose.
All you need to do now is to create an interface that is either write only or that is read/write, depending on the needs of the API.
Upvotes: 3