Skip to content

Instantly share code, notes, and snippets.

View mgroves's full-sized avatar

Matthew D. Groves mgroves

View GitHub Profile
[Serializable]
[MulticastAttributeUsage(MulticastTargets.Method, TargetMemberAttributes = MulticastAttributes.Public)]
public class ServiceErrorInterceptor : MethodInterceptionAspect
{
public override bool CompileTimeValidate(MethodBase method)
{
if (!typeof (ServiceBase).IsAssignableFrom(method.DeclaringType))
{
var declaringType = method.DeclaringType;
var declaringTypeName = "Unknown";
public override void OnInvoke(MethodInterceptionArgs args)
{
try
{
args.Proceed();
}
catch (SqlException ex)
{
var obj = (ServiceBase) args.Instance;
if (ex.Message.Contains("The DELETE statement conflicted with the REFERENCE constraint"))
void ErrorReturn(MethodInterceptionArgs args)
{
try
{
var methodInfo = args.Method as MethodInfo;
if (methodInfo == null)
return;
if (methodInfo.ReturnType == typeof(string)) // if it's string, just return a null
args.ReturnValue = null;
else if (typeof(IEnumerable).IsAssignableFrom(methodInfo.ReturnType))
public void OnInvoke(MethodInterceptionArgs args)
{
try
{
args.Proceed();
}
catch (Exception ex)
{
// ... snip ...
ErrorReturn(args);
public ActionResult MyAction()
{
var listResults = _service.GetAllResults(); // an exception here gets logged, but execution continues
var model = new MyViewModel();
model.NumResults = listResults.Count; // listResults is null, NullReferenceExcpetion here
return View(model);
}
var sb = new System.Text.StringBuilder(468);
sb.AppendLine(@"One of my favorite VS extensions is SmartPaster. Often when I'm pasting something into Visual Studio, I'm pasting it into C# source code. It could be a long directory name, a JSON string, or some sort of template that's going into a StringBuilder. Doing this with plain copy/paste can be tedious.");
sb.AppendLine(@"");
sb.AppendLine(@"Instead, just install SmartPaster. You'll get a new right-click menu option: ""Paste As"", which lets you paste text as a literal string, a comment, or as a StringBuilder.");
public static class IoC {
public static IContainer Initialize() {
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
// This is StructureMap 2.x
using StructureMap;
public static class IoC {
public static IContainer Initialize() {
ObjectFactory.Initialize(x =>
{
// ... snip ...
x.For<IDbConnection>().HttpContextScoped().Use(GetConnection);
// each entity uses this as a base
// different entities have different types of keys (guids, string, ints typically)
public class EntityBase<T> : IEntity<T>
{
public T Id { get; set; }
}
public class SomeEntity : EntityBase<int>
{
public string Foo { get;set;}
"../../../packages/FluentMigrator.1.1.2.1/tools/Migrate.exe" /conn "Data Source=(local);Initial Catalog=mydatabasename;uid=;pwd=;Trusted_Connection=yes;" /provider sqlserver2008 /assembly "myproject.migrations.dll" /verbose=true --task rollback --steps=%1