Summary

Legacy databases do not refer to databases that are old. It is more of a situation that refers to when the mismatch between your domain model and the database schema is too great. This could happen on any database schema for a variety of reasons ranging from using existing database to build a new application, to strict rules around how the database schema must be designed. Working with legacy database situation is interesting and challenging at the same time. NHibernate has some features built-in that help with navigating through legacy databases. This makes working with legacy databases more fun.

In the old days, developers preferred using natural identifiers, and at times that led to composite identifiers. NHibernate supports composite identifiers to the extent of getting most common database operations done. Some features, such as identifier generation, would not work with composite identifiers. A single entity needing to be mapped to multiple tables, or multiple entities needing to be mapped to single tables, can be common occurrences with legacy database situations. NHibernate has you covered in both situations via support for components and joins. If you are working against a very complex database schema but all you need to do is read from the database, then you can use database views that match your domain model. As long as you are not saving or updating entities in your application, this would work just fine. It is possible that you will be asked to use stored procedure for a particular business operation or for usual CRUD operations. NHibernate's support for stored procedure works just fine in both the situations. You should let NHibernate generate the SQL if you can, as that is what it does best. If none of the above helps, then you can use custom DTOs that match your database schema. In your application, you will then need to transform from DTOs to domain entities and vice versa. This will get the job done, but it comes with its own overheads. A flipside of using custom DTOs is that lazy loading might not work properly. The experimental implementation of lazy loading retention, that we saw in this chapter, should help.

With these tips at hand, I am sure you will find working with legacy databases interesting. We are coming close to the end of our NHibernate journey. There is another slightly disconnected chapter next, which talks about important NHibernate features that we did not get a chance to talk about so far. The features covered in the next chapter are not used on a daily basis. They are there for rare occasions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.137.38