Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# Interface implementation

Options
  • 23-02-2018 5:31pm
    #1
    Registered Users Posts: 6,252 ✭✭✭


    So, in college this week we're doing the repository pattern, and we've been given an assignment as follows: (well one part of it): Note, there's been little emphasis on interfaces lately.

    Create a repository interface with basic crud methods - IRepo and for the sake of an argument this interface contains a delete method.

    Create two other interfaces to represent two different tables (say car and part) and implement IRepo, say called ICar and IPart

    Create a DualModel Repository, say CarsAndParts Class that implements both ICar and IPart.

    I've done this, and explicitly implemented say the delete method, both of which look like:
      void IRepository<Car>.DeleteItem(int itemID)
            {
                Car item = _context.Cars.Where(p => p.ID == itemID).FirstOrDefault();
                _context.Cars.Remove(item);
            }
    
            void IRepository<Part>.DeleteItem(int itemID)
            {
                Supplier item = _context.Part.Where(p => p.ID == itemID).FirstOrDefault();
                _context.Part.Remove(item);
            }
    

    However, in my controller when I've instantiated this class I can't get access to these methods. Am I missing something obvious here or what's the story


Comments

  • Registered Users Posts: 83 ✭✭crazy eyes


    Should the method not use the public access modifier

    e.g
    void IRepository<Part>.DeleteItem(int itemID)
    {
    Supplier item = _context.Part.Where(p => p.ID == itemID).FirstOrDefault();
    _context.Part.Remove(item);
    }

    should be

    public void IRepository<Part>.DeleteItem(int itemID)
    {
    Supplier item = _context.Part.Where(p => p.ID == itemID).FirstOrDefault();
    _context.Part.Remove(item);
    }


  • Registered Users Posts: 6,252 ✭✭✭Buford T Justice


    The compiler complains that the modifier public is not valid for this item.


  • Registered Users Posts: 895 ✭✭✭Dubba


    Could it be that your Controller doesn't know if it's a Car or a Part object is required?

    https://stackoverflow.com/a/2669140


  • Registered Users Posts: 2,149 ✭✭✭dazberry


    Because you've had to implemented the interface explicitly, things work slightly differently. The members are not accessible through their qualified names so are essentially private, BUT because they can be accessed via their interfaces they are then public.

    So CarsAndParts might appear to have no methods but:

    CarsAndParts cp = new CarsAndParts();
    ICar car = cp;
    car.DeleteItem(0); <-- is valid

    IParts parts = cp;
    parts.DeleteItem(0); <-- is also valid

    D.


Advertisement