views:

57

answers:

3

Asp.net-mvc, using nhibernate.

my vs.net layout is like:

  • /dao (1 class per entity for database work, using repository)
  • /model
  • /mappings
  • /factory (db factory that provides access to each entities Dao)

Now I need utility methods, not sure where to put them.

example:

  • CartItems.cs
  • CartIemsDao.cs

Now say I have a method like:

IList<CartItem> items = CartItemsDao.GetById(234)

Now I want to create a method that populates a Dictionary<int,CartItem> from a given IList<CartItem>. Should I create a CartItemManager.cs for this?

And what would a 'Service' type class be used for? e.g. CartService.cs

I believe someone said earlier a 'service' type class is to wrap multiple calls/logic for Dao's etc. Is that what it is?

+1  A: 

There are several styles, you can definitely create a "helper" type that has all the static methods you need but this is not very discoverable from an API standpoint.

I would create these static methods on the data access objects themselves - this is much easier to discover. Of course nothing is stopping you from delegating the implementation of these static methods to some internal helper type.

Also as a side note: I don't personally care for the style of appending "DAO" or other similar identifiers to the names of types. The type is what it is so I would suggest that you leave off the "DAO" (but that has nothing to do with your question).

Andrew Hare
A: 

I would say that what you describe is a "Service", which I usually define loosely as [potentially] any operations I might want to make on entities that don't really fit into the entity itself (which practically by definition includes cross-aggregate operations).

To state it generally, you want transform a list of items into a dictionary of items using some function upon the item to derive the key.

With .Net generic typing, you could make a service for this so general it would fit best in a utility type of library that fits into the infrastructure layer where any other layer can utilize it.

public class CollectionToDictionaryMappingService {
  IDictionary<TKey, TValue> Map<TKey, TValue>(ICollection <TValue> items, Func<TKey, TValue> keyAccessor) 
  {
    var dictionary = new Dictionary<TKey, TValue>();
    foreach (TValue val in items) {
      dictionary.Add(keyAccessor(val), val);
    }
    return dictionary;
  }
}
qstarin
A: 
helios