从Django模型类中分离域类

时间:2022-09-25 11:45:19

So I have completed my OO analysis and design of a web application that I am building and am now getting into implementation. Design decisions have been made to implement the system using Python and the web development framework Django.

所以我已经完成了我的OO分析和我正在构建的Web应用程序的设计,现在我正在实施。已经做出设计决策来使用Python和Web开发框架Django来实现系统。

I want to start implementing some of my domain entity classes which need persistence. It seems that Django would have me implement these as classes that are inherited from the Django models class in order to use the Django ORM for persistence. However, this seems like far too strong coupling between my class entities and the persistence mechanism. What happens if at some stage I want to ditch Django and use another web development framework, or just ditch Django’s ORM for an alternative? Now I have to re-write my domain entity classes from scratch.

我想开始实现一些需要持久性的域实体类。似乎Django会让我实现这些作为继承自Django模型类的类,以便使用Django ORM进行持久化。但是,这似乎是我的类实体和持久性机制之间过于强烈的耦合。如果在某个阶段我想抛弃Django并使用另一个Web开发框架,或者只是放弃Django的ORM来替代它会发生什么?现在我必须从头开始重新编写我的域实体类。

So it would be better to implement my domain classes as standalone Python classes, encapsulating all my business logic in these, and then use some mechanism (design pattern such as bridge or adapter or ???) to delegate persistence storage of these domain classes to the Django ORM, for example through a Django model class that has been appropriately set up for this.

因此,最好将我的域类实现为独立的Python类,将所有业务逻辑封装在这些类中,然后使用某种机制(设计模式,如桥或适配器或???)来委托这些域类的持久存储Django ORM,例如通过为此正确设置的Django模型类。

Does anyone have suggestion on how to go about doing this? It seems from all I have read that people simply implement their domain classes as classes inherited from the Django model class and have business logic mixed within this class. This does not seem a good idea for down line changes, maintenance, reusability etc.

有没有人建议如何做到这一点?从我读过的所有内容来看,人们只是简单地将它们的域类实现为从Django模型类继承的类,并在这个类中混合了业务逻辑。这对于下线更改,维护,可重用性等似乎不太好。

4 个解决方案

#1


4  

Can you seriously envisage a possibility that you're going to just ditch the Django ORM, but keep everything else? Or that if you ditched Django totally, any of your code is still going to be applicable?

您是否可以认真考虑过您只是放弃Django ORM,但保留其他所有内容?或者,如果你完全放弃了Django,你的任何代码仍然适用?

You don't complain that if you ditched Django, you'll have to rewrite all your templates. Of course you will, that's to be expected. So why is it OK for the presentation layer to be bound up with the framework, but not the persistence layer?

你不要抱怨如果你抛弃了Django,你将不得不重写所有的模板。当然,你会,这是可以预料的。那么为什么表示层与框架绑定,而不是持久层呢?

This sort of up-front over-analysis to be avoided. Django is a RAD tool, and is best suited to quick, iterative development. For all that, it's capable of building some powerful, long-lived applications, as plenty of large companies will testify. But it's not Java, and it's not "enterprisey", and it doesn't conform particularly well to OO principles. In the Python world, that's seen as a feature, not a bug.

要避免这种前期过度分析。 Django是一个RAD工具,最适合快速迭代开发。尽管如此,它还是能够构建一些功能强大,寿命长的应用程序,因为很多大公司都会作证。但它不是Java,它不是“企业”,并且它不符合OO原则。在Python世界中,这被看作是一个特征,而不是一个bug。

#2


3  

Well, the way to go with Django is to inherit from Django's base model classes. This is the 'active record' pattern. Your django models will have all CRUD and query methods along with you business logic (if you decide to add it of course). This is seen as an anti-pattern in the java world, but the cool thing about it is that it can speed up development really really fast.

那么,使用Django的方法是继承Django的基础模型类。这是'活跃记录'模式。您的django模型将包含所有CRUD和查询方法以及业务逻辑(如果您决定添加它当然)。这被视为java世界中的一种反模式,但关于它的一个很酷的事情是它可以非常快地加速开发。

#3


0  

You would not have to "rewrite your models from scratch" if you wanted a different persistence mechanism. The whole point of an activerecord-style persistence system is that it imposes minimal constraints on the model classes, and acts largely transparently.

如果您需要不同的持久性机制,则不必“从头开始重写模型”。活动记录式持久性系统的重点在于它对模型类施加了最小限制,并且在很大程度上透明地起作用。

If you're really worried, abstract out any code that relies on queries into their own methods.

如果您真的很担心,请将依赖查询的任何代码抽象出自己的方法。

#4


-1  

I think that there's no implemented solution for decoupling Django models and the domain classes, at least I haven't found any. In fact, the only ORM with such decoupling that I know exists only in Smalltalk world and it's called GLORP. It allows you to persist your domain model in a relational DB without having to modify domain classes. I'm currently trying to implement similar ideas to decouple from Django ORM. My motivation is that current strong coupling between DB tables and domain classes hurts software evolution badly. I'll post again if I succeed :)

我认为没有实现解耦Django模型和域类的解决方案,至少我没有找到任何解决方案。实际上,我所知道的唯一具有这种去耦的ORM仅存在于Smalltalk世界中,它被称为GLORP。它允许您将域模型保存在关系数据库中,而无需修改域类。我目前正在尝试实现类似的想法,以便从Django ORM中解耦。我的动机是当前DB表和域类之间的强耦合严重损害了软件的演变。如果我成功,我会再次发帖:)

#1


4  

Can you seriously envisage a possibility that you're going to just ditch the Django ORM, but keep everything else? Or that if you ditched Django totally, any of your code is still going to be applicable?

您是否可以认真考虑过您只是放弃Django ORM,但保留其他所有内容?或者,如果你完全放弃了Django,你的任何代码仍然适用?

You don't complain that if you ditched Django, you'll have to rewrite all your templates. Of course you will, that's to be expected. So why is it OK for the presentation layer to be bound up with the framework, but not the persistence layer?

你不要抱怨如果你抛弃了Django,你将不得不重写所有的模板。当然,你会,这是可以预料的。那么为什么表示层与框架绑定,而不是持久层呢?

This sort of up-front over-analysis to be avoided. Django is a RAD tool, and is best suited to quick, iterative development. For all that, it's capable of building some powerful, long-lived applications, as plenty of large companies will testify. But it's not Java, and it's not "enterprisey", and it doesn't conform particularly well to OO principles. In the Python world, that's seen as a feature, not a bug.

要避免这种前期过度分析。 Django是一个RAD工具,最适合快速迭代开发。尽管如此,它还是能够构建一些功能强大,寿命长的应用程序,因为很多大公司都会作证。但它不是Java,它不是“企业”,并且它不符合OO原则。在Python世界中,这被看作是一个特征,而不是一个bug。

#2


3  

Well, the way to go with Django is to inherit from Django's base model classes. This is the 'active record' pattern. Your django models will have all CRUD and query methods along with you business logic (if you decide to add it of course). This is seen as an anti-pattern in the java world, but the cool thing about it is that it can speed up development really really fast.

那么,使用Django的方法是继承Django的基础模型类。这是'活跃记录'模式。您的django模型将包含所有CRUD和查询方法以及业务逻辑(如果您决定添加它当然)。这被视为java世界中的一种反模式,但关于它的一个很酷的事情是它可以非常快地加速开发。

#3


0  

You would not have to "rewrite your models from scratch" if you wanted a different persistence mechanism. The whole point of an activerecord-style persistence system is that it imposes minimal constraints on the model classes, and acts largely transparently.

如果您需要不同的持久性机制,则不必“从头开始重写模型”。活动记录式持久性系统的重点在于它对模型类施加了最小限制,并且在很大程度上透明地起作用。

If you're really worried, abstract out any code that relies on queries into their own methods.

如果您真的很担心,请将依赖查询的任何代码抽象出自己的方法。

#4


-1  

I think that there's no implemented solution for decoupling Django models and the domain classes, at least I haven't found any. In fact, the only ORM with such decoupling that I know exists only in Smalltalk world and it's called GLORP. It allows you to persist your domain model in a relational DB without having to modify domain classes. I'm currently trying to implement similar ideas to decouple from Django ORM. My motivation is that current strong coupling between DB tables and domain classes hurts software evolution badly. I'll post again if I succeed :)

我认为没有实现解耦Django模型和域类的解决方案,至少我没有找到任何解决方案。实际上,我所知道的唯一具有这种去耦的ORM仅存在于Smalltalk世界中,它被称为GLORP。它允许您将域模型保存在关系数据库中,而无需修改域类。我目前正在尝试实现类似的想法,以便从Django ORM中解耦。我的动机是当前DB表和域类之间的强耦合严重损害了软件的演变。如果我成功,我会再次发帖:)