Does GWT deferred binding return singletons of the same type within the scope of a single EntryPoint? - gwt

Let's say I have a number of GWT modules which are used as libraries, and one module with an entry point which inherits all of the library modules.
Each of the submodules needs to access a single instance of SomeClass.
If I call GWT.create(SomeClass.class) in modules A & B, do I get the same instance? If so, is this guaranteed?

No. GWT.create(SomeClass.class) compiles to new SomeClass(), unless there is a rebind rule of some kind - a replace-with or a generate-with rule will cause this to instead invoke the default constructor of whatever type is selected by those rules.
This means that GWT.create is not a suitable way to provide access to a singleton instance. Instead, consider some DI tool like Gin, or manual DI by always passing around the same instance. It is also possible to use the static keyword to keep a single instance where all code compiled into the same app can reference it.

Related

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?

Are PlayPlugin instances shared between different threads (Play 1.2.5)

I'm trying to find out how PlayPlugin objects are used within Play Framework (1.2.5).
Are same PlayPlugin instances shared between different Play threads?
With some source lookup I suppose yes but since Play has some meta-programming in many places and I'm not so familiar with all this, I'm not 100% sure.
Call stack for PlayPlugin.beforeInvocation:
PlayPlugin.beforeInvocation
PluginCollection.beforeInvocation
list of enabled plugins is a field within PluginCollection)
Invocation.before
uses static field Play.PluginCollection
Thread.currentThread().setContextClassLoader(Play.classloader) is one thing that could possibly affect Play.PluginCollection, for example.
Single instance for all threads -behaviour would also be confirmed by the article Play Framework: Introduction to Writing Modules:
beforeActionInvocation(): This code is executed before controller
invocation. Useful for validation, where it is used by Play as well.
You could also possibly put additional objects into the render
arguments here. Several plugins also set up some variables inside
thread locals to make sure they are thread safe.
So, I suppose the answer is that yes, the instances are shared, but would like to confirm that.
You are right. Each instance of PlayPlugin(subclass of course) is shared throughout the entire JVM. You get that instance via Play.plugin(class<T> clazz) method call.

MEF: Importing on Fields

Is it recommended that we place an Import on a property instead of a field? I tried it on a field and it is working but Resharper is telling me a warning that the field was never initialized.
ReSharper doesn't recognize that MEF will be setting the variable and since there is no guarntee that MEF will be setting the variable (example if it isn't put into a container for example), so it is reasonable for ReSharper to warn about this. You can either ignore it or simply initialize the field to null (or default(T)).
As for whether or not you should use a property or field I think using a field is fine (assuming it is not public). I generally reserve properties for things I want to expose publicly. One special case to consider here is that there are some issues having Imports on private members in low trust scenarios like SL or paritial trust because MEF uses reflection and you cannot use private reflection in some of those scenarios.

Is there a workaround for setting [HostType("Moles")] when dealing with anonymous methods in MSpec?

I'm using Pex and Moles for my low-level unit testing, but I'm also exploring MSpec for business-logic validation, and would like to keep using Moles for consistency. The problem, I think, is that MSPec uses anonymous methods, so there's no way to apply the HostType("Moles") attribute. For example:
Because of = () =>
employeeList = EmployeeManager.GetUsersByRoles(rolesToLoad);
It should_return_a_list_of_employees = () =>
employeeList.ShouldNotBeNull();
I'm mocking the Roles provider called inside "GetUsersByRoles," and when I try to run this test via MSpec, I get the standard "Moles requires tests to be IN an instrumented process" error, with the instruction to add [HostType("Moles")] to my test method. Is there any workaround or other option available here?
Side note: I have downloaded MSMSpec.tt and modified it to include the attribute on the generated VSTests, but I'd like to be able to run the MSpec tests directly via its own runner or TestDriven.net so I can get the friendly output for BAs and business owners.
The workaround is to replace the anonymous method with one that is not. Moling Mspec is basically not possible.
Moles is not capable of detouring anonymous methods. The reason why is that the methods must be addressable, to be detoured. Anonymous methods are not implicitly addressable, because they are generated and referenced during runtime. Simply put, you can not call an anonymous method through the class, because it is, well... anonymous.
The Moles Manual states, "Moles can be used to detour any .NET method, including non-virtual and static methods in sealed types." Therefore, operating under the assumption that Moles uses reflection to identify class members is a safe bet. Anything that can not be called via delegate, Action, or Func, can not be moled.

Entity Framework - How to Modify generated base constructor (DBContext)

We have to deal with production and test connection strings in our environment. Database First Solution.
I have an extremely picky client that is not happy with the fact that you can create a partial class with a second constructor with a parameter, or inherit from the named Entities class with an empty parameter constructor.
He claims that a developer could unknowingly use the base constructor.
Is there any way to modify the generated constructor, or set an option so that the base constructor does not get generated, so we can write our own?
Thanks!
If you are using T4 template for context generation you can do whatever you want. For example:
Make your context sealed
Remove partial keywork from generated context class
Define constructor you want directly in the template
The only thing you need to do is modify the ModelName.Context.tt template.
Anyway your client should concentrate on business requirements and not on stupid assumption about coding.
He claims that a developer could unknowingly use the base constructor.
I claim that this can happen but it is not an issue if your application is correctly tested and if you make code review for new team members or junior developers.