SimpleRepository 和抽象
我一直在使用 Sub Sonic 3.xxx,并且遇到了一些需要帮助的问题。 我正在使用抽象类和工厂模式等...... 这基本上就是问题:
public abstract class Person
{
}
public class Male : Person
{
}
public class Female : Person
{
}
....
我如何让它发挥作用?
String personType = "male";
Type myType = GetPersonTypeFromFactory(personType);
SimpleRepository rep = new SimpleRepository();
var all = rep.All<...>().ToList();
我无法放置 rep.All
那么我怎样才能让它工作呢?
I've been using Sub Sonic 3.x.x.x and I've come across something I need help with.
I'm using an abstract class and a factory pattern etc...
and this is basically the problem:
public abstract class Person
{
}
public class Male : Person
{
}
public class Female : Person
{
}
....
How do I get this to work?
String personType = "male";
Type myType = GetPersonTypeFromFactory(personType);
SimpleRepository rep = new SimpleRepository();
var all = rep.All<...>().ToList();
I can't put rep.All<myType>
so how can I get this working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SimpleRepository 无法持久化抽象对象。 但是我无法与 ActiveRecord 对话。
我相信您知道这一点,但如果您确实使用过 SimpleRepository 并且您希望获得一种类型的所有内容,您可以使用
rep.All()
。 这消除了你的工厂,我确信这个例子是你在现实世界中所做的事情的简化,所以这可能不是你想要的。The SimpleRepository cannot persist object that are abstract. I can't speak to ActiveRecord however.
I'm sure you know this, but if you did use SimpleRepository and you're looking to get all of one type you could do
rep.All<Male>()
. This eliminates your Factory and I'm sure the example is a simplified from what you're doing in the real world so this probably isn't what you want.值得记住的是,反序列化抽象对象总是一件痛苦的事情,因为抽象类没有默认的构造函数; 话虽如此,没有什么可以阻止您反序列化为具体类,并从工厂返回接口/抽象类。
当您将数据持久化/检索到数据库时,您只需通过具体的 DO 对象来完成,并通过复制构造函数在工厂中实例化您的 BO 对象,并让它实现与 DO 对象相同的接口。 但与模式一样,您必须询问代码会得到什么。
要检索:
做——> 工厂--> BO
到商店 :
博 --> 外观/控制器 --> DO
DO 和 BO 实现 IYourObject 接口,或派生自 YourObjectBase,后者具有复制构造函数 YourObjectBase(YourObjectBase src)。
如果您没有多种 BO 类型,则需要很多行代码:-)
It’s worth bearing in mind that de-serialising abstract objects is always a pain in the ass as abstract classes don’t have a default constructor; having said that, there is nothing stopping you de-serialising to a concrete class, and returning and interface/abstract class from your factory.
When you persist/retrieve data to the DB you just do it via your concrete DO object and instantiate your BO object in the factory via a copy constructor and have it implement the same interface as the DO object. But as always with patterns, you’ve got to ask what you’re getting for the code.
To retrieve :
DO --> Factory --> BO
To Store :
BO --> Façade/Controller --> DO
DO and BO implement IYourObject interface, or derive from YourObjectBase, which has the copy constructor YourObjectBase(YourObjectBase src).
Lots of lines of code though if you don’t have multiple BO types :-)