Skip to content

Instantly share code, notes, and snippets.

View melvinlee's full-sized avatar

melvinlee melvinlee

  • Singapore
View GitHub Profile
/**************************************************************************************
Version: 1.0.1
Delegate Extension Methods
***************************************************************************************/
using System;
using System.Windows.Forms;
public static class ControlExtension
{
@melvinlee
melvinlee / RepositoryDapper.cs
Last active August 29, 2015 14:11
Using Dapper Micro-ORM.
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Dapper;
public class Repository
{
private readonly Func<IDbConnection> _connection;
@melvinlee
melvinlee / TcpClient.cs
Last active August 29, 2015 14:25
Using TCP/IP connection to receive data from other party
public async override void Run()
{
if (string.IsNullOrEmpty(_socketObserver.Server) || (_socketObserver.Port == 0))
{
Logging("Socket NOT ready. Server address or port is not set.");
return;
}
_cts = new CancellationTokenSource();
@melvinlee
melvinlee / SerialPort.cs
Last active August 29, 2015 14:25
Receive bits from comm port using SerialPort
internal class FromSerialPort : BasedSource
{
private readonly CommPort _commPort;
private static System.IO.Ports.SerialPort _serialPort;
public FromSerialPort(CommPort commPort)
{
if (commPort == null) throw new ArgumentNullException("CommPort");
_commPort = commPort;
@melvinlee
melvinlee / AssemblyInfo.cs
Last active August 29, 2015 14:25
Using Log4Net
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4NetConfiguration.exe.xml", Watch = true)]
@melvinlee
melvinlee / function.cr
Created July 30, 2015 15:49
Converts seconds into a string in the format of days.hours:minutes:seconds. (0.00:05:12)
Function (NumberVar interval)
NumberVar Days := Truncate(interval / 86400);
NumberVar Hours := Truncate(Remainder(interval, 86400) / 3600);
NumberVar Minutes := Truncate(Remainder(interval, 3600) / 60);
NumberVar Seconds := Remainder(interval, 60);
Totext(Days,'##') +'.'+ Totext(Hours,'00') +':'+ Totext(Minutes,'00') +':'+ Totext(Seconds,'00')
@melvinlee
melvinlee / NameRegistrationFactory.cs
Created August 4, 2015 07:26
Register typeof T with named services
using System;
using System.Collections.Generic;
using System.Reflection;
namespace CallCosting.Reporting
{
public class ReportFactory
{
public ReportFactory()
{
@melvinlee
melvinlee / FakeItEasy.cs
Last active September 17, 2015 02:20
Unit test with moq framework
[Test]
public void Run_With_InboxMessages_Should_Call_Repo_Save()
{
//Arrange
var apiConfig = new ApiConfig();
apiConfig.SetCommPort(new CommPort());
var fakeApi = A.Fake<ISmsApi>();
var fakeLogging = A.Fake<ILogging>();
var fakeOutgoingsmsRepo = A.Fake<ISmsOutgoingRepository>();
@melvinlee
melvinlee / DependencyConfigure.cs
Last active September 10, 2015 10:06
ASP.NET MVC Autofac registration.
internal class DependencyConfigure
{
public static void Initialize()
{
var builder = new ContainerBuilder();
DependencyResolver.SetResolver(new AutofacDependencyResolver(RegisterServices(builder)));
}
private static IContainer RegisterServices(ContainerBuilder builder)
{
@melvinlee
melvinlee / ConnectionMapping.cs
Last active September 18, 2015 01:46
Generic Dictionary Mapping Helper
using System.Collections.Generic;
using System.Linq;
namespace Sample
{
public class ConnectionMapping<T>
{
private readonly Dictionary<T, HashSet<string>> _connections =
new Dictionary<T, HashSet<string>>();