3

I was wondering if it makes sense to have objects inherit from a class that implements the interface when dealing with dependency injections example

public interface IPeople
    {
        string Name { get; set; }
        int Age { get; set; }
        string LastName { get; set; }

    }

public class CPeople : IPeople
    {..implemented IPeople Methods..}

This way i only need to implement the interface in one place. I'm just not sure if this would be considered loosly coupled.

public class Dad : CPeople
    {
    }



public class Mom : CPeople
    {
    }

so that inside my controller i would have

public class Parent

{

    IPeople objMom;
    IPeople objDad;
    Parents m_Parent;

    public void factoryMethod()
    {

        objMom = new Mom();
        objMom.Age = 32;
        objMom.Name = "Jane";
        objMom.LastName = "Doe";

        objDad = new Dad();
        objDad.Age = 25;
        objDad.Name = "John";
        objDad.LastName = "Doe";

        m_Parent = new Parents(objMom,objDad);

    }
    public override string ToString()
    {                      
        return m_Parent.Mom.Name + " " + m_Parent.Mom.LastName +  " is " + m_Parent.Mom.Age + " years of age, " + m_Parent.Dad.Name + " " + m_Parent.Dad.LastName + " aged " + m_Parent.Dad.Age.ToString();
    }
6
  • 13
    Please, please, please, do not use hungarian notation in C#. It makes no sense, since everything are classes and objects, and it just hurts to look at it. Commented Dec 13, 2009 at 20:16
  • If your Mom and Dad class are identical, why both creating two separate classes for it? You could achieve the same by having one class with a bool/enum saying whether it is the mother or father. Maybe your example is too simplified to be meaningful? Commented Dec 13, 2009 at 20:17
  • 1
    @Konamiman: There are times when Hungarian notation is useful (apps Hungarian vs System Hungarian - en.wikipedia.org/wiki/…). I agree though that this is not one of them. Commented Dec 13, 2009 at 20:20
  • 1
    @Mark Byers,@Konamiman - 99% of people misuse Hungarian notation unfortunately. A lot of people use it to show type in statically typed languages when the original use of Hungarian notation was to show where it was being used (App Hungarian notation). Commented Dec 13, 2009 at 20:27
  • 4
    Easy, folks. Hungarian notation is one of those things that goes in and out of favor every few years and gets endlessly discussed as if it's important. Commented Dec 13, 2009 at 20:37

1 Answer 1

3

Yes, this is considered loosely coupled since the controller does not need to have any knowledge of the inner objects beyond the interface definition.

If you care that Mom/Dad be kept separate, you could implement interfaces for just those (even if they are empty) and use those to ensure that parents is both a IMom and an IDad.

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.