MVC3 ImportMany on Constructor Parameter for Exported Controller - constructor

I have a controller that is exported using MEF and loaded by the Controller factory.
[Export(Controller)]
public class MyController : Controller
{
private IRepository MyRepsoitory;
[ImportMany]
public IEnumerable<MyImportedItem> TestImportItems {get;set;}
public MyController([ImportMany]IEnumberable<MyImportedItem> items, [Import]IRepository repository)
{
// items here is always null
// However if I grab the container that the ControllerFactory used and tell it ComposeParts on this the TestImportItems will be filled with 50+ items
// repository however is instantiated appropriately.
GlobalItems.Container.ComposeParts(this);
//Now TestImportItems if filled but my items parameter alway null... how do I get constructor to fill
}
}
So MEF creates MyController but only creates the repository and sends null for the ImportMany even though it can fill the property later with the same Container.
What's also odd is if I do something that breaks one of the items the creation of MyConroller breaks in ControllerFactory.. as if it checks that is has parts for the constructor but never pushes them to the IEnumerable parameter.
What am I missing?
Obviously I have the parts available if the same Container works for .ComposingParts on (this) (and I reflected the catalog which has appropriate import/export Parts available at time of creating the Controller.
I could rewrite my class to use the filled Property but I would really like my importing constructor to get a filled collection.
UPDATE:
If I add a simple wrapper class for the import many MEF will load the [ImportMany] parameter.
So the following will fill the IEnumerable for me...
public MyController(TestImportClass test, [Import]IRepository repository)
{
//test.Items != null
}
public class TestImportClass
{
public IEnumberable<MyImportedItem> Items {get;set;}
[ImportingConstructor]
public TestImportClass([ImportMany]IEnumberable<MyImportedItem> items)
{
this.Items = items;
}
}
I am using a "Convention" system in my actual code to mark the Controller for Export. Maybe for some reason that is causing MEF to not understand the Import on initial Constructor Parameter? If that were the case though i am not sure why my IRepository always gets filled?

When you call ComposeParts, you pass objects which have already been constructed. It's not possible to call the constructor again on an existing object. (And in this case if you did you'd end up with infinite recursion). So ComposeParts doesn't satisfy constructor imports.
If your controller is pulled from the container some other way, and you put an ImportingConstructorAttribute on the constructor, the constructor imports should be satisfied.

Probably the convention system you are using doesn't support ImportMany in constructor arguments. Presumably the convention isn't applying to TestImportClass which is why the ImportMany works on that constructor.
We plan to have official convention model support in the next version of MEF, and we should be shipping a new codeplex release with a preview of this support soon.

Related

How to lock/unlock a resource?

First 2 parts of this query has been addressed in link and further would like to understand more about third part before implementation.
Part III (3 points)
Implement a "lockable" doubly-linked list ADT: a list in which any node can be "locked." A locked node can never be removed from its list. Any attempt to remove a locked node has no effect (not even an error message). Your locked list classes should be in the list package alongside DList and DListNode.
First, define a LockDListNode class that extends DListNode and carries information about whether it has been locked. LockDListNodes are not locked when they are first created. Your LockDListNode constructor(s) should call a DListNode constructor to avoid code duplication.
Second, define a LockDList class that extends DList and includes an additional method
public void lockNode(DListNode node) { ... }
that permanently locks "node".
Your LockDList class should override just enough methods to ensure that
(1) LockDListNodes are always used in LockDLists (instead of DListNodes), and
(2) locked nodes cannot be removed from a list.
WARNING: To override a method, you must write a new method in the subclass with EXACTLY the same prototype. You can’t change a parameter’s type to a subclass. Overriding won’t work if you do that.
Your overriding methods should include calls to the overridden superclass methods whenever it makes sense to do so. Unnecessary code duplication will be penalized.
It is important to understand locking and unlocking a DListNode without using any existing Java package. With an approach of, boolean flag; being member of LockDListNode to represent lock/unlock status, setting of this flag is non-atomic. Because, if i set flag = true; this set operation could be a multi-line instruction at byte code level.
Once this clarification that became a bottleneck to implement lock/unlock is resolved, Inheritance and override stuff could be taken care easily after this.
My question:
Can you suggest an approach to implement lock/unlock feature on DListNode?
Note: This query has nothing to do with knowledge of Java.
The new class LockDListNode will extend DListNode. Now you need to understand inheritance. DListNode should have an delete operation method you need to override that method not not do anything. See the code template below:
public class DListNode {
public DListNode{
}
public void delete(){
// TODO some delete code here
}
}
public class LockDListNode extends DListNode {
public LockDListNode{
super();
}
#override
public void delete(){
// DO nothing here
}
}
Now if you do
DListNode unableToDeleteNode = new LockDListNode();
unableToDeleteNode.delete();
You created an object of LockDListNode and call delete method it will call the delete for the LockDListNode and not DListNode.

AS3: inline way to write get and set?

I was reading a tutorial and the user there was starting setters and getters like so
function get f():Number;
function set f(value:Number):void;
inside an interface, then saving it in the main file inside a variable
var testNode:INode;
and referencing them as
testNode.f;
I thought this was really handy, specially for when you have a lot of variables. Instead of having to create two functions for each private value.
However, I tried to do the same without instancing the get and set inside an interface (because I had no need for this) and I get an error saying the function doesn't have a body.
Why is that? Is there any way to write get and set in such a clean, short manner? So far I've been writing these as
public function get someVar():SomeClass {
return _someVar;
}
public function set someVar(newValue:SomeClass):void {
_someVar = newValue;
}
but it's a bit of a hassle when I have several private variables.
An interface is just a model for your class, it does not allows you to skip the definition of a function (or a getter/setter). You must declare the getter and the setter in any class implementing your interface. What you are looking for is an extension (inheritage).
If you define a class Mother.as with a getter and a setter, the class Child.as which extends Mother.as doesn't need to redefine the getter and setter.
Furthermore, if you don't plan on writing anything else inside the getter and setter, you should use a public variable. These are less resource consuming than useless get/set.

Passing Model to View constructor for Stage Instances

I'm looking for an elegant solution to setting the Model argument for a View that is a stage instance.
My View constructor looks like this:
public function View($model:Model, $controller:IController=null){ ... }
My Subclass constructor:
public function ViewSubClass($model:Model, $controller:IController=null){ ... }
The idea is that the subclass will be a UI element that I'd rather just have on stage and not have to position it manually. I've thrown around the possibility of having placeholder elements that get removed and using their positions to attach the real UI elements but it seems a little hackish to me.
I suppose I could always set the default value of the $model argument in View to null, but not all views will be stage instances so I don't exactly want to do that either.
Any thoughts are appreciated.
Flash has no way to populate constructor parameters. This is why I always suggest avoiding constructor parameters for View. If this is your own Class, I'd suggest to go ahead and fix the parent Class so it doesn't need this.
If it is not, you can use
public function VewSubClass() {
super(null, null);
}
I would be concerned here that you actually need the Model and controller, so I would make setters available on the View subclass and then populate those variables once the instance arrives on stage.
public function set model(value:Model):void {
_model = value;
//do whatever the super constructor did based on receiving the model
}
public function set controller(value:IController):void {
_controller = value;
//etc.
}
Note that it probably shouldn't be necessary for the View to know about the controller, and I wouldn't suggest that the entire model be given to the View either--just the few properties it needs. So if the Class is yours, that puts you into a better position to correct these (IMO) architectural problems.
Based on the signature you've provided, I wouldn't be at all surprised to find that the model and controller internal storage are private, rather than protected, which means you're pretty much faced with a rewrite anyway (assuming the model and controller are needed on the View).
I solved the issue by changing the ViewSubClass constructor to:
public function ViewSubClass ($model:Model=null, $controller:IController=null){ ... }
Not sure why I was thinking that the constructor had to match the superclass' constructor perfectly. I'm still open to more elegant solutions if there are any. Thanks for looking.
-Veo

How do I pass reference of an initialised object from one class to another class, to add (addChild) to display list?

I created this simple example
because I was using a more complex class, a menu item that I wanted to initialise all the settings in the Main class and then add it in in the Game class (and updating it) when needed (both classes are separate)
Class: Main (document class, is (ideally) where everything is initialised/created)
public class Main extends MovieClip
{
//testing passing on reference to Game
private var testBitmap:Bitmap;
private var testBitmapData:BitmapData;
private var testArray:Array;
public function Main():void
{
testBitmapData = new BitmapData(256, 256, false, 0xCCDDEE);
testBitmap = new Bitmap(testBitmapData);
testArray = [];
testArray.push(testBitmap); //Array for reference
game = new Game(540, 960, testArray);
//create Game class, pass reference to Object
game.x = 0;
game.y = 0;
}
}
Class: Game (created by document class, is (ideally) where everything runs)
public class Game extends MovieClip
{
private var testingArray:Array
public function Game(gameWidth:int, gameHeight:int, testArray:Array):void
{
this.testingArray = testArray; //assign to local Array and access
addChild(testingArray[0]);
//addChild to Game from an item intialised in Main, doesn't work >___<
}
}
.
.
.
the thing is, in my original Game class; it receives an initial bundle of cached BitmapData and a list Array that tells it which BitmapData it needs to cycle through
cut-down here (and that reference only for updating works (if I addedChild in Main already):
public function Game(gameWidth:int, gameHeight:int, cachedBitmapClipArray:Array)
{
this.cachedBitmapClipArray = cachedBitmapClipArray;
private function update():void
{
for each (tempCachedBitmapClip in cachedBitmapClipArray)
{
tempCachedBitmapClip.updateCurrentTile();
//but updating through the reference to an item initialised in Main works !!
}
}
}
.
how do I make the reference and passed in objects (or have access to) behave as in the original instance ?? (being able to addChild)
i.e. can objects cross 'scopes' (??) or should objects only be controlled (instantiated) in the class where they have been initialised
Well to answer the last question, yes objects can be passed from one object to another. I'm having a hard time understanding what exactly the problem is though. In generic programming terms Object or any other complex type are passed by reference, primitive types are also passed by reference but the internals of the AVM handle them in such a way as to treat them as passed by value. For a pretty clear (in my eyes at least) explanation of how arguments are passed via function/method calls, read here:
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f56.html
Check out other areas in the tree navigation on the left for more details on the language itself.
One thing to note though I think the compiler will work around this error, in the first bit of code you posted Game has a return value of :void for a constructor there should be no declared return type since it should be implicitly typed as whatever the class is.
To add some explanation in my own words regarding pass by reference or pass by value. Pass by value means that the value of the object, that is the bytes stored at the location pointed to by the variable are actually copied to a new memory location which is then used. What this means in practice is they are not the same bytes of memory but rather they are two separate memory locations each with the same value stored in them, modification of one value after the passing doesn't affect the original. Pass by reference is to say you pass the memory location therefore no new memory for the value is allocated, both values are actually pointing to the same memory location (the pointer to them itself may be different but that pointers both point to the same location in memory). In this case the objects are the same.
What you're doing is advisable, dividing the labor and enapsulating particular types of functionality in classes does make the code easier to work with. I think the only problem would be if the Game object itself is never added as a child to something in the display tree (something that is attached to the stage). It appears you're creating a new Game and setting it's position but never adding it as a child to the stage, then adding anything to Game is never a part of the display tree.

Choosing the correct constructor using an external configuration file in Windsor?

I'm new to Windsor, but I'm certain there must be a way to do this...
I have a class with three different constructors:
public MyClass(string SomeParam)
{
...
}
public MyClass(string AnotherParam, string YetAnother)
{
...
}
public MyClass(string AnotherOne, string YeahIKnow, string AnnoyingExampleParam)
{
...
}
In my external configuration file, I have my service defined as:
<component
id="IMyClass"
service="IMyInterface, MyAssembly"
type="MyClass, MyOtherAssembly">
<parameters>
<AnotherOne>string value #1</AnotherOne>
<YeahIKnow>string value #2</YeahIKnow>
<AnnoyingExampleParam>string value #3</AnnoyingExampleParam>
</parameters>
</component>
When Windsor initializes an instance of my class, it only wants to initialize using the first (single string parameter) constuctor of my class, when I really want Windsor to use the third constructor.
I don't see anything in the docs about forcing the kernel to us a particular constructor using an external configuration, even though I can find references to doing it in code, but that sort of defeats the purpose of an external configuration!
Any advice would be appreciated.
Best,
David Montgomery
What version of Castle? I recall, from the depths of what goes for my memory at 4am in the morning, that there was a resolution for constructor work in Castle 2.0.
Humm, memory coming back a little now... Something tells me that Castle will construct the object with the first public ctor. May be as simple as moving what you want for Castle to load, to the top.
If that doesn't work for you, perhaps refactor your code a little?
Option 1) Make the first two constructors internal.
Option 2) Use a Factory pattern for your complex objects, which will utilize castle on the backend to new up the more simple or complex version.
Option 3) Create 3 classes from your base superclass, each having a more complicated constructor. This way, you can specific in the Castle config file exactly which service to load. For example:
public abstract class BaseClass
{
public BaseClass(String requiredParam)
{
...
}
}
public class SimpleClass : BaseClass
{
public SimpleClass(String requiredParam, String anotherParam)
: base(requiredParam)
{
...
}
}
public class MoreComplexClass : SimpleClass
{
public MoreComplexClass (String requiredParam, String anotherParam, String yetAnother)
: base(requiredParam, anotherParam)
{
...
}
}
But, I myself have not run into this yet. Mainly because I stick to only public 1 ctor on my classes, with a few private/internal ctors for things such as Linq to new up my objects with an empty ctor (since Linq doesn't support Dependency Injection, boo).
Note that in that last statement, internal ctors, that my SRP (Single Responsiblity Pattern) for resolving my IoC components is external, in a seperate higharchy assembly (i.e. an application or UI layer). Since it not internal to my domain objects, the internal ctors are not seen by Castle.
You must be doing something wrong.
Windsor uses the greediest constructor it can satisfy. If it uses the smaller one, you perhaps have some typo?
when your type is the service, you don't have to specify both
service="MyClass, MyAssembly"
type="MyClass">
remove the type.