In applications with a Service-Oriented Architecture (SOA), it is important for each of the services to be a standalone, loosely coupled unit. One way to achieve that is using ServiceStack - an open-source .NET and Mono REST Web Services framework, which provides simple, cleaner POCO-driven way of creating web services.

It is very easy to integrate OpenAccess ORM as a persistence solution and use it from a ServiceStack web service. Let’s start with the setup of ServiceStack. You could use the NuGet package manager to do that:

Install-Package ServiceStack

Before moving to the implementation of the service we will register it together with its dependencies. The following code snippet should be added under the Global.asax.cs file:

public class MayLilyAppHost : AppHostBase
{
    public MayLilyAppHost()
        : base("OpenAccess ORM and ServiceStack integration sample", typeof(MayLilyAppHost).Assembly)
    {
    }
 
    public override void Configure(Container container)
    {
        container.RegisterAutoWiredAs<Configuration, IConfiguration>().ReusedWithin(ReuseScope.Container);
        container.RegisterAutoWiredAs<MayLilyMetadataSource, IMetadataSource>().ReusedWithin(ReuseScope.Container);
        container.RegisterAutoWiredAs<MayLilyContext, IDbMigrator>().ReusedWithin(ReuseScope.None);
        container.RegisterAutoWiredAs<MayLilyContext, IMayLilyContext>().ReusedWithin(ReuseScope.None);
    }
}
 
public static class AppBootstrapper
{
    public static void Start()
    {
        MayLilyAppHost appHost = new MayLilyAppHost();
        appHost.Init();
 
        IDbMigrator migrator = appHost.Container.Resolve<IDbMigrator>();
        migrator.MigrateSchema();
        migrator.SeedData();
    }
}
 
public class Global : HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        AppBootstrapper.Start();
    }
}

 

There are three parts in a ServiceStack service: Request DTO, Response DTO and the service implementation. In this blog post we will read category data filtered based on its identity. This means we need an appropriate request DTO:

[Route("/get-category/{CategoryID}", Verbs = "GET")]
public class GetCategory
{
    public int CategoryID
    {
        get;
        set;
    }
}

 

The service will return the following response DTO:

public class GetCategoryResponse
{
    public CategoryDto Result
    {
        get;
        set;
    }
 
    public ResponseStatus ResponseStatus
    {
        get;
        set;
    }
}
 
public class CategoryDto
{
    public int Id
    {
        get;
        set;
    }
 
    public string Name
    {
        get;
        set;
    }
 
    public string Description
    {
        get;
        set;
    }
}

 

Note the name of the response DTO. It is constructed by appending Response to the request DTO name. This is the convention used by ServiceStack. To support automatic exception handling a ResponseStatus property is added to the response DTO class.

And here the implementation of the service itself:

public class CategoryService : Service
{
    private readonly IMayLilyContext context;
 
    public CategoryService(IMayLilyContext context)
    {
        this.context = context;
    }
 
    public object Get(GetCategory request)
    {
        Category result = this.context.Categories.Find(request.CategoryID);
        if (result == null)
        {
            throw HttpError.NotFound(string.Format("Unable to find category with id {0}.", request.CategoryID));
        }
 
        return new GetCategoryResponse
        {
            Result = new CategoryDto
            {
                Id = result.Id,
                Name = result.Name,
                Description = result.Description
            }
        };
    }
 
    public override void Dispose()
    {
        this.context.Dispose();
    }
}

 

If everything is configured correctly, you could run the service and go to http://[server_uri]/get-category/1 to see your OpenAccess ORM ServiceStack web service in action. 

The complete demo application can be found here.
About the Author

Damyan Bogoev

Damyan Bogoev is Senior Software Developer in OpenAccess ORM Team

Related Posts

Comments