Skip to content

Instantly share code, notes, and snippets.

@TianyuanC
Last active September 15, 2015 23:43
Show Gist options
  • Save TianyuanC/9e19549819f61b9e52e5 to your computer and use it in GitHub Desktop.
Save TianyuanC/9e19549819f61b9e52e5 to your computer and use it in GitHub Desktop.
markdown c#

DALs

Build status Coverage Status NuGet

Tired of writing the same block of code for the data access layer for each project, I extracted the common pieces out and put tons of unit test coverage.

  • Execute Stored Procedure using SqlTransaction, SqlConnection and SqlCommand
  • Access Restful Services using HttpClient
  • Azure storages...

Nuget

  • PM> Install-Package DALs.Core -Version 1.0.6-beta -Pre

Usage

Update/Insert

        /// <summary>
        /// update ad.
        /// </summary>
        /// <returns>Task&lt;System.Boolean&gt;.</returns>
        public async Task<bool> UpdateAsync()
        {
            var content = new SqlParameter("content", SqlDbType.VarChar) { Value = "Test" };
            var id = new SqlParameter("id", SqlDbType.Int) { Value = 1 };
            var config = new SqlConfiguration(ConnectionString, "UpdateAd") { SqlParameters = new List<SqlParameter> { content, id } };
            return await sqlClient.CommandAsync(config) > 0;
        } 

Retrieve

        /// <summary>
        /// Gets ads.
        /// </summary>
        /// <returns>Task&lt;IEnumerable&lt;Ad&gt;&gt;.</returns>
        public async Task<IEnumerable<Ad>> GetAsync()
        {
            var config = new SqlConfiguration(ConnectionString, Settings.GetAd, SprocMode.ExecuteReader);
            return await sqlClient.QueryAsync(config, AdsLoader);
        }

        /// <summary>
        /// Ads loader.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns>IEnumerable&lt;Ad&gt;.</returns>
        public static IEnumerable<Ad> AdsLoader(IDataReader reader)
        {
            var ads = new List<Ad>();

            while (reader.Read())
            {
                var adId = reader.Get<int>("ID");
                var lastMod = reader.Get<DateTime>("ModificationDate");
                var content = reader.Get<string>("Content");
                var counter = reader.Get<long>("Counter");
                var ad = new Ad
                {
                    AdID = adId,
                    LastModificationDate = lastMod,
                    Content = content,
                    TestCounter = counter
                };
                ads.Add(ad);

            }
            return ads;
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment