Unity Register type mapping if missing? - configuration

This is similar to This question but the answer doesn't solve my problem.
I want to register a default implementation for an interface in code but be able to override that in a config that is read BEFORE the code is run.
I want to do something like
if (!container.IsImplementationRegistered(typeof(TInterface))
{
container.RegisterType<TInterface, TImplementation>();
}
I can't use TryResolve as I will be registering several types in a row that might have dependencies of their own that aren't yet registered, leading to an erroneous override.
Will Unity 2.0 enable this or is there a way to do it in 1.2?

Maybe you can try with a child container.
Create a container in the code
Create a child container using CreateChildContainer()
Configure the child container reading from the config file
Use the child container in the application.
If the child container cannot resolve a type, Unity goes to the parent container to resolve.

Related

clone Xaml FramworkElement in WinRT

i need to clone a FrameworkELement in my CodeBehind in WinRT...
I did found a solution in the internet, though this workaround doesn't work in WinRT because the XamlWriter is NOT available in winRT! Is there an easy/built-in way to get an exact copy (clone) of a XAML element?
is there any other way to get another instance of my FrameworkElement?
I don't think there is an easy way to clone an element exactly - I don't know for example if there is a way to figure out the arbitrary attached properties set on one or figure out if properties are set by style, animation, template, explicit value etc.
There is one way that would possibly be a solution for your scenario if you have a specific element tree you want cloned - simply put it in a DataTemplate in XAML and then retrieve that template by name or resource key in code behind and call LoadContent() to generate an instance from the template.
If you have your original one in your XAML already that you don't want to put in resources and generate or lay out from code behind again - simply wrap it inside ContentControl/ContentTemplate/DataTemplate.

Correct use of addChild

I'm new to coding and AS3. I was reading about adding things to the stage using AS3 and learned about the addChild method. Reading more I found that there are different ways to use it. I also read that some ways are better than others, and that some ways are not good at all and better avoided.
I don't trust those sources though. As a coding newbie I come asking for help, StackOverflow. I'm about to start an addChild heavy project and I want to start it with the right implementations. I trust you, so let me ask you this: What's the correct use of addChild?
I just want to add things to the stage, but I read that it's not good to add them directly to the stage (without further argument though).
stage.addChild()
this.addChild()
addChild()
Are there other ways? Which one should I use?
Thanks for your time. :)
As the top-level container for all display objects in the display list hierarchy, there is only one Stage no matter how many SWF files are loaded into the runtime. So, generally, objects should not be added to the Stage, directly, at all. The only object the Stage should contain is the root object.
Generally, you should not use: stage.addChild()
Adding a DisplayObject to the display list should be performed within the scope of a DisplayObjectContainer.
Each SWF file has an associated ActionScript class, known as the main class of the SWF file which extends a display object. From this class or any child within the hierarchy you may call addChild().
The following are equal, and would add a child within the scope of the current display object container.
this.addChild()
addChild()
The this keyword explicity defines scope; however, is generally implicit when left off.
While a display object added via addChild() is added to the front (top) of all other children, to add a child to a specific index position, use the addChildAt() method.
References:
Display Programming
Basics of display programming
Adding display objects to the display list

Dojo, how destroy a custom widget?

I have created a custom dijit widget which contains a grid and some buttons.
What is the right way to destroy it? override uninitialize, destroy, destroyRecursive? which method and in which order?
Thanks.
Generally uninitialize is the best place to do this, since it is an extension point called within the destroy function before other teardown occurs.
That said, depending on how you are adding your child widgets, you may not actually have to do anything. For instance, if you are defining your child widgets within a template, widgets declared within a template automatically get added to an array which is iterated through in destroy.
If you wanted to be sure, for testing you could connect to the destroy methods of your child widgets to log a message when they get called.

How to dyamically adding Elements to Mootools Form.Validator?

Currently I am using Form.Validator from Mootools 1.2.5 and Mootools-More 1.2.5, but i am having a hard time validating an Element's input when dynamically injected into the DOM after ondomready. I was wonder if there's a way to attach Form.Validator's functionalities to the newly inject Elements?
UPDATE:
Using what #Dimitar suggested i was able to fix the issue. I use the build in function getFields to repopulate/attach events to the dynamic Elements. formValidatorObj.watchFields(formValidatorObj.getFields()); Hope this will help some Mootooler's in the future!
i am not a big -more users but looking at the source code on github, this seems like a good guess:
https://github.com/mootools/mootools-more/blob/master/Source/Forms/Form.Validator.js#L161
i guess you can pass any element - dynamically created or otherwise.
formValidatorObj.watchFields([someElsCollection]); // or from form.getElements or whatever.
// dynamically add a new field...
formValidatorObj.watchFields([new Element("input.required[value=John]").inject(formValidatorObj.element, "top")]);

IComponentActivator Instance

How can I use an IComponentActivator instance for a component, not just specifying a type.
That is, instead of
Component.For<XYZ>.Activator<MyComponentActivator>();
I want to be able say
Component.For<XYZ>.Activator(new MyComponentActivator(someImportantRuntimeInfo));
Also, is there a way I can choose an activator dynamically for a non specifically registered component? That is, I want to have an activator that looks at the type being resolved, decides if it can activate it, and if not, responsibility for activation should be passed on to the default activator.
So basically I want a global IComponentActivator that has the following logic:
Create(Type type){
if (ShouldActivate(type)){
DoActivate(type);
}
else{
// do default activation here somehow
}
}
Thanks!
EDIT
Just use ILazyComponentLoader
Activators are instantiated by the container, and there is no way to provide instances OOTB.
If you really want to do this, I'd say you can extend the container itself.
Put your custom instace activators in ExtendedProperties of your component under a well-known key
inherit from DefaultKernel
Override CreateComponentActivator method
In there return the activator from ExtendedProperties of the component
There's no way to create global activator. You can add IContributeComponentModelCreation implementation that switches the activator to some custom one wen creating your component if you really want to.
But the most important question is this:
What are you trying to achieve by
this? Why do you want to do this in
the first place?