Click here to Skip to main content
15,900,818 members
Home / Discussions / C#
   

C#

 
GeneralRe: Static Constructors/Static Initializers Pin
KnyteHawkk10-Dec-09 11:16
KnyteHawkk10-Dec-09 11:16 
GeneralRe: Static Constructors/Static Initializers Pin
PIEBALDconsult10-Dec-09 11:45
mvePIEBALDconsult10-Dec-09 11:45 
GeneralRe: Static Constructors/Static Initializers Pin
KnyteHawkk10-Dec-09 11:46
KnyteHawkk10-Dec-09 11:46 
AnswerRe: Static Constructors/Static Initializers Pin
Ian Shlasko10-Dec-09 11:28
Ian Shlasko10-Dec-09 11:28 
GeneralRe: Static Constructors/Static Initializers Pin
PIEBALDconsult10-Dec-09 11:44
mvePIEBALDconsult10-Dec-09 11:44 
GeneralRe: Static Constructors/Static Initializers Pin
Ian Shlasko10-Dec-09 12:14
Ian Shlasko10-Dec-09 12:14 
GeneralRe: Static Constructors/Static Initializers Pin
PIEBALDconsult10-Dec-09 12:41
mvePIEBALDconsult10-Dec-09 12:41 
AnswerRe: Static Constructors/Static Initializers Pin
PIEBALDconsult11-Dec-09 11:05
mvePIEBALDconsult11-Dec-09 11:05 
OK, you asked for it... (or maybe you didn't, I don't care, thanks for the challenge anyway)

I now have a lazy-instantiating, generic, enum/attribute-based instance manager with caching. Jig | [Dance]

There is a bunch of Reflection, but it doesn't get performed unless it's requested and then the results are cached for future use.

The main down-side is that Attributes can't be generic and the types of parameters for Attribute constructors is limited. Dead | X|


The result is that you can write a family of business classes and attach instances of them to enumerated values via an Attribute:

public enum DoerRegistry
{
    [DoSomethingAttribute( typeof(Yell) , "More, more, more" )]
    Rebel
,
    [DoSomethingAttribute( typeof(Yell) , "Hey, you" )]
    Pink
,
    [DoSomethingAttribute( typeof(Whisper) , "Mary" )]
    Wind
,
    Whatever
}


Ideally, the Attribute could be generic:
[DoSomethingAttribute<Whisper>( "Mary" )]

Another possibility would be if the constructor could accept the instance directly:
[DoSomethingAttribute( new Whisper ( "Mary" ) )]
Although that wouldn't allow for lazy-instantiation.

Yet a third possibility is for the business class to be the Attribute, but I don't think that's a very good design.


You then pass that enumeration to the manager and access the various business class instances with its indexer:

ValueMapManager<DoerRegistry,DoSomethingAttribute,IDoSomething> manager = 
    new ValueMapManager<DoerRegistry,DoSomethingAttribute,IDoSomething>() ;

manager [ DoerRegistry.Wind ].DoWhatYouDoBest() ;

IDoSomething doer ; 

doer = manager [ DoerRegistry.Rebel ] ;
doer.DoWhatYouDoBest() ;



It takes a bit of code to do this, but most of it can be shared among whatever projects/applications require it. The only project-specific code is the classes you need and the enumeration. The interfaces, Attribute, and Manager shouldn't require maintenance.

If this looks like something that you may find useful, let me know and I'll post the code.
Questionzedgraph Pin
jashimu10-Dec-09 7:25
jashimu10-Dec-09 7:25 
AnswerRe: zedgraph Pin
Luc Pattyn10-Dec-09 7:32
sitebuilderLuc Pattyn10-Dec-09 7:32 
GeneralRe: zedgraph Pin
jashimu10-Dec-09 8:27
jashimu10-Dec-09 8:27 
Question<key value=""> collection as a parameter in an interface method</key> Pin
Chiman110-Dec-09 5:51
Chiman110-Dec-09 5:51 
GeneralRe: collection as a parameter in an interface method [modified] Pin
Abhinav S10-Dec-09 6:22
Abhinav S10-Dec-09 6:22 
GeneralRe: collection as a parameter in an interface method Pin
Chiman111-Dec-09 17:34
Chiman111-Dec-09 17:34 
QuestionSend a class object to a webservice Pin
Gktony10-Dec-09 5:32
Gktony10-Dec-09 5:32 
AnswerRe: Send a class object to a webservice Pin
hammerstein0510-Dec-09 5:52
hammerstein0510-Dec-09 5:52 
GeneralRe: Send a class object to a webservice Pin
Gktony10-Dec-09 6:08
Gktony10-Dec-09 6:08 
GeneralRe: Send a class object to a webservice Pin
hammerstein0510-Dec-09 6:30
hammerstein0510-Dec-09 6:30 
Questionvariable names Pin
benjamin yap10-Dec-09 4:59
benjamin yap10-Dec-09 4:59 
AnswerRe: variable names Pin
Keith Barrow10-Dec-09 5:05
professionalKeith Barrow10-Dec-09 5:05 
AnswerRe: variable names Pin
OriginalGriff10-Dec-09 5:05
mveOriginalGriff10-Dec-09 5:05 
AnswerRe: variable names Pin
PIEBALDconsult10-Dec-09 5:18
mvePIEBALDconsult10-Dec-09 5:18 
GeneralRe: variable names Pin
benjamin yap10-Dec-09 5:22
benjamin yap10-Dec-09 5:22 
GeneralRe: variable names Pin
Abhinav S10-Dec-09 5:32
Abhinav S10-Dec-09 5:32 
GeneralRe: variable names Pin
benjamin yap10-Dec-09 5:39
benjamin yap10-Dec-09 5:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.