Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Domain Model Classes always depend on primitives?

Halfway through Architecture Patterns with Python, I have two questions about how should the Domain Model Classes be structured and instantiated. Assume on my Domain Model I have the class DepthMap:

class DepthMap:
    def __init__(self, map: np.ndarray):
        self.map = map

According to what I understood from the book, this class is not correct since it depends on Numpy, and it should depend only on Python primitives, hence the question: Should Domain Model classes rely only on Python primitives, or is there an exception?

Assuming the answer to the previous question is that classes should solely depend on primitives, what would the correct way create a DepthMap from a Numpy array be? Assume now I have more formats from where I can make a DepthMap object.

class DepthMap:
    def __init__(self, map: List):
        self.map = map
    
    @classmethod
    def from_numpy(cls, map: np.ndarray):
        return cls(map.tolist())

    @classmethod
    def from_str(cls, map: str):
        return cls([float(i) for i in s.split(',')])

or a factory:

class DepthMapFactory:
    @staticmethod
    def from_numpy(map: np.ndarray):
        return DepthMap(map.tolist())

    @staticmethod
    def from_str(map: str):
        return DepthMap([float(i) for i in s.split(',')])

I think even the Repository Pattern, which they go through in the book, could fit in here:

class StrRepository:
    def get(map: str):
        return DepthMap([float(i) for i in s.split(',')])

class NumpyRepository:
    def get(map: np.ndarray):
        return DepthMap(map.tolist())

The second question: When creating a Domain Model Object from different sources, what is the correct approach?

Note: My background is not software; hence some OOP concepts may be incorrect. Instead of downvoting, please comment and let me know how to improve the question.

like image 933
Ramon Avatar asked Nov 09 '20 14:11

Ramon


People also ask

What is domain primitive?

A value object precise enough in its definition that it, by its mere existence, manifests its validity is called a domain primitive. Domain primitives are similar to value objects in Domain-Driven Design. Key differences are that we require invariants to exist and they must be enforced at the point of creation.

What is domain purity?

A pure domain model is void of any persistence concerns. The injection of an interface whose specific purpose is to enable access to the database makes the domain model impure.

What is domain Modelling in Java?

A Domain model is a conceptual model of the problem domain. By "java domain model" they just mean the java classes representing that model. There's nothing specific to java in the concept. See also Domain Driven Design for an approach to focusing your development on the business domain needs.


1 Answers

I wrote the book, so I can at least have a go at answering your question.

You can use things other than primitives (str, int, boolean etc) in your domain model. Generally, although we couldn't show it in the book, your model classes will contain whole hierarchies of objects.

What you want to avoid is your technical implementation leaking into your code in a way that makes it hard to express your intent. It would probably be inappropriate to pass instances of Numpy arrays around your codebase, unless your domain is Numpy. We're trying to make code easier to read and test by separating the interesting stuff from the glue.

To that end, it's fine for you to have a DepthMap class that exposes some behaviour, and happens to have a Numpy array as its internal storage. That's not any different to you using any other data structure from a library.

If you've got data as a flat file or something, and there is complex logic involved in creating the Numpy array, then I think a Factory is appropriate. That way you can keep the boring, ugly code for producing a DepthMap at the edge of your system, and out of your model.

If creating a DepthMap from a string is really a one-liner, then a classmethod is probably better because it's easier to find and understand.

like image 128
Bob Gregory Avatar answered Nov 01 '22 23:11

Bob Gregory