4

We've implemented the null object pattern in our domain model. We're using code first with fluent api to persist our domain model.

One of our entities has a navigation property - this navigation property is a FK and is where we use the null object pattern.

Our null object has the default 0 value for it's id. Is there a way with fluent api (or direct mappings) to tell EF 'if this is one of those null objects, REALLY throw null in the database fk field, and not that invalid 0 id'.

EDIT:

As an example, imagine you have a Student Entity, and a FavoriteSubject entity. A student has a reference to a FavoriteSubject, unless he has no FavoriteSubject, then the null object would be something like NoFavorite.

When we persist this to the database, we get the Id of NoFavorite (0) where we'd really like NULL in the db as NoFavorite is a null object (follows null object pattern).

6
  • Are your entities your contract between your domain and application layers, or do you have domain objects that map to entities? Commented Aug 25, 2015 at 21:02
  • 1
    A code example would be nice. Commented Aug 25, 2015 at 21:02
  • @moarboilerplate our domain models/entities map to the database with fluent api. The application layer is as far as the domain model goes Commented Aug 25, 2015 at 21:04
  • Okay, so your entities are your domain objects. You're going to have to get a little creative. See here. If that doesn't work for you, then the null object pattern may not be a good fit for your design. Commented Aug 25, 2015 at 21:17
  • The (not really ideal) alternative would be to encapsulate your entities in your domain layer and map them to a new set of domain objects, where you could perform custom mapping there. Commented Aug 25, 2015 at 21:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.