Skip to content

Instantly share code, notes, and snippets.

@grefly
Created August 27, 2010 19:12
Show Gist options
  • Select an option

  • Save grefly/553984 to your computer and use it in GitHub Desktop.

Select an option

Save grefly/553984 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericImplementationMapping
{
internal class AutomatedNoteStrategyCustomer : IsAAutomatedNoteStrategy<Customer>
{
private Customer _parent;
public AutomatedNoteStrategyCustomer(Customer Parent)
{
_parent = Parent;
}
public string Text
{
get { string.Format("Automated Note for {0}",_parent.Name); }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericImplementationMapping
{
internal class AutomatedNoteStrategyInvoice : IsAAutomatedNoteStrategy<Invoice>
{
private Invoice _invoice;
public AutomatedNoteStrategyInvoice(Invoice Parent)
{
_invoice = Parent;
}
public string Text
{
get { return string.Format("Automated Note cost {0}",_invoice.Cost.ToString()); }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericImplementationMapping
{
public class Customer : IsANoteParent
{
public string Name { get { return "Customer Name"; } }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericImplementationMapping
{
public class Invoice : IsANoteParent
{
public Invoice()
{
Cost = .02M;
}
public decimal Cost { get; private set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericImplementationMapping
{
internal interface IsAAutomatedNoteStrategy<ParentT>
{
string Text { get; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericImplementationMapping
{
public interface IsANoteParent
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericImplementationMapping
{
public class Note<ParentT> where ParentT : IsANoteParent
{
private IsAAutomatedNoteStrategy<ParentT> automated_note_strategy;
private ParentT _parent;
public Note(ParentT Parent)
{
_parent = Parent;
// So here, I want to set the AutomatedNoteStrategy,
// but in order to do that, I have to always test the
// type of the Parent - seems like I should be able
// to change the signature of the AutomatedNoteStrategy
// constructor to accept a Func that returns a string
// or something, but I still *always* need to know
// the concrete type, and I have to implemnt either:
// 1. A map from ParentT to AutomatedNoteStrategy or
// ex: ObjectFactory.For<IsAAutomatedNoteStrategy<Customer>>().Use<AutomatedNoteStrategyCustomer>()
// or
// 2. if typeof ParentT is Customer { ... }
//
// I kinda want to do something like (although this isn't completely thought out):
// automated_note_strategy = AutomatedNoteStrategy<ParentT>(x => x.Name);
//
// Refactorings *extremely* welcome, but working on a legacy system...
}
public string Text { get; set; }
public void SetAutomatedText()
{
Text = automated_note_strategy.Text;
}
}
}
@grefly
Copy link
Author

grefly commented Aug 27, 2010

The Note uses the Parent to know which ID and Type to relate to, as Notes can be "attached" to different things in the system.

@coreyhaines
Copy link

I'd take a serious look at whether you are mixing responsibilities here. Perhaps the functionality needs to be pulled out somewhere else. I'd have to see it all. With this code, I'd wonder why the strategy isn't the thing being passed in, not the note.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment