Mendz.Data.SqlServer is on GitHub and NuGet

Mendz.Data.SqlServer, which provides a generic Mendz.Data-aware context for ADO.Net-compatible access to SQL Server databases, is now on GitHub and NuGet!

Mendz.Data.SqlServer provides SqlServerDbDataContext, which derives from Mendz.Data.Common's DbDataContextBase, an implementation of IDbDataContext. Repositories based on DbRepositoryBase are expected to be passed with an IDbDataContext instance. SqlServerDbDataContext can be that instance.

SqlServerDbDataContext assumes that appsettings.json contains an entry/section for DataSettings.

{
    "DataSettings": {
        "ConnectionStrings": {
            "SqlServerConnectionString" : "connection string to Sql Server",
            "SqlServerExpressConnectionString" : "connection string to LocalDB"
        }
    }
}

In the application startup or initialization routine, the DataSettings should be loaded into DataSettingOptions as follows (the application must reference Mendz.Data):

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            DataSettingOptions.Initialize(
                Configuration.GetSection("DataSettings").Get<DataSettings>());
        }

Mendz.Data-aware repositories using SqlServerDbDataContext can have a skeleton that looks like the following:

    public class TestRepository : DbRepositoryBase<SqlServerDbDataContext>
    {
        ...
    }

In an ASP.Net MVC application, for example, the controller code can use TestRepository as follows:

    public class TestController : Controller
    {
        // GET: Test
        public ActionResult Index(int id)
        {
            using (TestRepository testRepository = new TestRepository())
            {
                return View(testRepository.Search(id, null));
            }
        }
    }

Why did I create SqlServerDbDataContext? Shouldn't Mendz.Data.Common define instead a DbDataContext instance that uses System.Data.Common? Now that, mind you, is a great idea. Unfortunately, it's not possible yet. DbProviderFactories, the magic that can make it happen, is still not available in .Net Standard 2.0 and in .Net Core 2.0. As of this writing, the issue is open at https://github.com/dotnet/corefx/issues/4571.

At the moment, the best way to start using Mendz.Data is to create concrete data context instances for the source/target data source. Just like how Mendz.Data.SqlServer is created, Mendz.Data.Common can be used to create ADO.Net compatible data contexts for your database vendor, like Oracle and MySQL, for example. You can use the Mendz.Data.SqlServer source code as a guide/pattern. The full source code is available at GitHub: https://github.com/etmendz/Mendz.Data.SqlServer.

If you are ready to create Mendz.Data-aware repositories that connect to SQL Server, and your strategy is to use ADO.Net or compatible libraries, like micro-ORM Dapper, to perform the actual data access, you can get Mendz.Data.SqlServer from NuGet: https://www.nuget.org/packages/Mendz.Data.SqlServer/. This package has dependency on Mendz.Data, which is included when you download/install Mendz.Data.SqlServer.

Comments