0

I am working within a framework where you do the following:

IMyClass instance = Session.GetAllObjectsOfType("IMyClass")[0];
ILock lock = instance as ILock;
if(lock != null)
{
   lock.Lock();
   instance.DoSomething();
   lock.Unlock();
}

ISaveable saveable = instance as ISaveable;
if(saveable != null)
   saveable.save();

For this to work I have

class MyClass : IMyClass, ISaveable, ILock
{

}

I reality I have something like 8-15 interfaces I need to implement and they need to be accessable by casting the main object. What is the cleanest way to implement this? I looked into the facade patter, but I dont think that can be used here.

5
  • 1
    I don´t understand your question. Let your class implement the interfaces, cast your "mainobject" to the specific interface, call its members and you´re done. There are not so many (actually just one) ways to do this. Commented Nov 8, 2016 at 11:15
  • I was hoping there was a better way then the obvious one. There will be a lot of code going into the realisation the the different interfaces, and the ILock interface does not really need to know about the ISavebale functionalty. I am basically looking for a clean way to do this that does not result in one class with 5k lines of code and 200 public functions. Commented Nov 8, 2016 at 11:17
  • 1
    Google "Interface Segregation Principle" - it's part of the SOLID principles. Commented Nov 8, 2016 at 11:20
  • Just to be clear: what exactly are you asking? Are you asking how to write a class such as MyClass to be compatible with the given framework? Or are you asking how to write and/or improve the framework? And related to that, do you consider IMyClass to be part of the framework, or is it part of your code and can you change it? Commented Nov 8, 2016 at 11:20
  • I am asking, is there a better way of writing MyClass then above? The framework is done and can not be changed, IMyClass is not part of the framework, it is my implementations of something that should work within the framework, The best solution is to use toadflakz solution, but as MyClass will contain a huge amount of wrapper functions I was hoping there would be an even cleaner solution, but I can not think of one. Commented Nov 8, 2016 at 11:34

1 Answer 1

3

Based on your comment:

I was hoping there was a better way then the obvious one. There will be a lot of code going into the realisation the the different interfaces, and the ILock interface does not really need to know about the ISavebale functionalty. I am basically looking for a clean way to do this that does not result in one class with 5k lines of code and 200 public functions

If you want a class to implement the interface the class will need to inherit the interface but the functionality could be injected and wrapped (aka Facade).

This would be the cleanest way to achieve your goal of generic but separate code classes for ISaveable, ILock etc.

For example:

public MyClass : IMyClass, ISaveable, ILock
{
    private readonly ISaveable _saveableImplementation;
    private readonly ILock _lockImplementation;

    public MyClass(ISaveable saveableImplementation, ILock lockImplementation)
    {
        _saveableImplementation = saveableImplementation;
        _lockImplementation - lockImplementation;
    }


    public void ISaveable.Save()
    {
        _saveableImplementation.Save();
    }

    public void ILock.Lock()
    {
        _lockImplementation.Lock();
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.