Skip to content

Instantly share code, notes, and snippets.

View thaianhduc's full-sized avatar

Thái Anh Đức thaianhduc

View GitHub Profile
using Microsoft.Extensions.Configuration;
namespace CoolApp.ApplicationService
{
public class JsonAppConfigService : IAppConfigService
{
private readonly string _defaultCurrency;
public JsonAppConfigService(IConfiguration configuration)
{
_defaultCurrency = configuration["DefaultCurrency"];
namespace CoolApp.ApplicationService
{
public interface IAppConfigService
{
string GetDefaultCurrency();
}
}
@thaianhduc
thaianhduc / IIndexedTeacherCollection.cs
Last active January 6, 2019 04:39
Improved version of TeacherCollection design
namespace DotConnect.LeakyAbstraction
{
public interface IIndexedTeacherCollection
{
int GetIndex(string teacherName);
}
}
@thaianhduc
thaianhduc / TeacherCollection.cs
Last active January 6, 2019 04:26
A teacher collection which wraps the logic of operations on teachers. To support getting the index, the GetIndex method is introduced and the collection is ordered by the creation date by default on the constructor. This demonstrates a design issue.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace DotConnect.LeakyAbstraction
{
public class TeacherCollection : IEnumerable<Teacher>
{
private readonly IList<Teacher> _teachers = new List<Teacher>();
@thaianhduc
thaianhduc / sql_drop_database.sql
Created December 12, 2018 14:44
A right, safe way to drop a database
IF DATABASEPROPERTYEX (N'DB_NAME', N'Version') > 0
BEGIN
ALTER DATABASE [DB_NAME] SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
DROP DATABASE [DB_NAME];
END
GO
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Demo the readability of the code from method parameters");
var hr = new HrDepartment();
var applicantCvs = new List<VerificationContext>{
new VerificationContext()
{
ApplicantCv = {ApplicantName = "Batman"},
@thaianhduc
thaianhduc / ApplicantVerification.cs
Last active December 8, 2018 03:40
A simple piece of code to demonstrate the code readability by looking at method's parameters.
using System;
using System.Collections.Generic;
using System.Linq;
public class VerificationContext
{
public string SpecialNotes { get; set; }
public ApplicantCv ApplicantCv { get; set; }
}
@thaianhduc
thaianhduc / sql_configure_blocked_threshold.sql
Last active December 4, 2018 10:32
Configure the SQL Server to monitor the blocking issues. Use SQL Profiler (or custom trace) with the Blocked process report under Errors and Warnings category
-- Configure the blocked process threshold for 10 seconds.
-- Be careful with the number. If we set it too low, there will be too many events generated.
EXEC sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'blocked process threshold', 5;
GO
RECONFIGURE;
GO
@thaianhduc
thaianhduc / sql_sp_trace.sql
Last active December 4, 2018 10:26
Script to create custom trace to monitor various events inside SQL Server without much overhead when using SQL Profiler.
DECLARE @TraceID INT;
DECLARE @filesize BIGINT = 10;
EXECUTE sp_trace_create
@TraceID = @TraceID OUTPUT,
@Options = 2, -- TRACE_FILE_ROLLOVER,
@tracefile = N'D:\a location\tracecfilewithoutextension',
@maxfilesize = @filesize,
@stoptime = NULL,
@filecount = 5;
SELECT L.request_session_id AS SPID,
DB_NAME(L.resource_database_id) AS DatabaseName,
O.Name AS LockedObjectName,
P.object_id AS LockedObjectId,
L.resource_type AS LockedResource,
L.request_mode AS LockType,
ST.text AS SqlStatementText,
ES.login_name AS LoginName,
ES.host_name AS HostName,