![]() |
Spring's .NET Enterprise Services support allows you to export a 'plain CLR object' as a .NET Remoted object. By "plain CLR object" we mean classes that do not inherit from a specific infrastructure base class such as ServicedComponent.. You can leverage the IoC container to configure the exporter and service endpoints. You may also opt to not use the IoC container to configure the objects and use Spring's .NET Enterprise Services classes Programatically, as you would with any third party library. Services components in .NET are able to use COM+ services such as
declarative and distributed transactions, role based security, object
pooling messaging. To access these services your class needs to derive
from the class
Many of these services can be provided without the need to derive
from a ServicedComponent though the use of Spring's Aspect-Oriented
Programming functionality. Nevertheless, you may be interested in
exporting your class as a serviced component and having client access that
component in a location transparent manner. By using Spring's
Note that the following sections do not delve into the details of programming .NET Enterprise Services. An excellent reference for such information is Christian Nagel's "Enterprise Services with the .NET Framework" Spring.NET includes an example of using these classes, the 'calculator' example. More information can be found in the section, .NET Enterprise Services example. One of the main challenges for the exporting of a serviced component to the host is the need for them to be contained within a physical assembly on the file system in order to be registered with the COM+ Services. To make things more complicated, this assembly has to be strongly named before it can be successfully registered. Spring provides two classes that allow all of this to happen.
Let's say that we have a simple service interface and implementation class, such as these: namespace MyApp.Services { public interface IUserManager { User GetUser(int userId); void SaveUser(User user); } public class SimpleUserManager : IUserManager { private IUserDao userDao; public IUserDao UserDao { get { return userDao; } set { userDao = value; } } public User GetUser(int userId) { return UserDao.FindUser(userId); } public void SaveUser(User user) { if (user.IsValid) { UserDao.SaveUser(user); } } } } And the corresponding object definition for it in the application context config file: <object id="userManager" type="MyApp.Services.SimpleUserManager"> <property name="UserDao" ref="userDao"/> </object> Let's say that we want to expose user manager as a serviced
component so we can leverage its support for transactions. First we need
to export our service using the exporter
<object id="MyApp.EnterpriseServices.UserManager" type="Spring.Enterprise.ServicedComponentExporter, Spring.Services"> <property name="TargetName" value="userManager"/> <property name="TypeAttributes"> <list> <object type="System.EnterpriseServices.TransactionAttribute, System.EnterpriseServices"/> </list> </property> <property name="MemberAttributes"> <dictionary> <entry key="*"> <list> <object type="System.EnterpriseServices.AutoCompleteAttribute, System.EnterpriseServices"/> </list> </entry> </dictionary> </property> </object> The exporter defined above will create a composition proxy for our
SimpleUserManager class that extends The next thing we need to do is configure an exporter for the COM+ application that will host our new component: <object id="MyComponentExporter" type="Spring.Enterprise.EnterpriseServicesExporter, Spring.Services"> <property name="ApplicationName" value="My COM+ Application"/> <property name="Description" value="My enterprise services application."/> <property name="AccessControl"> <object type="System.EnterpriseServices.ApplicationAccessControlAttribute, System.EnterpriseServices"> <property name="AccessChecksLevel" value="ApplicationComponent"/> </object> </property> <property name="Roles"> <list> <value>Admin : Administrator role</value> <value>User : User role</value> <value>Manager : Administrator role</value> </list> </property> <property name="Components"> <list> <ref object="MyApp.EnterpriseServices.UserManager"/> </list> </property> <property name="Assembly" value="MyComPlusApp"/> </object> This exporter will put all proxy classes for the specified list of components into the specified assembly, sign the assembly, and register it with the specified COM+ application name. If application does not exist it will create it and configure it using values specified for Description, AccessControl and Roles properties. Because serviced component classes are dynamically generated and
registered, you cannot instantiate them in your code using the new
operator. Instead, you need to use
<object id="enterpriseUserManager" type="Spring.Enterprise.ServicedComponentFactory, Spring.Services"> <property name="Name" value="MyApp.EnterpriseServices.UserManager"/> <property name="Template" value="userManager"/> </object> You can then inject this instance of the IUserManager into a client class and use it just like you would use original SimpleUserManager implementation. As you can see, by coding your services as plain CLR objects, against well defined service interfaces, you gain easy pluggability for your service implementation though this configuration, while keeping the core business logic in a technology agnostic POCO, i.e. Plain Old CLR Object.
|