Request assistance with a domain issue

Here’s a stripped down version of one of my entities:

namespace Venue.Domain
using Enterprise.Foundation.Domain;
public class Favorite : Entity
private Post post = default(Post);
public Post Post { get { return post; } }
public string Username { get; protected set; }
protected Favorite()
post = default(Post);
Username = string.Empty;
public Favorite(string username) : this()
Username = username;

Ok, so I have a back reference there. This is part of the problem. I don’t want, or need it there, but the domain signature (FK) for this entity uses both the PostId and Username fields. If I do not include it, then any two Favorite instances will equate as long as the username is the same, regardless of which Post it belongs to.


Surely it shouldn’t be possible to create create a favourite with a Post? Aren’t you letting your entity get into an invalid state? If its possible for a favourite to exist with a proper post, then you might want to look into using a Null Object

can you show an example of how you use this class?

What I want to do is:

IN: FavoriteService

public bool ProcessAdd(FavoriteAddModel model)
Post post = postRepository.GetById(model.PostId);
Favorite favorite = new Favorite(post, model.Username);
return true; // never reached if exception occurs.

But here’s the problem. In order to do this, the favorite class needs a private field to map to, and at least a public getter for the classmap to bind the mapping to. This means the entity now has a back reference to the owning post.


note: I edited the post…

using(var trans = Session.BeginTransaction()) // Doesn't have to be an explicit call 
   var post = postRepository.GetById(model.PostId);
   var preferences = preferencesRepository.GetById(model.Username);
} // use flush on commit transaction and there is no need to call session.SaveOrUpdate();

how do pull all user favorits? -> favoriteRepository.GetUserFavorites(username)?
where will the “logic” around favorites go? Spread into services?

map UserPreferences to the user table… but you don’t new UserPreferences, you create users, they just happen to map to the same table. The collection in Preferences would map the FK tables

public class UserPreferences
public string UserName { get; }
public IEnumerable<Post> Favorites { get {…} }

That just confused me Pufa. Completely.

In your first code block, you have calls to repositories inside a “using session”. That’s what the repositories, themselves do. When I call favoriteRepository.GetByUsername(principal.Identity.Username), that call is wrapped in such a using structure. There’s no need to do it again. Is there?

Second, I grab the users favorites, as highlighted in the paragraph above. I am using the adonetdb for user management. Actually, let me clarify. There are two ways I use this data.

One is when I need to get a list of favorites per user.

The second is when I need to get a list per Post (yes, I do this).

Adding one would be.

Third. I don’t have a UserPreferences class. The Post entity maintains a list of it’s favorites, since I do not have a User entity or a UserPreferences entity.

I’m really having a hard time applying these principles to my model. While I understand (I think) what it is I am supposed to be doing, doing it is another matter. The old 1:1 anemic data model is really starting to look good about now, and letting services handle the logic. I am afraid I am just not “getting” this.