Iota 1.1.12
dotnet add package Iota --version 1.1.12
NuGet\Install-Package Iota -Version 1.1.12
<PackageReference Include="Iota" Version="1.1.12" />
paket add Iota --version 1.1.12
#r "nuget: Iota, 1.1.12"
// Install Iota as a Cake Addin #addin nuget:?package=Iota&version=1.1.12 // Install Iota as a Cake Tool #tool nuget:?package=Iota&version=1.1.12
Overview
Iota is a C# code generator that creates classes and methods from SQL Server stored procedures or queries.
Features and benefits
- Generate classes based on columns selected by a procedure
- Generate classes based on parameters used by a procedure
- Generate static instances for tables with rows that rarely change (i.e.
CustomerStatus
) - Generate methods that call procedures
- Generate synchronous or asynchronous methods
- Handle common input or output parameters (i.e.
@ID_CURRENT_USER
,@MODIFIED_ON
,@ERROR_MESSAGE
etc.) - Generated code works on .Net Framework and .Net Core
- Very fast
Usage
Use it in three simple steps:
- Create an instance of
Iota.SqlServer.CSharp.ProgramGenerator
- Add the stored procedures and what code you want to generate from them
- Write the generated code
Examples
Create the program generator
using Iota.Metadata;
using Iota.SqlServer.CSharp;
...
ProgramGenerator generator = new ProgramGenerator(sqlServerConnectionString)
{
DefaultNamespace = "MyProject.Entities", // Set namespace for generated classes
DefaultAsyncMode = AsyncMode.Asynchronous, // Choose from synchronous or synchronous methods
DefaultTimeout = 120, // Set default timeout (in seconds) for SQL commands
AdapterNamespace = "MyProject.Services", // Set namespace for generated adapter
AdapterName = "DataAdapter", // Set class name for generated adapter
NullableReferenceTypes = false // Set to true to generate nullable reference types
};
Create types from stored procedures
Generate class Customer
from columns selected by the procedure and class CustomerFilter
from procedure's parameters:
generator
.WithProcedure("dbo", "usp_Customers_Select")
.AddType("Customer")
.AddTypeFromParameters("CustomerFilter");
Handle common parameters
When most of the procedures have the same parameter (i.e. a parameter called @ID_CURRENT_USER
that holds the logged in user id) use this syntax to automatically handle the parameter(s) for all procedure calls:
generator.AddDefaultParameterService(
new ParameterService
{
ParameterNames = new string[] { "IdCurrentUser" },
Initializer = true, // Initialize the parameter before calling the procedure
HideParameters = true // Hide the parameter when generating classes and methods
});
After this, you can write a method to initialize the current user for all commands:
namespace MyProject.Services
{
partial class DataAdapter
{
void InitializeIdCurrentUser(ref int? idCurrentUser)
{
// TODO: Add your logic here
}
}
}
Map table types
Create a class for a table type and map all appearances of the table type to the new class:
generator.MapTableType("dbo", "TblTypCustomers", "TblCustomer");
Map table types with a single column
Map a table type that contains a single column to a basic C# type:
generator.MapTableType("dbo", "TblTypIntKeys");
Create list methods
Generate method CustomersSelect
that receives a CustomerFilter
, calls the stored procedure and returns a list of Customer
instances:
generator
.WithProcedure("dbo", "usp_Customers_Select")
.AddList("Customer", "CustomersSelect", "CustomerFilter");
Create single object methods
Generate method CustomersGet
that calls the stored procedure and returns a single Customer
instance:
generator
.WithProcedure("dbo", "usp_Customers_Get")
.AddFirstObject("Customer", "CustomersGet");
Create non query methods
Generate method CustomersSave
that receives a Customer
and then calls the stored procedure passing all properties as parameters:
generator
.WithProcedure("dbo", "usp_Customers_Save")
.AddNonQuery("CustomersSave", "Customer");
Generate method CustomersDelete
that calls the stored procedure:
generator
.WithProcedure("dbo", "usp_Customers_Delete")
.AddNonQuery("CustomersDelete");
Create scalar methods
Generate method CustomersGetRevenue
that calls the stored procedure and returns a single decimal value:
generator
.WithProcedure("dbo", "usp_Customers_GetRevenue")
.AddScalar("System.Decimal", "CustomersGetRevenue");
Generate method CustomerSelectContactNames
that calls the stored procedure and returns a list of string values:
generator
.WithProcedure("dbo", "usp_Customers_SelectContactNames")
.AddScalarList("System.String", "CustomerSelectContactNames");
Create static instances
Generate static instances of type CustomerStatus
from rows returned by a stored procedure, and place them in an array named Instances
generator
.WithProcedure("dbo", "usp_CustomerStatus_Select")
.AddType("CustomerStatus")
.AddInstances("CustomerStatus", "Name", "Instances");
Create constants
Generate constant fields in type CustomerStatus
from rows returned by a stored procedure. Each constant will have the same name as the status and the same value equal to the status id.
generator
.WithProcedure("dbo", "usp_CustomerStatus_Select")
.AddInstances("CustomerStatus", "Name", "IdStatus");
Write the generated code
Use this sample to write all generated code to a single file:
generator.WriteAll("Generated.cs");
Customization
- Use the
Write
method overrides to write the generated code to multiple files. - Use the optional parameters for the
Add
methods to customize individual stored procedure calls.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
.NET Framework | net46 is compatible. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
.NETCoreApp 3.1
- System.Data.SqlClient (>= 4.8.6)
-
.NETFramework 4.6
- No dependencies.
-
net5.0
- System.Data.SqlClient (>= 4.8.6)
-
net6.0
- System.Data.SqlClient (>= 4.8.6)
-
net7.0
- System.Data.SqlClient (>= 4.8.6)
-
net8.0
- System.Data.SqlClient (>= 4.8.6)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.1.12 | 136 | 6/19/2024 |
1.1.11 | 197 | 5/31/2023 |
1.1.10 | 161 | 5/31/2023 |
1.1.9 | 152 | 5/31/2023 |
1.1.8 | 281 | 1/13/2023 |
1.1.7 | 454 | 2/6/2022 |
1.1.6 | 422 | 2/5/2022 |
1.1.5 | 396 | 12/10/2021 |
1.1.4 | 380 | 11/3/2021 |
1.1.3 | 437 | 11/21/2020 |
1.1.2 | 498 | 4/30/2020 |
1.1.1 | 467 | 4/24/2020 |
1.1.0 | 463 | 4/21/2020 |
1.0.4 | 638 | 9/28/2019 |
1.0.3 | 531 | 9/25/2019 |
1.0.2 | 575 | 9/23/2019 |
1.0.1 | 536 | 9/22/2019 |
net8.0 support