Multiple interfaces binded to a single class - ninject

I have these 3 interfaces:
interface IA {}
interface IB {}
interface IC {}
Also, I've this other interface which inherits from IA, IB and IC:
interface II : IA, IB, IC {}
Then, I've also created a class CC inherits from II:
class CC : II {}
I've created these bindings:
this.Bind<IA>().To<CC>().InSingletonScope();
this.Bind<IB>().To<CC>().InSingletonScope();
this.Bind<IC>().To<CC>().InSingletonScope();
this.Bind<II>().To<CC>().InSingletonScope();
I don't know if, each time I've to request for a whichever interface, NInject kernel is going to give the same singleton instance of CC.
So, I mean:
IA ia = kernel.Get<IA>();
IB ib = kernel.Get<IB>();
ia is the same instance that ib?
How could I get this behavior?

As far as I know, this should work :
this.Bind<IA, IB, IC, II>().To<CC>().InSingletonScope();
The overload of Bind takes up to four type parameters.

Related

Creating public instance of private inner class in Kotlin

Why Kotlin doesn't allow creation of public instances of private inner classes unlike Java?
Works in Java:
public class Test {
public A a = new A();
private class A {
}
}
Doesn't work in Kotlin (A class has to be public):
class Test {
var a = A()
// ^
// 'public' property exposes its private type 'A'
private inner class A
}
I would assume because there isn't really a case where it seems like the right thing to do. Any code accessing the property a would not have access to its type. You couldn't assign it to a variable. Test.A myVar declaration outside of the Test class would error out. By not allowing it, the code will be forced to be more consistent. A better question would be why would Java allow it? Other languages, such as swift, have the same restriction.
https://kotlinlang.org/docs/reference/visibility-modifiers.html#classes-and-interfaces
states:
NOTE for Java users: outer class does not see private members of its inner classes in Kotlin.
For your usecase, you can use Nested Classes
In private inner classes you are only able to access members of your outer class.
I think the kotlin team implemented it that way, so that is possible to reduce the scope of members in private inner classes to be accessible only inside the inner class. I think this is not possible in Java.

marked and unmarked class in pharo 2.0 smalltalk

i need to implement the message markedSubclass in pharo that works just like subclass but i need the class that gets created to be somehow marked,for example i tried adding a unique instance variable to it after creating it but it's just not working,maybe i'm adding it to a wrong place.
the requirments are:
every subclass of this marked class should also be marked even if it
was created via subclass (not markedSubclass).
other than that a marked class should function just as a regular class should.
any help would be appreciated.
example:
User markedSubclass: #MarkedUser
User subClass: #UnmarkedUser
MarkedUser subclass: #MarkerUser2
i need to somehow know that MarkedUser and UnmarkedUser are both marked classes.
what i thought of lately is adding the method "isMarked" to Class class and this way all
the classes will have it, and each class will override it accordingly so if we write
User class isMarked.
it will return false but if we write:
MarkedUser class isMarked.
MarkedUser2 class isMarked.
it will return true for both.
but where can i add this method?and how can i make a class override the method in runtime?
Add a class method like the following to your User class:
markedSubclass: className
| subclass |
subclass := self subclass: className asSymbol.
subclass class compile: 'isMarked', String cr, String tab, ' ^ true'.
^ subclass
Then try in a workspace:
User markedSubclass: 'MyMarkedSubclass'
Add an #unmarkedSubclass: class method accordingly.
You could then override the general #subclass: method in your User class to set the same marker as the receiver.

ninject ioc with inherited base generic repository

I have a generic repository, abstract and concrete as below
public interface IGenericRepository<T> where T : class
public class GenericRepository<T> : IGenericRepository<T> where T : class
I then have an repository that inherits from this, again abstract and concrete
public interface ISkyDiveCentreRepository : IGenericRepository<DiveCentre>
public class SkyDiveCentreRepository : GenericRepository<DiveCentre>
In my ninject config I then have
kernel.Bind(typeof(IGenericRepository<>)).To(typeof(GenericRepository<>));
kernel.Bind<ISkyDiveCentreRepository>().To<SkyDiveCentreRepository>();
This is the first time I've tried to do this but am getting the error:
Error 2 The type 'UKSkyDiveCentres.DAL.imp.SkyDiveCentreRepository' cannot be used as type parameter 'TImplementation' in the generic type or method 'Ninject.Syntax.IBindingToSyntax<T1>.To<TImplementation>()'. There is no implicit reference conversion from 'SkyDiveCentres.DAL.imp.SkyDiveCentreRepository' to 'SkyDiveCentres.DAL.ISkyDiveCentreRepository'. C:SOMEPATH\UKSkyDiveCentres\App_Start\NinjectWebCommon.cs 56 13 SkyDiveCentres
Your SkyDiveCentreRepository inherits from GenericRepository<DiveCentre> and doesn't implement the ISkyDiveCentreRepository interface.
Simply explicitly implement it:
public class SkyDiveCentreRepository :
GenericRepository<DiveCentre>, ISkyDiveCentreRepository
// ^^^^^^^^^^^^^^^^^^^^^^^^ this
Without it.. you can't do simple things like this:
ISkyDiveCentreRepository repo = new SkyDiveCentreRepository();
If you can't do it.. Ninject can't either.

Access class (or object) parent in a correct way using OOP

I need know if this is inside the normal OOP behavior, or if not, what is the most common way to do it (without being specific to a language).
I had a class instanced on the main, called A , which inside had instanced the class B as a variable. When A calls methods inside B, B needs some methods from A for work.
For that, I must bypass the A reference itself via arguments or I must use always the tools specific for the language? OOP give some reference to this or it's out from their scope?
Thank you.
I had a class instanced on the main, called A , which inside had
instanced the class B as a variable. When A calls methods inside B, B
needs some methods from A for work.
The problem here is that, your class A knows too much - it knows how to instantiate another class with its arguments, therefore it becomes also responsible for a factory responsibility. Thus you end up breaking the Single-Responsibility Principle.
So, let's take a look at your current class hierarchy
class A
{
private b;
public void A()
{
b = new B();
}
}
a = new A();
Since you didn't inject an instance of B, this one also becomes bound to the class A.
Since B is tightly-coupled to A, It makes unit-testing very very hard (because you cannot inject a mocked instance of B) i.e you cannot test class A in isolation
It introduces another form of global state, since B comes from global scope
A proper way of doing this would be as (by adhering to the Dependency Injection):
class A
{
private b;
public void A(B bInstance)
{
b = bInstance;
}
}
b = new B();
a = new A(b);

How to make my class implement from IMyInterface<T>?

I want to port the following code from c# to c++/cli:
class MyClass : IEnumerable<int> { ... }
I've tried
class ref class MyClass : IEnumerable<int>
but it doesn't seem to be working.
C++ has multiple types of inheritance. Don't forget to specify it, e.g.:
ref class MyClass : public IEnumerable<int>
{ };
In C++/CLI, I frequently find myself spelling out the full namespace in interface implementations. E.g.:
ref class MyClass :
public MyCompany::MyProject::MyComponent::IMyInterface
{ };
If your class doesn't actually implement the provided interface, you'll also (of course) get an error. And you'll want to remove the ^ from the class declaration. You're inheriting from an interface, not from a GC Handle to an instance of that interface.
Assuming your code is exactly what you're trying to compile, you have an extra class there. It should be just ref class, and not class ref class. Also, don't forget to translate any C# using statements to C++ using namespace, i.e.: using namespace System::Collections::Generic.