How-To: Create Service Layer Components with Velocity in LASG

This walk-through will get you familiarized with the Velocity: Service Layer Component Generator that is available in Layered Architecture Solution Guidance 2013 to create service layer components. We are now at the fourth step in the LASG Code Generation process where we will be exploring the creation of service layer components.

Before beginning this guide, please make sure that you have completed the previous tutorial on Creating Business Components with Motion in LASG. Please make sure that your solution is fully compilable without any errors before starting. This walk-through also uses the LeaveSample database that is part of the Layered Architecture Sample for .NET but you may use your own database to follow through.


  1. In Solution Explorer, expand the Tutorial solution, go to the Services folder.
  2. Right-click on any one of the Tutorial.Services or Tutorial.Services.Contracts project and on the context menu, click Add Services and Service Contracts...


  3. This will launch the Velocity: Service Layer Component Generator which contains 4 sections. Take note that Velocity is designed to generate Windows Communication Foundation (WCF) services and therefore, you are encouraged to acquire some knowledge of WCF to understand the terminologies used.


  4. By default, Velocity will attempt to load the business project assembly from the centralized bin folder located under the solution (i.e. Tutorial\bin) and populate the details in the  1. Define Service  section. In this exercise, the Tutorial.Business.dll should be automatically loaded.

    Note: If the business project assembly is not loaded, please exit Velocity and return to Visual Studio to make sure that there are business components in the Tutorial.Business project and the solution is compilable without errors. Also please ensure that all dependent assemblies are located in the centralized bin folder. You can also manually load the business project assembly by clicking the Load button and selecting the .dll file.
  5. All business components in the assembly will be listed in the Business components dropdown list. Make sure that LeaveComponent is selected.
  6. Velocity will attempt to suggest the Service name for us. By default, it will replace the Component suffix from the business component with a Service suffix. Leave the default suggestions and let Velocity create a LeaveService component for us.
  7. By default, the namespace is left blank but you can enter one of your choice it in the Namespace text box or click the   button to get Velocity to suggest one for you. For this exercise, we will leave the namespace as blank.

    Note: WCF will automatically give a service namespace of http://tempuri.org if the namespace is left blank.
  8. You can also set the Instance Context Mode and Concurrency Mode of your service. Verify that Instance Context Mode is PerCall and Concurrency Mode is Multiple.

    Tip: For more information, read Session, Instancing, and Concurrency.
  9. Velocity allows you to create custom fault contracts which you can extend later, you can check the Generate fault contract check box and specify the name of the fault contract. All fault contracts will be suffixed with Fault. For this exercise, we will not create any fault contracts.
  10. Since most of the values have been automatically filled in for us, please ensure that the settings that you have are somewhat like the following:


  11. At the  2. Select Business Methods  section, the Business methods grid lists all the business methods that are available in the business component. Select the ListLeaves and GetLeave methods by checking on the their check boxes in the first column.


    Note: Velocity exposes business methods as service methods in a one-to-one mapping.
  12. Define an endpoint for the new service. This only needs to be done once when we first create a service. At the  3. Define Endpoint Configurations  section, make sure basicHttpBinding is selected in the Binding dropdown list. Leave the suggested Endpoint name and Address. Click the   button to add it to the Service endpoints grid.


    Tip: You can create multiple endpoints for your services based on WCF's guidelines. If you are using a HTTP based binding and want to use SSL, you can also check the SSL/HTTPS enabled check box to allow Velocity to generate the basic configuration settings for you. For this exercise, we will not create any extra endpoints or use SSL.

    Note: The Address is only used in non-IIS/WAS hosts i.e. your own Console host. Since most enterprise WCF services are usually (and recommended to be) hosted on IIS/WAS, this setting is seldom used and is just retained for backward LASG compatibility.
  13. Click on the Add to Code Generation button to queue the definition.
  14. Like the other code generators, you can perform various action from the tool-bar at the  4. Generate Service Components  section. You can Preview the code that will be generated, set Options, reposition the methods, Reset the definitions so that they will be code-generated again or Delete the definitions.

    Warning! Deleted definitions will not be recoverable.

    If you Preview the code, you will noticed that the preview window now opens up in split view to show you the code of the service interface and the service implementation classes.

    Note: All definitions will be marked as code generated (indicated by the IsUpdated property) after code generation. They will continue to appear in the list but will not be code generated the next time. Clicking Reset will change one or more definitions' IsUpdated value to true and mark them for code generation again.

    You can also Delete a method, a service component definition or all service component definitions from the tool-bar. Clicking on the Options icon will open the Code Generation Options dialog that will allow you to set a few standard options. Leave the settings alone and click OK.
  15. Make sure the Generate service configuration check box is checked.
  16. Click the Generate button to generate the service layer component classes.
  17. At the Code Generation Completed dialog, click OK.
  18. The Tutorial.Services project should contain a generated LeaveService code file and the Tutorial.Services.Contracts projects should contain a generated ILeaveService code file. Take note that this is WCF's requirement that all services must be accommodated by a service contract (or interface).

Summary

We have basically gone through how to use the Velocity: Service Layer Component Generator to create service layer components for our layered application. As you can see, Velocity is very easy to use as it provides most of the default values that are needed for code generation. Since Velocity generates WCF service components, it is recommended that you do some read-up on WCF to fully understand what is going. You can still change the generated code but take note that any changes made in your code will not be reflected in the definitions stored in Velocity. Therefore, practice caution when you are regenerating code on existing code file.

Coming up next - How-To: Create Workflow Services and Activities with Vector in LASG.

No comments:

Post a Comment

Popular Post