views:

789

answers:

1

Suppose I have service layer doing hibernate and DB stuff. Silverlight and windows clients need to get service layer through some kind of remote service. ASP.NET would benefit if could get it directly (on the same machine, same process, class instantiation).

Is it a good idea to use spring or other tool to "get" your service layer through DI container and be able to change the configuration later?

+2  A: 

Absolutely. You should have some sort of an interface that your service layer uses, then you can change the implementation at any time, so long as it implements the same interface it should be interchangeable.

That's the benefit of interfaces and DI, especially using a container such as Spring. Once you change the implementation, just configure spring to use the new one and away you go.

===============

Update cause the comments are too small:

Well I guess that all depends on your service. The thing is, that if you use an interface (say over top of NServiceBus), you can always create an Adapter to plug your WCF implementation into the interface.

For example:

public interface IService {
   void DoSomething();
}

public class NServiceBusService : IService {
   public void DoSomething() {
      //Some NServiceBusCode
   }
}

Now (I'm guessing, I'm not very familiar with spring.net), you can have Spring use the NServiceBusService as the implementation of IService.

And now you decide to plug in WCF instead? You could just have a WCF Implementation:

public class WCFService : IService {
   public void DoSomething() {
      //Some WCF Code
   }
}

And now you can set up Spring to use WCFService for IService instead of NServiceBusService.

Or, if your WCFService doesn't match the signature for the IService, as in maybe it looks like:

public class WCFService {
   public void DoSomethingWCFStyle() {
      //Some WCF Code
   }
}

Then you could just use the adapter pattern to make that work:

public class WCFServiceAdapter : IService {
   private WCFService wcfService;

   public WCFServicAdapter(WCFService wcfService) {
      this.wcfService = wcfService;
   }

   public void DoSomething() {
      wcfService.DoSomethingWCFStyle();
   }
}

No matter what, there are ways for you to inject that dependency. The hard part is that you need to set up the contract using your interface and your objects must abide by that contract, otherwise you're going to have trouble. But there's always a way to map (for example, using the adapter pattern above) the objects to look like they implement the interface and make it work.

mendicant
what kind of interface would you implement? I mean...there are different types of services. If I call operations like "RegisterOrder", i will be able to use it later with WCF-style services...but this will not be compatible with REST or NServiceBus...what do you think?
badbadboy
@mendicant: thanks for big update. It makes a lot of sense.I am wondering if these kind of adapters can cover cases with totally different types of services...for instance, an RPC service might have methods "AddOrder" and "AddCustomer", when CRUD service would only have Add (Object) method...:)
badbadboy