Data Mapper - what classes are responsible for?


  • QA Engineer

    Where boundaries pass between class responsibility zones using pattern http://www.design-pattern.ru/patterns/data-mapper.html ?

    If I understand the logic of the pattern, these classes are at least three: the mapper, the objective layer, and http://www.design-pattern.ru/patterns/repository.html ♪ And if there's a difference between the basic tasks, I don't understand which class should be responsible for searching.

    Let's say there's a method in the opera. find()which, by the identifier, finds the necessary footage and returns the ready object layer. This is in theory, and in practice, in real search conditions, much more: the model of equipment can be found not only by the technician, but also (for example) by the manufacturer, category, value, date of production.

    The situation is very complicated if the attributes in the subject-matter object are different from the name of the field in the database (e.g., producerId in code and producer_id in OBD.

    The situation will be further complicated if there are several related objects in the system and are used. http://www.design-pattern.ru/patterns/single-table-inheritance.html ♪ Mapper's calmly taking the name of the class from the OBD with other data and generating an object.

    So, the search. So we have to create another class that will be responsible for the search? And if the mapper returns one of the three possible objects, there must be three of those classes.

    Pick up, please, please.



  • I recommend that we look first towards the solutions that have already been prepared: Doctrine for PHP and Hibernate for Javaalso to read: M. Fowler's corporate applications. - Classical work, the section on the pathters of objective-realistic reflection - all yours.

    Now, it is understandable that in a short answer, I will not be able to convey to you in two words the contents of the whole section of the book and the conclusions from the background. Doctrine♪ but I'll try ♪

    First, the results of the search are either Mapper or Repository. Who is, depending on the implementation and viewpoint, there is no single correct answer. Create a separate class that will only be responsible for searching, but sometimes there is a need to separate interfaces (e.g., ProductFinderwhich will be relevant to the package of the subject area and which will be implemented in the classes of the data access package (in your mappers). I mean, the classes of the subject area will know about the search interface, but they will not depend on the specific implementation.

    Second, mappers can be determined except find() z findBy(array $criteria)which can meet private search objectives. The discontinuation of the names of columns and fields - the mappers must be determined on the basis of their configuration or from metadata. One of the tasks of the mapper is to make your facilities independent of the data storage structure. For the sake of full implementation findBy and so on, you need to make some form of patter. Specification♪ In fact, the Fowler quote (not exact): "Repository and Specification often contain a significant part of the objective database implementation"which, in general, should lead to a better solution (and the most verified)

    Prospecting: The converter should know the essence of the table. In general, once again, I suggest that we look at the same task. Hibernate

    UPD

    Do I understand correctly that the date of the mapper implies one substance = one table (this is even in the answer, in the last paragraph?)

    Wrong.

    This is a comprehensive question and deal with it, not taking the theoretical basis of the last paragraph of my reply (in which, as I mentioned earlier, I will not be able to fully disclose the subject with all intent), but at least the book referred to in the first paragraph, M. Faulera (which is useful to read to any developer).

    If short, Data Mapper - it's a private accident. Mapper♪ Modification to its application: Make the business logic as independent as possible of the data storage structure Data source

    Call attention to the word Data source (sighs)Data Sourceif you like. If you think of an abstract concept data sourceyou can see that the table in the database is just his private case. But it should be noted that this private case covers 95 per cent of the tasks of developing modern applications, so most librarians and packages focus on it. But. Again, the table in the database is a total private case of the data source. Accordingly, you can write a lock to another data source - for example, you can consider result of requestas a data source, with which your converter(s) will work.

    I don't know don't know how your case fits, but you can create a database. viewand work with him like a table, if necessary. reading Data (which is likely to fail to be recorded in any particularly simple or general solution for situations where one substance is collected from a variety of tables - will either have to sign a logic in each reverse of the fields into the data and send the relevant data to its data source for recording, or to organize a precision logic of change based on metadata). I cannot but notice that if one substance is not a single table, it may be an excuse to treat it as a combination of other creatures, each of which has its own table and has its interface and logic, and the general nature will be a sort of orchestrator to work with its members by providing a higher level interface.

    Once again, if you really want to deal with the question, buy and roll and read Fowler. From the short responses to SO, you will not have a beautiful structured span of consistent knowledge on such an integrated issue. Moreover (and judging by the question) "mopper = table?", this is already happening to some extent, you may have faithful in some parts, but not a generally true picture of the understanding on this (which is very important for any enterprise-oriented developer) issue.

    As a bonus, a little reference to Doctrine can help:

    https://stackoverflow.com/questions/5741318/mapping-two-tables-to-one-entity-in-doctrine2 https://stackoverflow.com/questions/16037626/symfony2-map-entity-to-different-tables




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2