Created
August 27, 2010 19:12
-
-
Save grefly/553984 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"; } } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| namespace GenericImplementationMapping | |
| { | |
| internal interface IsAAutomatedNoteStrategy<ParentT> | |
| { | |
| string Text { get; } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| namespace GenericImplementationMapping | |
| { | |
| public interface IsANoteParent | |
| { | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } | |
| } |
Author
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.
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
My first thought is that you are mixing abstractions here. Why do you have Note knowing about both the strategy and the parent object. That smells a bit to me like a design issue. What is it doing with the parent?