I have a list of objects implementing an interface, and a list of that interface:
public interface IAM
{
int ID { get; set; }
void Save();
}
public class concreteIAM : IAM
{
public int ID { get; set; }
internal void Save(){
//save the object
}
//other staff for this particular class
}
public class MyList : List<IAM>
{
public void Save()
{
foreach (IAM iam in this)
{
iam.Save();
}
}
//other staff for this particular class
}
The previous code doesn't compile because the compiler requires all the interface members to be public.
internal void Save(){
But i don't want to allow the from outside my DLL to save the ConcreteIAM
, it only should be saved through the MyList
.
Any way to do this?
Update#1: Hi all, thanks for the answers so far, but none of them is exactly what i need:
The interface needs to be public because it is the signature the client from outside the dll will use, along with ID
and other properties i didn't bother to write in the example to keep it simple.
Andrew, I don't think the solution is create a factory to create another object that will contain the IAM
members + Save. I am still thinking... Any other ideas?
Make another interface that is internal, and use explicit implementation for the method.
Maybe you want to separate the saving of your items into a different set of classes that are internal to your assembly:
I don't think you should be using a interface here maybe you should be using an abstract base something like.:
Will still allow you to do this:
I had similar situation and thought this could help. (not sure if this is not needed at this time or not)
This is a legit case for me: in case suppose there is an interface which has some API which is internal to the DLL and some API can be accessed from outside the DLL. Since interface is an abstract class, instead of defining as an interface you can define as a abstract class.
I was wondering on the same issue here, and stumbled upon this question...
As i thought on this, i understood i don't need the internal method in the Interface in the first place.
I can access it through my Concrete Class, and leave the Contract for the Out-Side code.
In your example:
Why don't you use inner classes to control accessibility of your methods?
Example:
Your primary assembly
Your secondary assembly
This pattern will still let you implement
Save()
method in other assemblies but onlyItemCollection
which is inner class ofItem
can call it. Brilliant, isn't it?