d:DesignInstance with an interface type

前端 未结 4 590
-上瘾入骨i
-上瘾入骨i 2020-12-06 09:39

I\'m binding an UI to an interface (which is implemented by several presenters, not accessible from the UI assembly).

I really like d:DesignInstance in designer beca

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 09:58

    @Olivier: This is one of the best scenarios where you can make use of "Adapter Design Pattern"

    Note: I have no idea on Resharper, I am a C# & .NET developer. but I understand there is compatibility problem based on your explanation. below is the possible solution that you can try.

    1. Write one C# class that implement the Interface type you need.
    2. Implement the interface in the class however do not write the entire implementation on your own. instead call the other class methods that implement the interface within the implemented methods.
    3. start using the class that you have written.
    4. the class that you have written to get the compatibility is known as Adapter.

    sample code:

    class Program
    {
        static void Main(string[] args)
        {
            // create an object of your adapter class and consume the features.
            AdapterInterfaceUI obj = new AdapterInterfaceUI();
    
            // Even though you have written an adapter it still performs the operation in base class
            // which has the interface implementation and returns the value.
            // NOTE : you are consuming the interface but the object type is under your control as you are the owner of the adapter class that you have written.
            Console.WriteLine(obj.DisplayUI());
            Console.ReadKey();
        }
    }
    
    
    #region code that might be implemented in the component used.
    public interface IinterfaceUI
    {
        string DisplayUI();
    }
    
    public class ActualUI : IinterfaceUI
    {
        //Factory pattern that would be implemented in the component that you are consuming.
        public static IinterfaceUI GetInterfaceObject()
        { 
            return new ActualUI();
        }
    
        public string DisplayUI()
        {
            return "Interface implemented in ActualUI";
        }
    }
    #endregion
    
    
    #region The adapter class that you may need to implement in resharper or c# which ever works for you.
    public class AdapterInterfaceUI : ActualUI, IinterfaceUI
    {
        public string DisplayUI()
        {
            return base.DisplayUI();
        }
    }
    #endregion
    

    I think this solution will help you out.

提交回复
热议问题