GetItems 静态方法放在哪里? +继承问题

发布于 2024-10-25 20:09:32 字数 593 浏览 1 评论 0原文

我有 Item 类:

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

现在我想添加一个根据过滤器从数据库检索项目的函数。这应该是返回 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();
}

问题是这个方法放在哪里? 1.我应该为此创建一个新课程吗?我该如何称呼这个班级? 2.我应该把这个方法放在Item类中吗?

另一个问题: 如果我想继承 Item 类。如何强制子类实现这样的 GetItems 方法?

I have 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 retrieve items from the db according to filters. This should be static method that returns 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? How will I call this class?
2. should I put this method in Item class?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沐歌 2024-11-01 20:09:32

我会推荐一个简单的存储库模式实现。您可以创建一个名为 ItemRepository 的类,它了解您的 Item 对象和 DAL 实现。存储库只需调用 DAL 即可获取所需的任何数据,然后将业务对象返回给使用者。

这实现起来非常简单,如果您创建一个接口来配合它(例如:IItemRepository),单元测试就会变得非常容易,因为您将能够模拟存储库并将其传递任何消耗它的对象。如果你把所有东西都静态化,那么测试就会困难得多,所以我不推荐它。

至于继承:如果您想定义每个存储库对象上都可用的通用方法签名和属性,则使用接口来准确指定每个存储库应具有的共同点。如果您想提供基本存储库框架,那么摘要可能更合适。

I would recommend a simple repository pattern implementation. You could create a class called ItemRepository that knows about your Item object and your DAL implementation. The repository would simply call into the DAL to get any data it needs and then return business objects back to the consumer.

This is very simple to implement, and if you create an interface to go along with it (ex: IItemRepository), unit testing becomes very easy because you'll be able to mock the repository and pass it along to any object that consumes it. If you make everything static, then testing is much harder, so I wouldn't recommend it.

As for inheritance: if you want to define general method signatures and properties that should be available on every Repository object, then use an interface to specify exactly what each repository should have in common. If you want to provide a base repository skeleton, then an abstract may be more fitting.

甜心小果奶 2024-11-01 20:09:32

对我来说,GetItems(复数)听起来不像是 Item(单数)的成员。特别是因为 Item 如此简单。我将创建一个名为 ItemUtility 的静态实用程序类(工厂模式),它具有 GetItems。

回答你的第二个问题:如果一个类继承自 Item,它也将继承其成员的任何具体实现。因此,如果 Item 上有一个 LoadItem 方法,并且我创建了 SpecialItem : Item,那么 SpecialItem.LoadItem() 实际上只是 Item.LoadItem() 代码。如果您想让 Item.LoadItem() 可重写,那么您可以在该方法上使用“virtual”修饰符(这将使 SpecialItem 可以选择使用该方法执行自己的操作)。

如果您只想将 Item 用作其他更复杂的类(如 SpecialItem)的样板基类,则 Item 也可以是抽象类。

另一种选择是创建 IItem 接口,并使 LoadItem(以及任何其他必需的成员)成为接口定义的一部分。

GetItems (plural) does not, to me, sound like something that should be a member of Item (singular). Especially since Item is so simple. I would create a static utility class (the factory pattern) called ItemUtility that has GetItems.

To answer your second question: if a class inherits from Item it will also inherit any concrete implementations of its member. So if there was a LoadItem method on Item, and I made SpecialItem : Item, then SpecialItem.LoadItem() would actually just be the Item.LoadItem() code. If you want to make Item.LoadItem() overridable then you can use the "virtual" modifier on the method (which would give SpecialItem the option of doing its own thing with that method).

Item could also be an abstract class if you only intend it to be used as a boilerplate base class for other more complex classes like SpecialItem.

Another option would be to create an IItem interface, and make LoadItem (and any other required member) part of the interface definition.

花期渐远 2024-11-01 20:09:32

数据访问应该放在一个单独的类中。遵循关注点分离的概念,数据访问应存储在与对象定义不同的单独层中。

如果你想强制所有继承的对象实现一个方法,你可以将它抽象化。

The data access should go in a separate class. Data access should be stored in a separate layer than the object definition, following the Separation of Concerns concept.

If you want to force all inherited objects to implement a method, you can make it abstract.

三月梨花 2024-11-01 20:09:32

您想要将该方法设为静态有什么具体原因吗?如果您想在子类中继承 GetItems 方法,则不能将其设为静态。按顺序回答您的问题:

1) 是的。创建一个名为 ItemManager 之类的新类,该类实际调用 DB 层以获取项目。这样,您就可以将数据访问代码与业务逻辑分开。

2) 您应该在 Item 类中创建一个方法,该方法调用 ItemManager 中的方法来获取实际数据。

3) 如果您希望子类重写该方法以提供自己的实现,请将您在步骤 2 中创建的方法标记为虚拟方法。如果您想强制它们重写并且不需要在基类本身中实现,则将基类标记为抽象,以便子类必须重写它

Is there any specific reason you want to make the method static? If you want to inherit the GetItems method in your child classes, you cannot make it static. To answer your questions in order:

1) Yes. Create a new class called something like ItemManager that makes the actual call to the DB layer to get the Items. That way you are separating your Data Access code from business logic

2) You should create a method in Item Class that calls the method in ItemManager to get the actual data.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文