Is it possible to use NHibernate without altering a DDD model that is part of a framework

后端 未结 4 565
耶瑟儿~
耶瑟儿~ 2021-02-01 22:32

I dig a lot of things about the DDD approach (Ubiquitous language, Aggregates, Repositories, etc.) and I think that, contrary to what I read a lot, entities sho

4条回答
  •  不思量自难忘°
    2021-02-01 23:22

    • For NHibernate:
      • All mapped classes require a default (no-arguments) constructor. The default constructor does not have to be public (it can be private so that it is not a part of the API), but it must exist. This is because NHibernate must be able to create an instance of the mapped class without passing any arguments. (There are workarounds, but don't do that.)
      • All mapped properties for which lazy-loading will be required must be marked virtual. This includes all reference properties and all collection properties. This is because NHibernate must be able to generate a proxy class deriving the mapped class and overriding the mapped property.
      • All mapped collection properties should use an interface as the property type. For example, use IList rather than List. This is because the collections types in the .NET Framework tend to be sealed, and NHibernate must be able to replace a default instance of the collection type with its own instance of the collection type, and NHibernate has its own internal implementations of the collection types.
      • For NHibernate, prefer Iesi.Collections.Generic.ISet to System.Collections.Generic.IList, unless you are sure that what you want is actually a list rather than a set. This requires being conversant in the theoretical definitions of list and set and in what your domain model requires. Use a list when you know that the elements must be in some specific order.

    Also note that it's typically not easy to swap object-relational mapping frameworks, and in many cases it is impossible, when you have anything beyond a trivial domain model.

提交回复
热议问题