where to put entity related methods?

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?

Answers


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).


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;
  }
}

I propose to view it from a layered architecture point of view.

App layers

It depends how are you architecturing you app. I generally use two diferents layers for DAO and for Business/Logic.

In an information system logic layer generally replicates the methods of DAO, because the methods needed for implementing the use cases are get data / update data... much the same that the DAO layer. BUT I think it's important to separate them because you can add extra logic in the logic/business layer, things more complex (or composite) than the DAO.

List -> Dictionary case

I don't know if you need that method for business logic. If it's the case maybe the method can be private to some business class that uses it. Or it can be a public method of a protected helper class in the business layer.

If it's not the case I'd publish it in the business layer anyway.

BUT, if I follow the layered point of view... The service layer should provide methods for implementing the use cases. It is: it should provide the data structures needed. in this case: it should provide the dictionary.

So:

  • the convert method should be private to the business layer

or

  • be a generic conversion method like the proposed by qstarin

Need Your Help

How do I install modperl under OS X Leopard's default Apache 2?

macos apache2 install osx-leopard mod-perl

My attempts to install modperl under the default vanilla Leopard Apache 2 have failed and all I can find online are variations on this:

Download large file in python with requests

python download stream python-requests

Requests is a really nice library. I'd like to use it for download big files (>1GB).