2

I have a MVC app that uses ninject to inject service dependencies into controllers and it works well. However I also have some domain objects that require these services in their constructors and I want to resolve these dependencies using ninject, but don't want to reference ninject directly in my domain objects assembly. I have read lots of questions and answers here but its still not clear to me the best way to go about this. For example I have a ShoppingCart domain object that needs an instance of a IProductCatalogService passed to its constructor. What is the best pattern to create an instance of a shopping cart? I could have a reference to the root kernel and call out to that, but that would mean having references to ninject throughout my domain assembly. Should I wrap access to the kernel in a factory class?

Any thoughts or suggestions welcome!

Ruben Bartelink
  • 55,135
  • 22
  • 172
  • 222
Mike Hanrahan
  • 1,192
  • 8
  • 18
  • Related: http://stackoverflow.com/questions/1933351/if-you-are-forced-to-use-an-anemic-domain-model-where-do-you-put-your-business-l – Steven Aug 18 '11 at 08:18
  • Related: http://stackoverflow.com/questions/1793576/ddd-concepts-in-n-layer-development – Steven Aug 18 '11 at 08:18

1 Answers1

1

It is usually considered bad practice to have services in domain objects. I think you need to rethink exactly what you are attempting to achieve. Why does a ShoppingCart need to consume Product Catalog Services?

From a Domain perspective I would assume that a ShoppingCart would consist of many 'items', have properties like total etc and potentially would be passed to an ordering service. Your controller actions would update the Shopping Cart domain by adding items, removing items, etc, etc.

If you really need to consider this option, is to use commonservicelocator. This will separate out your (direct) dependency on ninject.

Alistair
  • 1,004
  • 8
  • 17
  • Ok...thanks.. I will have a look at the CommonServiceLocator. In this case the ShoppingCart needs an instance of the ProductCatalogService to check check price of an added product (the product doesn't have a price property as the prices are very dynamic). That was just an example..it might have been something else that need to be injected, such as an ICustomer, but sounds like I need to get a better understanding of the architecture. – Mike Hanrahan Aug 18 '11 at 01:00
  • I am not so sure about never putting services into domain objects....it seems to me the domain object should be able to perform operations on its data (I have dto's for moving the data around). Check out this post from the venerable Martin Fowler - http://www.martinfowler.com/bliki/AnemicDomainModel.html – Mike Hanrahan Aug 18 '11 at 01:49
  • Well this could get complicated.... :) link [link](http://blog.jonathanoliver.com/2010/02/domain-models-aggregate-roots-and-lookup-tables/). – Alistair Aug 18 '11 at 02:28
  • Agreed! Sounds like a whole other question. Anyway, thanks for answering my original question....CommonServiceLocator looks like what I need. – Mike Hanrahan Aug 18 '11 at 18:07