last day (16 days later) » 

11:06 PM
0
Q: Creating a Delegating Factory with Autofac using a property

John KattenhornI'm trying to create a factory to help transform a class based on an interface (IIncomingMessage) into new instances of other classes (AMessage, BMessage) based on a property of the single class, like: public interface IIncomingMessage { public DeviceTypeEnum DeviceType {get;} } public clas...

 
Are you going to inject your factory in to the classes that depend on it, or instance it yourself with each use?
 
I was trying to stick with injecting it via the constructor as that my usual style but at the moment I settle for just getting it working anyway I could.
 
I assume that the enum is a predetermined value and is not changed once the class is instanced?
Unfortunately, since being compared against an instance property (the enum), you'll have to hold a cache of every single Type that implements the IIncomingMessage, with each one having been instanced. If you were to use an attribute, this would be a bit simpler with less overhead. I'm working an example showing how I have done it in the past
 
That's no so bad since there's only one implementation of IIncomingMessage right now, it needs to be transformed into many different types (one for each value in the enum) however (currently about 15 but will grow as new devices are added). Each new message type is transformed from an incoming bytearray differently depending on the devicetype. An example would be great, I hope this context make's it clearer.
 
It's alright, I can work around it. In my project I have a single interface and about 120 implementations of it (IRC Chat command converters). I do the differentiation via attributes so that I don't have to inject a collection of every implementation, pre-instanced. It lets me instance just 1, saving on creatng a bunch of objects that will get GC'd just to find one.
I don't see any if your example classes above implementing the IIncomingMessage interface. Are AMessage and BMessage supposed to implement that? It would be helpful if one of those Transform methods were implemented. I'm not sure how you are wanting to handle the DeviceType to AMessage check.
 
11:06 PM
I see what your saying about the attributes idea, that's interesting, I like to see how that might work if you have time to share. I've added some context I hope on the implementations that I missed out, I'm afraid I've done it without reference to real code so I hope it makes sense.
 
How are you thinking you'd want to compare your enum for a IMessageTransformer<T> that can be used to do the conversion?
Is it a one-to-one relationship? So if an IIncomingMessage being passed in to the transform method has an enum value of TypeA, then it is always going to transform in to AMessage?
The TypeA enum value can never represent any other Type?
 

  last day (16 days later) »