samedi 25 février 2017

How to instantiate an association to a service class based on the contract hierarchy?

I want to instantiate an association to a service class based on the contract hierarchy. Consider following code:

public interface IServiceBase
{
    void DoSomething();
}

public interface IService : IServiceBase
{
    void DoSomeOtherthing();
}
public class ServiceBase : IServiceBase
{
    public virtual void DoSomething()
    {
        Console.WriteLine("Base service implementation");
    }
}
public class Service : ServiceBase, IService
{
    public void DoSomeOtherthing()
    {
        Console.WriteLine("New service implementation");
    }

    public override void DoSomething()
    {
        base.DoSomething();
        Console.WriteLine("Child service implementation");
    }
}
public abstract class ClientBase
{
    public IServiceBase Service { get; set; }
}

public class Client : ClientBase
{
    public IService Service { get; set; }
}

I want the Service property in the ClientBase and in Client have the same reference to an instance of Service class. And if someone try to use Service property, somewhere in ClientBase, the Service property refers to and instance of Service class. Is there any design pattern which targets this problem?

Aucun commentaire:

Enregistrer un commentaire