4

Currently I am creating a WCF service which has to connect to a DAL which, just connects to a database using ADO.net and stored procedures.

The DAl writes its responses from the database to a datacontract which is passed over the wire to the client via the service.

I was reading that this may possibly be the anti pattern 'CRudy Interface', but I wasn't sure as I am sharing the datacontract.

If I am using an anti pattern, can anyone suggest a better pattern to use for the behavior I require?

Abel
  • 56,041
  • 24
  • 146
  • 247
Miker169
  • 244
  • 3
  • 16

3 Answers3

4

Well, there seems to be some controversy about the CRUDy pattern and it's pros and cons. At a minimum, I would call a service interface that makes you write this kind of code to use it an anti-pattern (as commented here):

service.CreateCustomer(c);

foreach(Group group in c.Groups)

  service.AddCustomerToGroup(c.CustomerId, group.GroupId);

foreach(Person person in c.Contacts)

  service.AddCustomerContact(c.CustomerId, person);

Is exposing CRUDy interfaces bad in itself? I wouldn't say so. What's important is to provide an interface which will

  1. encapsulate knowledge about the underlying processes
  2. not be very chatty
Tomislav Nakic-Alfirevic
  • 10,017
  • 5
  • 38
  • 51
  • The problem with the CRUD interface is that it will generally result in breaking the 2nd rule because multiple calls have to be made to do anything remotely complex - it will be "chatty". – Joe Ratzer Mar 16 '10 at 08:48
  • 1
    The danger of a CRUD interface is that it encourages multiple calls. For example, for processing an Order there could be both an InsertOrder and InsertOrderLine method - two interface calls. This is in contrast to using one well-defined interface method to process the whole transaction. – Joe Ratzer Mar 16 '10 at 09:14
  • I imagine those two methods would share logic. What kind of interface would you expose to support the "place an order" use case? – Tomislav Nakic-Alfirevic Mar 16 '10 at 09:28
  • For a start I would have just one method that processes an order, the "four tenets of service orientation" in the paper I reference are a good check-list. – Joe Ratzer Mar 16 '10 at 09:36
  • Hmmm...I think the vagueness of term "CRUDy" is what makes it a controversial subject. I (maybe mistakenly) thought about insertOrder() as a CRUDy concept even though if I were to implement it, it would handle order line insertion as well, along with everything else that happens when a new order is placed. In any case, I've bookmarked the paper you linked to for more detailed reading. :) – Tomislav Nakic-Alfirevic Mar 16 '10 at 11:14
2

It does seem like the CRUD interface anti-pattern, but it would be good to see some interface examples to confirm.

This paper has a really good discussion on designing better service interfaces.

It includes a critique on, and alternative to, the CRUD anti-pattern.

Joe Ratzer
  • 18,176
  • 3
  • 37
  • 51
1

If you have a cruddy use case to implement you will get a cruddy interface, don't sweat it. The anti-pattern is when you implement non-cruddy things in a cruddy way.

MetalLemon
  • 1,390
  • 11
  • 16