SqlMirror 2.0.0-beta47

This is a prerelease version of SqlMirror.
dotnet add package SqlMirror --version 2.0.0-beta47
                    
NuGet\Install-Package SqlMirror -Version 2.0.0-beta47
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SqlMirror" Version="2.0.0-beta47" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SqlMirror" Version="2.0.0-beta47" />
                    
Directory.Packages.props
<PackageReference Include="SqlMirror" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SqlMirror --version 2.0.0-beta47
                    
#r "nuget: SqlMirror, 2.0.0-beta47"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SqlMirror@2.0.0-beta47
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SqlMirror&version=2.0.0-beta47&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=SqlMirror&version=2.0.0-beta47&prerelease
                    
Install as a Cake Tool

SqlMirror

This project is not an ORM but acts as a facilitator to DataAccess based in Microsoft's IDbX interfaces, like IDbConnection. Because of this you can use it with SqlServer and any other implementation.

Why

Because I can!!

Really, I did it as a way to improve my knowledge of the language (C#) and Roslyn Source Generators without to much worrying on code organization.

Cloning

I actually this there are much better examples for this but feel free to use it. You just have to clone it and open the solution with Visual Studio.

Installation

If you just want to give it a spin and you're lazy as I am, just go and get the available nuget.

Usage

There are a few requirements that you must take care in your code for the magic to happen.

There must be a partial class and it must be decorated with the SqlMirror.Database attribute.

   [SqlMirror.Database()]
   public partial class Database
   {}

This will be detected as a "database proxy" where all your data access methods will be hold (hence the name, get it?!!)

There must be either at the class scope or just passed into each method an IDbConnection or a derived type.

NOTE that every method in this class will be processed as a data access method, including but not only private methods.

    private partial void DoSomething();

    partial int DoOtherthing();

For example the first example will generate the following code (notice the

        private partial void DoSomething()
        {
            if (connection is null)
            {
                throw new NullReferenceException(nameof(connection));
            }

            var con = connection;
            var connectionState = con.State;
            using var command = con.CreateCommand();
            command.CommandType = System.Data.CommandType.Text;
            command.CommandText = "select * from DoSomething";
            try
            {
                if (con.State != System.Data.ConnectionState.Open)
                {
                    con.Open();
                }

                command.ExecuteNonQuery();
                return;
            }
            finally
            {
                if (connectionState != ConnectionState.Open)
                {
                    con.Close();
                }
            }

            return;
        }

Then, attributes are the configuration pattern for SqlMirror

Attributes

SqlMirror.Database (Targets:Class)

As previously noted, its the marker for any (can have multiple) data access class.

Property TableNamePrefix (optional string)

Allows a prefix for all tables

Example

    [SqlMirror.Database(TableNamePrefix = "mytables_")]
    public partial class Database
    {
        public List<Person> Persons();

will generate a query

    command.CommandText = "select * from mytables_DoSomething"
Property StoredProcedureNamePrefix (optional string)

Allows a prefix for all stored procedures

Besides the Database attribute there are also some method attributes that, although we try to arrange the best way do calls to the database, I strongly suggest you use any of the Method attributes as they allow some fine tuning of the process.

All of them share some properties, like

Property ExecutionMode (optional ExecutionModes)

It will instruct SqlMirror the mode to execute the query/command by choosing

  1. Auto as the default mode,
  2. Command wich will run a command returning only the records affected,
  3. Query that will return rows from the database,
  4. Single wich will only return a single row,
  5. Scalar for returning the value of the first column of the first row.

Property MapToFields (optional bool)

If true the data returned will be mapped to the object's fields instead its properties.

Property UseAsyncAwait (optional bool)

If true and the underlying connection allows, it will run with async/await.

Note that by default this will happen so the best usage is to prevent the asyncronism.

Property ConfigureAwait(optional bool)

Async/Await implemented feature.

Property StrictAssignment (optional bool)

If true, to every column returned from the database must match a property/field in the target entity (by default if a column does not have a match its value is discarded).

Property ReturnTypeIncludeInheritLevel (optional bool)

By default only the property/fields declared in the target object are used unless you specify a inheritance level.

Property CommandTimeOut (optional int)

The time in ms the command will wait for the database

SqlMirror.QueryTextAttribute (Targets: Method)

Property Query (string)

Accepts a string as an Sql Query.

Deployment

Additional notes on how to deploy this on a live or release system. Explaining the most important branches, what pipelines they trigger and how to update the database (if anything special).

Server

  • Live:
  • Release:
  • Development:

Branches

  • Master:
  • Feature:
  • Bugfix:
  • etc...

Additional Documentation and Acknowledgments

  • Project folder on server:
  • Confluence link:
  • Asana board:
  • etc...
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

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
2.0.0-beta47 122 9/16/2023
1.0.1-Beta103 156 5/24/2023
0.1.1-g0d2e7efb3d 191 12/23/2022
0.0.0-dev-84 335 7/6/2022
0.0.0-dev-102 191 11/7/2022