Home > Mobile >  How to start on ServiceStack?
How to start on ServiceStack?

Time:03-10

Can you help me point out how should I start on this:

I'm new to API , and I'm currently working on ASP.NET Core 3.1 MVC paired with Microsoft SQL Server. I have requirement that I should use API (ServiceStack) for a certain method.

My question are :

  • how or where do I start from my existing project solution?
  • If I use API should it be calling on SQL also? (I supposed the data will stay on db)
  • with regards to first question : they gave me a link where I can see this. Link

Where should I start , I'm just so confused.

I've looked up on youtube but there's no similar case to mine, they all use in-memory. Suggestions and advice are welcome !

CodePudding user response:

Go through ServiceStack's Getting Started Section starting with Create your first Web Service.

Configure OrmLite in your AppHost

To configure OrmLite, start with the OrmLite Installation tells you which package to download whilst the OrmLite Getting Started docs lists all the available SQL Server Dialects which you'd use to configure the OrmLiteConnectionFactory in your IOC.

E.g. for SQL Server 2012:

public class AppHost : AppHostBase
{
    public AppHost() : base("MyApp", typeof(MyServices).Assembly) { }

    // Configure your ServiceStack AppHost and App dependencies
    public override void Configure(Container container)
    {
         container.AddSingleton<IDbConnectionFactory>(
            new OrmLiteConnectionFactory(connectionString, 
            SqlServer2012Dialect.Provider));
    }
}

Using OrmLite in Services

Then inside your ServiceStack Services you can access your ADO .NET DB connection via base.Db which you can use with OrmLite's extension methods, e.g:

public class MyServices : Service
{
    public object Any(GetAllItems request) => new GetAllItemsResponse {
        Results = Db.Select<Item>()
    };
}

Checkout the OrmLite APIs docs for different APIs to Select, Insert, Update & Delete Data.

Creating effortless RDBMS APIs using AutoQuery

As you're new I'd highly recommend using AutoQuery RDBMS since it lets you create RDBMS APIs with just Request DTOs.

You can enable it by adding the AutoQueryFeature plugin in the ServiceStack.Server" NuGet package:

public override void Configure(Container container)
{
    Plugins.Add(new AutoQueryFeature { MaxLimit = 100 });
}

Then you can create an AutoQuery API for your Item table with just:

[Route("/items")]
public class QueryItems : QueryDb<Item> {}

Which will now let you query each Item column using any of AutoQuery's implicit conventions, e.g by exact match:

/items?Id=1

Or by any of the query properties:

/items?NameStartsWith=foo

Creating Typed Request DTO

Once you know which Query APIs your client Apps needs I'd recommend formalizing them by adding them as strong typed properties in your Request DTO, e.g:

[Route("/items")]
public class QueryItems : QueryDb<Item> 
{
    public int? Id { get; set; }
    public string NameStartsWith { get; set; }
}

Calling from Service Clients

Which will enable an end-to-end Typed API using any of ServiceStack's Service Clients, e.g:

var client = new JsonServiceClient(BaseUrl);

var response = client.Get(new QueryItems { NameStartsWith = "foo" });

response.PrintDump(); // quickly view results in Console

There's also AutoQuery CRUD APIs that will let you create APIs that modify your RDBMS tables using just Request DTOs.

  • Related