Introduction
Starcounter 3.0.0 Release Candidate is available for general access.
Downloads:

Starcounter 3.0 release candidate 20191212 main changes

    Starcounter failover setup using Windows Failover Cluster.
    The star tool is a cross-platform console application with SQL REPL & database import/export features.
    Starcounter apps can now be distributed as a regular .NET Core app in framework dependent and self contained modes. Read more.
    It's now possible to execute DML & DDL SQL statements using the star tool.
    Starcounter namespace has been changed from Starcounter.Nova.App to Starcounter.Database.

Dependency Injection (DI) and Starcounter

Requirements

Note: Please let us know if you encounter any issues while working with Starcounter. We monitor our GitHub: Starcounter/Home issue tracker and stand ready to assist.

Installation

Note: This section assumes that you have required operating system and .NET Core 3.0.100 SDK installed.

Binaries

    Create a folder for Starcounter binaries, for example Starcounter.3.0.0-rc-20191212.
    Download Starcounter.3.0.0-rc-20191212.zip into the folder.
    Unzip downloaded archive into the folder.

Windows 10

On Windows Starcounter requires x64 version of Visual C++ to be installed. Download and install it from the Microsoft website:
Make sure to Unblock the archive after downloading prior to extracting it. See the screenshot below:

Ubuntu 18.04 & 19.10

Install prerequisites.
1
sudo apt-get install wget unzip
2
sudo apt-get install libaio1 libtinfo5
Copied!
Starcounter relies on a specific version of SWI-Prolog.
1
sudo add-apt-repository ppa:swi-prolog/stable
2
sudo apt-get update
3
sudo apt-get install swi-prolog-nox=7.\*
Copied!
Download and unpack Starcounter binaries.
1
cd $HOME
2
mkdir Starcounter.3.0.0-rc-20191212
3
cd Starcounter.3.0.0-rc-20191212
4
wget https://starcounter.io/Starcounter/Starcounter.3.0.0-rc-20191212.zip
5
unzip Starcounter.3.0.0-rc-20191212.zip
Copied!

Application

Create an application folder and initialize a .NET Core console application.
1
mkdir StarcounterConsoleSample
2
cd StarcounterConsoleSample
3
4
dotnet new console
Copied!
All the following commands shall be executed from the StarcounterConsoleSample folder.
Setup NuGet to consume Starcounter packages feeds.
Create nuget.config file and add required package sources:
    local, points to the Starcounter binaries folder.
    Starcounter, points to https://www.myget.org/F/starcounter/api/v2.
NuGet tips:
    Default NuGet.config file can be created with dotnet new nugetconfig command.
    For more information on how to work with NuGet configurations, see this post by Microsoft.
End file should look similar to this:
1
<?xml version="1.0" encoding="utf-8"?>
2
<configuration>
3
<packageSources>
4
<clear />
5
<add key="local" value="[Starcounter.3.0.0-rc-20191212]" />
6
<add key="Starcounter" value="https://www.myget.org/F/starcounter/api/v2" />
7
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
8
</packageSources>
9
</configuration>
Copied!
Visual Studio Setup:
Visual Studio requires manual NuGet package sources configuration. For this go to the Tools → Options → NuGet Package Manager → Package Sources menu then add local and Starcounter feeds.
Note: Replace the [Starcounter.3.0.0-rc-20191212] value with the actual path to the folder with unzipped Starcounter binaries.
Add Starcounter.Database package reference
1
dotnet add package Starcounter.Database --version 3.0.0-*
Copied!
Add minimal Starcounter database access
Replace content of the Program.cs file with the following:
1
using System;
2
using System.Linq;
3
using Starcounter.Database;
4
using Microsoft.Extensions.DependencyInjection;
5
6
namespace StarcounterConsoleSample
7
{
8
[Database]
9
public abstract class Person
10
{
11
public abstract string Name { get; set; }
12
}
13
14
public class Program
15
{
16
public static void Main()
17
{
18
string connectionString =
19
"Database=./.database/StarcounterConsoleSample;"
20
+ "OpenMode=CreateIfNotExists;"
21
+ "StartMode=StartIfNotRunning;"
22
+ "StopMode=IfWeStarted";
23
24
// Here we create a service collection that
25
// we add the Starcounter services to.
26
// When we call BuildServiceProvider(), we get an instance
27
// that we can use to fetch service instances,
28
// for example ITransactor, which we then can use to
29
// to make database transactions.
30
using var services = new ServiceCollection()
31
.AddStarcounter(connectionString)
32
.BuildServiceProvider();
33
34
// Here we fetch our ITransactor instance from the service provider.
35
var transactor = services.GetRequiredService<ITransactor>();
36
37
// And here we use it to make a database transaction.
38
var name = transactor.Transact(db =>
39
{
40
// Here inside the transaction,
41
// we can use the IDatabaseContext instance
42
// to interact with the Starcounter database.
43
44
// We can query it using SQL
45
// (which returns an IEnumerable<Person>
46
// that we can use with LINQ).
47
var p = db.Sql<Person>
48
(
49
"SELECT p FROM Person p WHERE Name = ?",
50
"Jane"
51
).FirstOrDefault();
52
53
if (p == null)
54
{
55
// We can insert new rows in the database using Insert().
56
p = db.Insert<Person>();
57
58
// We write to the database row
59
// using standard C# property accessors.
60
p.Name = "Jane";
61
}
62
63
// Let's return the name as result of the transaction.
64
return p.Name;
65
});
66
67
// And let's print it in the console
68
Console.WriteLine(name);
69
}
70
}
71
}
Copied!
For ASP.NET Core applications:
Update Startup.cs class with the following:
1
using Microsoft.AspNetCore.Builder;
2
using Microsoft.Extensions.DependencyInjection;
3
4
namespace StarcounterMvcSample
5
{
6
public class Startup
7
{
8
public void ConfigureServices(IServiceCollection services)
9
{
10
string connectionString =
11
"Database=./.database/StarcounterMvcSample;"
12
+ "OpenMode=CreateIfNotExists;"
13
+ "StartMode=StartIfNotRunning;"
14
+ "StopMode=IfWeStarted";
15
16
// This adds and configures Starcounter services to our application,
17
// for example ITransactor,
18
// that allows us to create database transactions.
19
services.AddStarcounter(connectionString);
20
}
21
}
22
}
Copied!
Note: Starcounter works with Kestrel Web Server only. IIS and IIS Express are not yet supported.

Running with Visual Studio Code

    Open Visual Studio Code in the application folder (from command line: code ./).
    Restore dependencies Visual Studio Code asks for.
    Click Ctrl + F5 to start the application.
Everything should run out of the box.

Running with Visual Studio 2019

    Update Visual Studio 2019 to the latest version using Visual Studio Installer.
    We checked version 16.3.0.
    Open StarcounterConsoleSample.csproj from Visual Studio.
    Click Ctrl + F5 to start the application.

Extra information

Before asking questions or reporting issues, please read these few lines, and maybe you will find an answer for your question.
Last modified 1yr ago