0

I most likely didn't describe my Title correctly and am totally lacking the terminology for what I'm trying to do... so if you are reading this and know what I should be asking for.. please share.

So here is my issue. I am creating a DLL. This DLL will of course have several Classes inside. The calling solution will need access to those classes and their various methods.

However, I want to force the calling program to only create an instance of the "Parent\Root" class because I require certain information anytime they are going to use anything that is part of this class (think sort of like a license key but not really).

So from the Client view.. I want it to work like such..

FooDll.Client myFooClient = new FooDLL.Client(myLicensesKey)

myFooClient.Store.PlaceOrder()
myFooClient.Shop.FixWidget()

Now from my DLL side, I was thinking something like this...

public class Client
{
   public StoreLogic Store {get;}  //This is a internal Class that contains the method PlaceHolder
   public ShopLogic Shop {get;}    //This is an internal Class that contains the method FixWidget

   public Client(string LicenseKey)
   {
     set some internal global flags for the entire library
   }
 }

Now I can do this now as long as I declare the classes StoreLogic and ShopLogic as "Public". but if I do that, the client can now do this...

FooDll.StoreLogic myStore = new FooDLL.StoreLogic()

Which is what I don't want.

pfx
  • 20,323
  • 43
  • 37
  • 57
da_jokker
  • 954
  • 2
  • 12
  • 16
  • 1
    Unless I'm missing something, can't you just make your internal classes with the `internal` keyword instead of `public`? – mituw16 Jun 20 '19 at 17:20
  • @3Dave classes marked as (nested) `private` or `internal` can not be exposed as `public` property on a `public` class, here eg. `public StoreLogic Store` on `public class Client`. This results in an inconsistent accessibility build error. – pfx Jun 20 '19 at 18:48

1 Answers1

3

Just mark the constructors of StoreLogicand ShopLogic as internal.
Doing so, only a class contained in the same assembly can instantiate these, like the Client class.

public class StoreLogic
{
    internal StoreLogic()
    {}

    public void PlaceOrder() {}
}

public class ShopLogic
{
    internal ShopLogic() 
    {}

    public void FixWidget() { }
}

public class Client
{
    public StoreLogic Store { get; }
    public ShopLogic Shop { get; }

    public Client(string LicenseKey)
    {
        this.Shop = new ShopLogic();
        this.Store = new StoreLogic();
    }
}
pfx
  • 20,323
  • 43
  • 37
  • 57
  • I'll have to sit down when I have time and try to understand how this works.. BUT IT DOES. The client "windows program" can see the StoreLogic, but it can not call any of the methods directly, can't create an instance of it, and must get through a "new instance" of the Client, which of course required the License.. beautiful!!! – da_jokker Jun 21 '19 at 17:22