Where to put the static GetItems method? + inheritance issue

I have an Item class:

public class Item {
    public long Id {get; protected set;}
    public string Name {get; protected set;}
}

and now I want to add a function that extracts elements from db according to filters. This should be a static method returning Item []:

public static Item[] GetItems(long? itemId, string itemName) {
    //Do Search in the db for items with Id=itemId (in case itemId is not null) and
    //with Name=itemName (in case itemName is not null)

    return itemsList.ToArray();
}

The question is where to put this method? 1. Should I create a new class for this? What will I call this class? 2. Should I put this method in the Item class?

Another question: In case I want to inherit the Item class. How to force child classes to implement such a GetItems method?

+3
source share
4 answers

. ItemRepository, Item DAL. DAL, , , - .

, (ex: IItemRepository), , , . , , .

: , , , , . , .

+2

GetItems ( ) , Item ( ). , . ( factory), ItemUtility GetItems.

: , . , LoadItem, SpecialItem: Item, SpecialItem.LoadItem() Item.LoadItem(). Item.LoadItem() overridable, "" ( SpecialItem ).

, , SpecialItem.

- IItem LoadItem ( ).

+1

. , , " ".

, , .

0

- , ? GetItems , . :

1) . ItemManager, . -

2) , ItemManager .

3) Mark the method that you created in step 2 as virtual if you want the child classes to redefine the method to provide their own implementation. If you want to force them to override and not need to be implemented in the base class, then mark the base class as abstract, so that the child class must override it

0
source

All Articles