Skip to content

Instantly share code, notes, and snippets.

View changhuixu's full-sized avatar
💭
Everyone has a happy ending. If you're not happy, it's not the end.

Changhui Xu changhuixu

💭
Everyone has a happy ending. If you're not happy, it's not the end.
View GitHub Profile
function deco(
target: any,
propertyKey: string,
descriptor: PropertyDescriptor
) {
console.log(`method "${propertyKey}" decorator: begin`);
if (descriptor === undefined) {
descriptor = Object.getOwnPropertyDescriptor(target, propertyKey);
}
const originalMethod = descriptor.value;
@changhuixu
changhuixu / WebBlogsDbContext.cs
Created July 11, 2018 18:26
WebBlogsDbContext.cs
public class WebBlogsDbContext : Microsoft.EntityFrameworkCore.DbContext
{
public virtual DbSet<Author> Authors { get; protected set; }
public WebBlogsDbContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Author.cs
public class Author
{
public int Id { get; protected set; }
public string FirstName { get; protected set; }
public string LastName { get; protected set; }
private readonly List<Blog> _blogs = new List<Blog>();
public IReadOnlyCollection<Blog> Blogs => _blogs;
}
// AuthorEntityTypeConfiguration.cs
internal class AuthorEntityTypeConfiguration : IEntityTypeConfiguration<Author>
{
public void Configure(EntityTypeBuilder<Author> builder)
{
builder.ToTable("Authors");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id).HasColumnName("Id").ValueGeneratedOnAdd();
builder.Property(x => x.FirstName).HasColumnName("FirstName").HasMaxLength(50).IsRequired();
export class CanvasAnimation {
private readonly context: CanvasRenderingContext2D;
constructor(private readonly canvas: HTMLCanvasElement) {
this.context = this.canvas.getContext('2d');
window.requestAnimationFrame(() => this.draw());
}
draw() {
// do stuff
@changhuixu
changhuixu / calculateMouseRelativePositionInCanvas.ts
Created July 13, 2018 21:51
correctly get mouse cursor relative position in canvas
private calculateMouseRelativePositionInCanvas(e: MouseEvent) {
// Note: I have handled scroll effect
this.mousePosition.x =
e.clientX +
(document.documentElement.scrollLeft || document.body.scrollLeft) -
this.canvas.offsetLeft;
this.mousePosition.y =
e.clientY +
(document.documentElement.scrollTop || document.body.scrollTop) -
this.canvas.offsetTop;
public class LotNamesCache : ILotNamesCache
{
public const string CacheKey = nameof(LotNamesCache);
private readonly ImmutableCacheDbContext _dbContext;
private readonly IMemoryCache _cache;
public LotNamesCache(ImmutableCacheDbContext dbContext, IMemoryCache cache)
{
_dbContext = dbContext;
_cache = cache;
[TestMethod]
public async Task ShouldSetGetCache()
{
var options = new DbContextOptionsBuilder<ImmutableCacheDbContext>()
.UseInMemoryDatabase(databaseName: "LotNamesCacheTests")
.Options;
using (var dbContext = new ImmutableCacheDbContext(options))
{
var lot1 = Mock.Of<ParkingLot>(x => x.LotCode == "02" && x.LotName == "Lot 02");
@changhuixu
changhuixu / quill-editor.component.ts
Created March 24, 2019 03:31
Lazy Load External JavaScript Library in Angular
declare var Quill: any;
@Component({
//...
})
export class QuillEditorComponent implements OnInit {
constructor(
// ...
private readonly svc: QuillEditorService,
const isObject = obj => obj && obj.constructor && obj.constructor === Object;
function merge(dest, src) {
for (var attr in src) {
if (isObject(dest[attr]) && isObject(src[attr])) {
merge(dest[attr], src[attr]);
} else {
dest[attr] = src[attr];
}
}
return dest