Is using spring to get your service layer a good idea?

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

Can spring or another tool be used to "get" your service level through a DI container and be able to change the configuration later?

0


source to share


1 answer


Quite right. You must have some kind of interface that your service layer uses, then you can change the implementation at any time, if it implements the same interface, it must be interchangeable.

This is the advantage of interfaces and DI, especially when using a container like Spring. After you change the implementation, just configure spring to use the new one from you as well.


Update because the comments are too small:

Well, I guess it all depends on your service. The point is, if you are using an interface (say on top of NServiceBus), you can always create an adapter to wire your WCF implementation to the interface.

For example:

public interface IService {
   void DoSomething();
}

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

      

Now (I assume I am not very familiar with Spring.net), you can use spring NServiceBusService as an IService implementation.



Have you decided to enable WCF now? You can only have WCF implementation:

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

      

And now you can configure spring to use WCFService for IService instead of NServiceBusService.

Or, if your WCFService doesn't match the signature for the IService, as it might look like this:

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

      

Then you can simply use the adapter pattern to make this work:

public class WCFServiceAdapter : IService { 
   private WCFService wcfService;

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

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

      

Whether or not you have ways to introduce this dependency. The tricky part is that you need to set up a contract using your interface, and your objects must abide by that contract, otherwise you will have problems. But there is always a way to match (for example using the adapter pattern above) to make the objects look like they implement the interface and make it work.

+2


source







All Articles