none
Implementing interfaces RRS feed

  • Question

  • I have an interface and class like this

    public interface ICustomerEx
    {
       void Load(DataRow row);
    }
    
    public class Customer:ICustomerEx
    {
       public string Name{get;set;}
       public string Address{get;set;}
    
       void Load(DataRow row)
       {
    	Name = (string)row["name"];
    	Address = (string)row["address"];
       }
    }

    Now I'm building this as a class library and add to another project as a reference.
    In that project there's a class called UiCustomer that implements from the same refereced interface ICustomerEx
    In this class it has its own property and load that property from its load method.

    public class UiCustomer:ICustomerEx
    {
       public string Telephone{get;set;}
    
       void Load(DataRow row)
       {
    	Telephone=(string)row["tele"];
       }
    }

    Now is there any way to implement my first class's(that build as a class library) Load method to load Ui project's properties after loading it's own properties by using like Dependency Injection.

    eg.

    public class Customer:ICustomerEx
    {
       public string Name{get;set;}
       public string Address{get;set;}
    
       void Load(DataRow row)
       {
    	Name = (string)row["name"];
    	Address = (string)row["address"];
    
    	//Call load methods in other places that Interface implemented
       }
    }


    Saturday, July 6, 2013 10:48 AM

Answers

  • Thanks for your reply. My requirement is little different with your answer. This customer instance need to create from the reference dll side. i.e From a class in the same assembly that customer class exists. My requirement is to create common class library to create customer instance and load it. Other than that when it referenced to any assembly it should able to load it's own properties also from the referenced assembly.
    • Marked as answer by Sanyon_d Wednesday, July 10, 2013 2:51 PM
    Monday, July 8, 2013 2:06 AM

All replies

  • Hello Sanyon_d,

    I don't think it makes sense (logically in regards to the OO programming) to do what you want to achieve.  It would make more sense to me to have it structured as:

    class library contains ICustomerEx and Customer:ICustomerEx

    and your UI project to contain UICustomer:Customer.  This way you can have an override on the Load method that can then access the Customer object's Load method.

    For Example:

        // in your class library
        public interface ICustomerEx
        {
            void Load(DataRow row);
        }
    
        public class Customer : ICustomerEx
        {
            public string Name { get; set; }
            public string Address { get; set; }
    
            public virtual void Load(DataRow row)
            {
                Name = (string)row["name"];
                Address = (string)row["address"];
            }
        }
    
        // in your UI project
        internal class UiCustomer : Customer
        {
            public string Telephone { get; set; }
    
           public override void Load(DataRow row)
           {
                Telephone = (string)row["tele"];
    
                base.Load(row);
            }
        }

    Cheers,

    Jeff


    Jeff

    Sunday, July 7, 2013 10:05 PM
  • You can use reflection to determine all the types that implement the given interface but what exactly are you trying to accomplish here?
    Sunday, July 7, 2013 11:52 PM
  • You can use reflection to determine all the types that implement the given interface but what exactly are you trying to accomplish here?

    The point of writing an interface is to not have to deal with what class that is. It can be anything as long as it implements the interface.

    If it implements an interface, it has coded out certain Functions. And those functions is all you care about.

    Also UICostumer does not makes any real sense if it does not inherit from Customer. It has all the signs of a more specilised Customer (a suffix, an extra Telephoen field). So:

    ICustomerEx demands you to code out void Load(DataRow row);

    Customer implements ICustomerEx and has two Properties

    UiCustomer inherits from Customer, adds a property and overwrites ICustomerEx.Load().


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    Monday, July 8, 2013 12:54 AM
  • Jeff has it right, gets my vote.

    Although, I would prefer the call to base.Load(row) be the first statement in the overridden Load method prior to any additional statements.  Memory fails as to whether there is a reason or just personal preference.

    Monday, July 8, 2013 1:33 AM
  • Thanks for your reply. My requirement is little different with your answer. This customer instance need to create from the reference dll side. i.e From a class in the same assembly that customer class exists. My requirement is to create common class library to create customer instance and load it. Other than that when it referenced to any assembly it should able to load it's own properties also from the referenced assembly.
    • Marked as answer by Sanyon_d Wednesday, July 10, 2013 2:51 PM
    Monday, July 8, 2013 2:06 AM