Sponsored By Aspose - File Format APIs for .NET

Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

Web Service Software Factory – Data Access Guidance Package – Code Generation

The Tampa Bay Buccaneers play the Carolina Panthers at 1pm today, and I am so frustrated with the play calling by Jon Gruden this season that I don't think I can watch the game in real-time. We have two 0-2 football teams needing a win badly, and if the Bucs didn't have home field advantage today, I would predict a win by the Panthers. I can't stop pacing around the house and mumbling to myself today, so let's talk about the Web Service Software Factory!

Sam has been mentioning the Web Service Software Factory quite a bit and using it in production, but I haven't had a lot of time for it until this past week. It is my first real introduction to the code generation and automation capabilities of the Guidance Automation Extensions and Toolkit, and I am blown away.

The Web Service Software Factory is a combination of two very impressive guidance packages:

  • ASMX guidance package. This guidance package helps build the service interface, the messages the service will expose and consume, and the translators that map these messages to domain model entities.
  • Data Access guidance package. This guidance package helps build business entities from and existing data model, build stored procedures from a data model, and generate the repository classes and factories that comprise the data access logic.

I decided to create web services to administer a simple, unfinished, and mythical blog engine whose database and tables have already been created:

 

 

The Web Service Software Factory will generate a number of projects in your solution for you based on nothing more than namespace information:

 

 

 

Data Access Guidance Package

Things start getting really interesting when you start invoking the guidance packages.

The Data Access Guidance Package is a collection of recipes, wizards, etc. that offer some pretty cool code generation capabilities based on an existing database and its set of tables. It can help generate

  • CRUD Stored Procedures
  • Business Entities for each Table
  • A Data Access Layer full of repositories that map stored procedures to getone, insert, update, delete CRUD methods, etc

It had me at hello :)

 

Generate CRUD Stored Procedures

I walk through the wizard to generate stored procedures. I am playing here, so I just pick my four tables and accept the defaults and in about a minute I have a sweet looking Blogs.sql file inserted in my data access layer project that I run against my database and get the following ( more pictures ):

 

 

 

I know we can get this from CodeSmith and other places, but you have to love it.

 

Generate Business Entities

The same 1 minute process works to generate my business entities. I select the tables, rename the plural names to singular ( you can even rename the properties so they don't have to match the column names ) in a quick wizard:

 

 

 

These are simple persistent ignorant business entities: Default Constructor, Fields, and their associated properties.

 

Generate The Repositories for my Data Access Layer

Now that I have the stored procedures and business entities created, I run another wizard that creates a repository for each table, allowing me to 1) map stored procedure to CRUD methods and 2) map stored procedure input and output parameters to business entity properties. This wizard could be made easier and faster, but again the process is simple:

 

 

 

These aren't repositories in the Domain-Driven Design sense, because each table has a repository, where in DDD only aggregates have a repository. I think of them as Data Access Objects.

 

Generate Some Business Logic to Get A Blog Post

Up to this point I have just been fighting carpal tunnel by all this mouse clicking, and haven't done any coding. The horror :)

Here is my first cut of the business logic for getting a blog post by Id.

 

using Boohoo.BlogAdminServices.BusinessEntities;
using Boohoo.BlogAdminServices.DataAccess;

namespace Boohoo.BlogAdminServices.BusinessLogic
{
    public class GetPostAction
    {
        public Post Execute(int postId)
        {
            PostRepository postRepository = new PostRepository("Blog");
            Post post = postRepository.GetPostByPostId(postId);
            
            if (post != null)
            {
                CategoryRepository categoryRepository = new CategoryRepository("Blog");
                CategoryCollection categories = categoryRepository.GetCategoriesByPostId(postId);

                post.Categories = categories;
            }
            
            return post;
        }
    }
}

 

Conclusion

This has all been about the Data Access Guidance Package ( wait 'til we talk about the ASMX Guidance Package ), which shows you some pretty cool code generation of stored procedures, business entities, and a data access layer. It's worth checking out if just to use the code generation and learn some design patterns.

Now for that Bucs and Panthers Game…

 

Resources

This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

5 Responses to Web Service Software Factory – Data Access Guidance Package – Code Generation

  1. zjysky says:

    why not chinese
    and the documnet is less

  2. dhayden says:

    Rudib,

    I would say these guidance packages are an alternative. Some people might prefer this method of development, just as some people prefer CodeSmith vs. LLBLGen Pro vs. NHibernate, etc.

    The factories are built on top of the Guidance Automation Toolkit, which allows you to build your own guidance packages, wizards, recipes, etc. that generate code. It will be exciting to see if people embrace the GAT and we start seeing a lot of guidance packages being shared that generate code.

    I am still new to the GAT and software factories, so I don’t know if this will be a bang or a bust.

  3. Rudib says:

    Cool stuff as I am doing exactly the same now using codesmith although I use one factory with all methods for one Entity (http://www.theserverside.net/tt/articles/showarticle.tss?id=DomainObjectsPersistencePattern). I wonder where does this leave tools like LLBLGEN etc , are they being replaced by this development?

  4. Marcos says:

    Oooohhh! Too bad about Simms, but to finish the game… and back to the business at hand, this guidance package stuff looks like it deserves a bit of attention on my part. Thanks for the heads-up.

  5. Nice article David – welcome back and good luck with the Bucs…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>