ReactiveUI.Routing dependency on Splat - mvvm

The ReactiveUI.Routing requires us to register the views in Splat container (Locator.CurrentMutable). If I don’t register it with Splat it is not working.
If we are using some other IOC container or service locator like Ninject, is tere a way to provide our own container to ReactiveUI.Routing? Or still we should register the views in Splat?

I see 2 ways to do that, you can fully replace the splat locator default implementation by NInject, here's a post of someone who did that.
You can also set the ViewLocator property on your RouteViewHost (before its activation), which won't go to Splat once it's set.

Related

Powershell: Dynamically gather data types for IIS configuration element validation

I'm attempting to dynamically use the Microsoft.Web.Administration namespace within a powershell cmdlet.
Is there a way to add a variable into a Namespace.Class declaration.
I'm using the namespace [Microsoft.Web.Administration] and it's class [Microsoft.Web.Administration.ApplicationPool], it has a property under it 'Recycling' that you can access using a GetProperty method.
[Microsoft.Web.Administration.ApplicationPool].GetProperty("Recycling")
If you spit out the PropertyType of the above using this
[Microsoft.Web.Administration.ApplicationPool].GetProperty("Recycling").PropertyType.FullName
You get this result,
Microsoft.Web.Administration.ApplicationPoolRecycling
which is another class. I now want to access this class dynamically within the cmdlet. How do access this class from within the code, I want the code to dynamically discover the new class of the object and then access that class. But I can't find a way to accomplish this.
Psuedocode for what I'm trying
[System.Reflection.Assembly]::LoadFrom( "C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll" )
$appPoolProperty = [Microsoft.Web.Administration.ApplicationPool].GetProperty($Property)
$subClassName = $appPoolProperty.PropertyType.FullName
#This is the step I'm lost on
$subClass = New-Object $subClassName
So I'm not sure if there's a way to have a TypeName for an object put in as a string value and I can't seem to find a way to cast the variable as anything else. Sorry if this is remedial, I'm a basement grown coder and just learn things as I go.
EDIT: As requested by Mathias below in the comments, an overview of what I'm trying to achieve.
I have a script that automates the configuration of many IIS components. At this time I'm attempting to add validation to the cmdlet Set-AppPoolConfiguration that I've created that allows a configuration to be fed into the cmdlet that configures an AppPool (this is used to deploy websites/weapplications throughout a distributed web environment). Utilizing the details inside the Microsoft.Web.Administration I'm able to get enum values, as well as types of the many configuration components of an AppPool. This is saving me time to where I don't have to hard code in the data types and can just dynamically discover them and do validation on the values when the specific configuration element is chosen.
For example. If an AppPool Recycle schedule is need to be configured, I need to validate it against a TimeSpan data type. If it is not a TimeSpan data type issues will arise when being added to the ScheduleCollection. So I'm looking to validate the value provided before attempting to add it.
Since there are many AppPool configuration elements, I don't want to have to create a massive switch or if/elseif chain that checks each configuration element and statically dictate what data type it is for validation. I want the class itself to dynamically provide this information to simplify the code.
I can get the majority of these data types by simply accessing the property chain within the namespace. For example, if you want to know what type is required for the QueueLength, use this:
[Microsoft.Web.Administration.ApplicationPool].GetProperty("QueueLength").PropertyType.Name
And you'll get Int64.
It's also extremely useful for getting enums.
[Microsoft.Web.Administration.ApplicationPool].GetProperty("ManagedPipelineMode").PropertyType.GetEnumNames()
However attempting this with Schedule and you run into a small issue as it returns ScheduleCollection. This is true of any of the configuration elements that are part of a collection.
[Microsoft.Web.Administration.ApplicationPool].GetProperty("Recycling").PropertyType.GetProperty('PeriodicRestart').PropertyType.GetProperty('Schedule').PropertyType.Name
However the knowledge that the schedule item inside the ScheduleCollection is only accessible from within the Schedule class. My code currently checks to see if it is a collection, and then if it is, it is attempting to access that collection/class and get the information that is required. To find out that schedule is a TimeSpan you have to access it's specific class instance:
[Microsoft.Web.Administration.Schedule].GetProperty('Time').PropertyType.Name
Now AppPools are simple, there's only a single collection group that is normally edited, so hard coding in that if you're attempting to set a new recycle schedule it will be a TimeSpan isn't that big of a deal, however when we move over to WebSite/WebApplication configurations, it becomes more tedious to statically declare data types for each configuration element that is part of a collection, and becomes more useful to try and discover these dynamically based on the configuration element selected.
This was my initial approach, I just included the above for clarity. I'm going to step back and take another look at how to attack this as this does not appear to be as easy as I had hoped, I'll post my solution here.
You can retrieve the constructor from the type literal and invoke it like so:
$type = [Microsoft.Web.Administration.ApplicationPoolRecycling]
$ctor = $type.GetConstructor('NonPublic,Instance',$null,#(),$null)
$appPoolRecyclingInstance = $ctor.Invoke($null)
Though there may be a way to do the above, in order to complete the updates to my cmdlet and proceed forward with my project I went a hybrid route.
The reason why I started exploring the [Microsoft.Web.Administration] namespace was that it provided information on the data types where the typical way I was manipulating IIS settings failed using Get/Set/Add-WebConfigurationProperty.
The specific failure is in reporting back valid data types for Enums. Take for instance ProcessModel.IdentityTypes. There is a set of valid entries for an IdentityType. However the following doesn't provide you with those valid types, so you either have to create static instances of them inside your cmdlet, or some other external data source, whereas I wanted Microsoft to provide them either through IIS itself, or through the classes attached to these configuration elements so the cmdlet would need minimal updating as IIS versions/configuration elements change.
This code returns Boolean
(Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.applicationHost/applicationPools/add[#name='$AppPool']" -name "AutoStart").Value.GetType().Name
However, this code returns string, which is true, but I needed to know that it is an enum and I needed to know the proper values.
(Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.applicationHost/applicationPools/add[#name='$AppPool']" -name "processModel").identityType.GetType().Name
Using a mix of both the Namespace and the traditional Get-WebConfigurationProperty commands I can successfully now test for data types, as well as gather proper values for enums dynamically in code. If there is interest I can post the entire validation code here that I'm using for this cmdlet.

How to Install a module that needs an instance per something else that is registered in Castle Windsor

I am trying to get the hang of IoC and DI and am using Castle Windsor. I have an object I have created that can be multiply instantiated but over different generic types. For example
MyType<Generic, Generic2>
on Installation of MyType's assembly
container.Register(Component.For(typeof (IMyType<>)).ImplementedBy(typeof (MyType<>)));
Then in my main modules initialization I install MyTypes module with MyTypeInstaller which is a IWindsorInstaller.
Then I am manually resolving the various types of MyType that I want (this will actually be spread around different installers). But something like
container.Resolve<IMyType<type1, type2>();
That creates an actual instance of MyType registered for the generic types passed in.
This works fine, I get the instances of MyType<,> I need created.
Now, finally I have another module I have created that I will install last. I want to say,
container.ResolveAll<IMyType<,>>()
then create this instances of this new object for each object that exists.
However I cant seem to resolve all of the IMyTypes<,> without knowing the concrete types that each one were instantiated as.
At any rate, it is possible I am just doing this wrong and want feedback in general as well.
First, if MyType<T1,T2> can only be instantiated once for each combination of T1,T2 then you should be registering it is a Singleton.
Second, You cannot call strongly type container methods (like ResolveAll<T>) with an open generic - it MUST be a closed type.
Third, MyType is an open generic type and the number of closed generic classes is infinite (generic type constraints are not considered by the container). So, as far as the container is concerned you can call Resolve<ANYTHING, ANYTHINGELSE> and it will attempt to provide a MyType<ANYTHING,ANYTHINGELSE> for you. If ANYTHING and ANYTHINGELSE don't satisfy the type constraints then you will simply get a run time error.
Even if you could call ResolveAll<IMyType<,>>() what would you expect it to return given that you have registered an open generic implementation?

How to mock multiple fields of same type in GwtMockito

I understand that #GwtMock annotation creates a mock of a particular type when run with GwtMockitoTestRunner, but what if I had multiple items of a particular type in my widget? For example if I had many buttons, or many anchors in my widget, is there a way to create mocks for each one individually?
Right now I am thinking of bypassing this shortcoming in my test with something like
#GwtMock mockButtonn;
and then later on
when(mockButton.something()).thenReturn(value1,value2,value3,...)
so I can distinguish the buttons based on order of invocation. But this is definitely unmaintainable. Can someone suggest an alternative?
There are a few different ways to use the mocks generated by GwtMockito, take a look at the documentation at http://google.github.io/gwtmockito/ if you haven't already. It depends on how you're creating your widgets:
If you're creating widgets via UiBinder (fields annotated with #UiField), you don't have to do anything special - they will automatically be filled with mocks when you call createAndBindUi. You can then reference them directly in your tests, e.g. when(myClass.myLabel.getText()).thenReturn("some text"). This works because fields have to be package-private for UiBinder, so you can see them from your tests.
If you're passing widgets into your class via its constructor or some other way (dependency injection), then just declare fields in your test using the normal Mockito #Mock annotation. GwtMockito will invoke Mockito to fill these in automatically, but otherwise it's just the same as any other Mockito mock.
If you're creating widgets via GWT.create, this is where #GwtMock is valuable. The only difference between #GwtMock and #Mock is that #GwtMock also makes it so any calls to GWT.create will return the object annotated with #GwtMock. So it doesn't really make sense to annotate multiple fields of the same type with #GwtMock, since GWT.create can return only one of them.
If you're creating widgets directly via new, don't do that! It will be impossible to create fake version for testing, and you should use dependency injection instead.
1 and 2 are the most common cases by far - it's somewhat uncommon to actually have to use #GwtMock.

Getting a new Shell instance from CompositionContainer in Prism 4 with MEF

I'm coming across some strange MEF behaviour in Prism, which I can't really explain. I've found a way around it that I'm not too happy with, so I'd really like to understand what's causing it.
I've declared my shell window class with a PartCreationPolicy of NonShared. And I'm trying to use the CompositionContainer.GetExportedValue<>() function from my MefBootstrapper to create a new instance of the Shell.
The strange thing is, if I call Container.GetExportedValue<>() before the shell has been created, I get a new object of type Shell, each time I call it. However, once the shell has been initialized, repeated calls to Container.GetExportedValue<>() return the same instance of the Shell.
It's as if the shell initialization somehow re-registers my Shell export as a Shared.
However, I don't see any calls in the bootstrapper code that explicitly try to achieve this.
Can anyone explain:
what action has this side effect
How (if possible) to restore the NonShared behaviour, so I can create multiple shells using MEF/ServiceLocator.
Cheers,
Mark
I'm not sure how Prism uses MEF, but here's a theory: How is the shell being created in normal startup? My guess is that it is not by calling GetExportedValue from the MEF container, but rather by calling the constructor for the Shell and then adding it to the container via ComposeParts() or using a CompositionBatch. A part added directly to the container in that way would override what was available in the catalog, and the CreationPolicy wouldn't apply either (because MEF isn't creating that part).
here is a answer for your multiple shell question. you have to check if the NonShared behavior is answered there.

Castle Windsor - Null constructor argument

How can I pass a null constuctor argument using Castle Windsor? I thought the following would work
<parameters>
<repository>null</repository>
<message>null</message>
</parameters>`
If you want them to be null, it means that they are non-essential dependencies. By having them as ctor arguments you suggest otherwise. You should redesign your class to have another constructor that takes only essential dependencies, if you wish that they not change throughout the lifetime of an object (be readonly), or expose them as properties.
With Windsor you can't make it to pass nulls, for reasons mentioned in the other answer.
Wouldn't it better to simply have an additional public constructor that doesn't take these parameters, then you wouldn't need to register the parameters in the config?
This was discussed a while back on the mail list, and at the time I looked into the code. Null values are deliberately filtered out (mainly because the complicate type resolution).
I couldn't find a simple way to make a special case to add them.