I have 2 library projects: AProject and BProject and one web project: WebProject.
AProject has a class:
public class WorkerManager {
public Worker Get(long workerId) {
....
....
}
public Worker Get(Guid companyId, long workerId) {
....
....
}
}
I would like classes from BProject to be able to use the first version of the Worker Get:
WorkerManager a=new WorkerManager ();
a.Get(8);
And I would like this method to be hidden for the web project.
If I make the Worker Get(long workerId) internal, then BProject will not be able to use this method, and if I make it public the web project gets access to this method.
How can I prevent access to the web project and allow access to the BProject?
It sounds like you need Friend Assemblies.
They basically allow you to make your class internal, but specify that
BProjectshould have access to the internal classes ofAProject.Update
I’ve never actually had to use Friend Assemblies, so I won’t be able to give you any better direction than Google can.
Other people have mentioned using interfaces, and I agree that this is generally a good approach. Whether or not it works for you will depend on the structure of your code, though. Let’s say you move the
WorkerManagerclass to a new Project (C). Project C depends on Project B. Project A depends on both C and B. WebProject depends on A and B.Project B now defines an interface like the following:
And WorkerManager in Project C implements the interface:
Now Project A can produce
IWorkerManagers via a factory, so your Web Project never has to saynew WorkerManager().Now it’s still possible for Project A to access the other methods:
… but Web Project can only access what’s been defined by the interface:
This illustrates one way that you might achieve what you’re looking for. You could find better solutions by leveraging Dependency Injection, but this explanation is getting well beyond the scope of the original question.