Skip to content

Instantly share code, notes, and snippets.

View ro31337's full-sized avatar

Roman Pushkin ro31337

View GitHub Profile
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Box
{
public interface IAsset
{
@ro31337
ro31337 / gist:e6e9fbe29a4d2463a4f0
Created August 3, 2014 10:19
xmpp4r-simple.rb on Ruby 1.9+
...
presence = case new_presence.type
when nil
new_presence.show || :online
when :unavailable
:unavailable
else
nil
end
...
public class CompanyService
{
// ...
public Company GetById(long companyId)
{
return dbContext
.Companies
.FirstOrDefault(x => x.Id == companyId && !x.IsDeleted);
}
public class CompanyService
{
// ...
public Company GetById(long companyId)
{
return dbContext
.Companies
.FirstOrDefault(x => x.Id == companyId);
}
public class MyContext : DbContext
{
public virtual IDbSet<Company> Companies { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Company>()
.Map(m => m.Requires("IsDeleted").HasValue(false))
.Ignore(m => m.IsDeleted);
}
public class CompanyService
{
// ...
public Delete(long companyId)
{
var company = GetById(companyId);
company.IsDeleted = true;
database.SaveChanges();
}
}
public class CompanyService
{
// ...
public Delete(long companyId)
{
var company = GetById(companyId);
company.DeletedAtUtc = DateTimeOffset.Now;
database.Companies.Remove(company); // it's like we usually delete
database.SaveChanges();
}
public class MyContext : DbContext
{
//...
public override int SaveChanges()
{
foreach (var entry in ChangeTracker.Entries()
.Where(p => p.State == EntityState.Deleted))
SoftDelete(entry);
private void SoftDelete(DbEntityEntry entry)
{
Type entryEntityType = entry.Entity.GetType();
string tableName = GetTableName(entryEntityType);
string primaryKeyName = GetPrimaryKeyName(entryEntityType);
string sql =
string.Format(
"UPDATE {0} SET IsDeleted = 1 WHERE {1} = @id",
public partial class MyContext
{
private static Dictionary<Type, EntitySetBase> _mappingCache =
new Dictionary<Type, EntitySetBase>();
private string GetTableName(Type type)
{
EntitySetBase es = GetEntitySet(type);
return string.Format("[{0}].[{1}]",