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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C# Interface implementation

  • 23-02-2018 4:31pm
    #1
    Registered Users, Registered Users 2 Posts: 6,262 ✭✭✭


    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, Registered Users 2 Posts: 84 ✭✭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, Registered Users 2 Posts: 6,262 ✭✭✭Buford T Justice


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


  • Registered Users, Registered Users 2 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, Registered Users 2 Posts: 2,152 ✭✭✭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