Does dependency exist if A is only used in a method of B? - data-modeling

I have a class Idea and class Generator
In Generator's run() method, a list of Idea will be constructed and returned as return value of run()
I only know that if Generator has a member which is a list of Idea, their dependency relationship will exist certainly. But how about the situation I mentioned above?

When looking at dependencies, ask yourself what would happen if you didn't have that particular class. If it wouldn't run/compile, then there is a dependency. So yes, Generator has a dependency on Idea.

Dependencies are displayed at classifier level in the class diagram. I mean that if two methods call each other then there is a dependency. You have different kinds of dependencies which are displayed with a dependency link and a stereotype such as << call >> etc..
Below is an example:
(source: forum-omondo.com)

The situation you are describing is a Usage dependency following the create stereotype. It is indeed a classic example of dependency, as noted here:
Dependency relationships in UML

Related

AutoFixture AutoDataAttribute Customization Beyond Derived Attribute

I am using the AutoDataAttribute class within AutoFixture.Xunit2 in a lot of projects. The recommended approach to add your own customizations seems to be a derived attribute like the following (note I am using FakeItEasy):
public class AutoFakeItEasyDataAttribute : AutoDataAttribute
{
public AutoFakeItEasyDataAttribute()
: base(() => new Fixture().Customize(new DomainCustomization()))
{
}
}
In an effort to reduce code copying/pasting, I wanted to abstract this derived attribute to a package we could consume in our projects. However, despite attempts utilizing dependency injection with this library and running into CLR issues with the DataAttribute not able to take anything beyond basic "primitives", I have ran into the proverbial "brick-wall". Obviously constructor injection doesn't seem to work here nor property injection to my knowledge (although unlikely that matters as the property isn't allocated until after the constructor call anyway).
The bottom line, I am looking for a way to include this derived attribute into a package but in a way where the domains can be customized for each individual project's needs?
I don't think what you're trying to achieve is possible due to how attributes work in C#. As you mentioned yourself you cannot pass into the attributes but a small set of primitive values, and in xUnit 2 data attributes don't have access to the test class instance, so you can not inject instances via reflection.
You could theoretically inject the IFixture instance into the test class using the library you mentioned (which I think is a horrible practice, that promotes sloppier tests), but then you'd have to give up the decorator notation of AutoFixture and use the declarative notation, to create your test data.

How obtain list of qooxdoo sublasses programmatically

I am working on a ClojureScript wrapper for qx.mobile and would like to programmatically build a cljs type hierarchy mirroring the qx class hierarchy.
Is there a way to get all the subclasses of a qooxdoo class?
How about a programmatic way to query the superclass of a class?
I am already putting qx.Class.getProperties to good use.
Thx, kt
The programmatic way of getting the superclass of a given class is documented at http://demo.qooxdoo.org/current/apiviewer/#qx.Class
<classname>.superclass
or getting the name of the superclass as a string
<classname>.superclass.classname
which means that e.g.
qx.ui.core.Widget.superclass.classname
will return the string "qx.ui.core.LayoutItem".
Regarding the programmatic way to retreive all subclasses of a class:
This is currently not possible without iterating the whole class hierarchy/tree and testing the objects against being subclasses of the given class.
We discussed at https://gitter.im/qooxdoo/qooxdoo that it maybe would be usefull to create an array for each class holding the subclasses. This could be added to the code of the private method __createClass in qx.Class.
We would like to encourage everyone who needs this (or other) functionalities to join us on https://github.com/qooxdoo/qooxdoo/ and help extending qooxdoo by creating a pull requests. Thank you.
After digging arround a bit in qx.Class we decided to implement a method qx.Class.getSubclasses which returns a hash object with all subclasses of a given class.
var subclasses = qx.Class.getSubclasses(qx.ui.core.Widget);
gets all subclasses of qx.ui.core.Widget.
Landed in qooxdoo master with commit https://github.com/qooxdoo/qooxdoo/pull/9037

Sencha Touch 2.3.x - is there a universal method to call a function from another class?

I'm stuck with a very simple issue - calling functions between classes. Say I have a function (renderMap) in one of my already defined class: App.ux.MyClass (I also added this class to 'requires' in app.js). How to call the 'renderMap' function from other classes?
I tried App.ux.MyClass.renderMap() but I got 'undefined is not a function'.
I would solve the problem by creating a mixin that would contain all functions shared by many classes. See the mixins docs for details.
Then you would just call
this.renderMap()
in any class that uses that mixin.
I think it's problem in application architecture.
You can to use DI (it will be best choice), but, if you cannot, try to create Singleton or ServiceLocator patterns (yes, I know they are anti-patterns).
In ExtJS 4.x and Sencha Touch 2.x Singleton can be created via statics definition in class. Read more: http://docs-origin.sencha.com/touch/2.3.2/#!/api/Ext.Class-cfg-statics
Then, you just can to call method like App.ux.MyClass.methodName().

Should a factory method of a custom container return the newly created instance?

I have a custom collection Doohickeys indexed by keys. This collection has a factory method createDoohickey(key) and an accessor Doohickey(key). Should createDoohickey(key) return the new object or should it return void?
In the first case, I would use it like this
myDoohickey = doohickeys.createDoohickey(key);
doStuff(myDoohickey);
in the other case like this
doohickeys.createDoohickey(key);
doStuff(doohickeys(key));
Which one would you consider preferable, and why?
EDIT I believe I have learned quite a bit over the years and that the answer I accepted is actually not really the best one.
I'm not sure the container should have a factory method to create the object it contains - good separation of concerns would be to separate object creation from the container.
Therefore, I think you should have something like this:
Doohickey myDookickey = new Doohickey();
doohickys.Add(key, myDookickey);
doStuff(myDookickey); // or
doStuff(doohickeys(key));
You are then free to use a separate object factory or instantiate Doohickey directly. This makes it easier to unit test the container with mock Doohickey objects.
If you must have the factory method, I would return the Doohickey, because it is probably cheaper to ignore the return value (assuming a reference/pointer is returned) when it is not needed than to use the index to retrieve it when it is.
Yes, a Factory should return the newly created instance.
However like Alex pointed out you could seperate the Repository and the Factory. I would most likely design the factory to have a reference to the repository. I don't think it would be the end of the world of you combined the two into one class just as long as the factory method actually returns the newly created instance.
The example above embeds the factory method in the container class. I agree with willcodejavaforfood, and this may or may not be a good idea over the longer term. The issue is whether the reduced coupling of splitting the factory into its own class is worth the extra cost.
As far as why a factory method should return the newly created instance, it is preferable to decouple creation from containment. Some users of the class might want one but not the other. Also, when you couple creation and containment you can no longer (unit) test creation without also testing containment.
This answer is from a colleague of mine. He suggested to have createDoohickey(key) indeed return the Doohickey; and, additionally, to call it getDoohickey(key) instead.
EDIT I believe after what I have learnt since that this answer is not necessarily the best one any more.

Is using the RequestHandlerComponent in a model possible?

I'm new to PHP and decided to use the cakePHP framework to help me get started.
I can't figure out one thing though, I want to call methods on the RequestHandlerComponent class to update a users last used IP address and other information, I figured the best place to put this would be in the beforeSave() method on the User model.
I can't figure out how to call the getClientIP method though.
The normal code that would otherwise go in the controller doesn't work. Is there another way to call this class if you're in the model and not the controller?
Class Level:
var $components = array('RequestHandler');
And in the function:
$this->data['User']['lastActiveIP'] = $this->RequestHandler->getClientIP();
Gives:
Undefined property: User::$RequestHandler
Call to a member function getClientIP() on a non-object
Components, by design, aren't available to models (without bypassing MVC convention - which you can do, of course). If you chose to force it to be available, look into ClassRegistry::init(). A better solution, I think, would be to employ the RequestHandler component in your controller (where it's meant to be used), set the lastActiveIp value in the controller (exactly as you've shown in your own example code) and pass the entire data array along to the model.
Now your component is being used where it should be and the model gets to remain ignorant about where it gets its data. At the risk of oversimplification, all the model should know is what to do with the data once it arrives; let the controller worry about collecting and packaging the data.
In addition to Rob's answer, maybe it's enough to put a bit of code together yourself that uses the general env('REMOTE_ADDR') or similar variables. Look at the RequestHandler code, it's not doing anything terrifically complicated.
You may even be able to call the component statically, which is slightly better than instantiating it in the model (still in violation of MVC though). Untested, but should work:
App::import('Component', 'RequestHandler');
RequestHandlerComponent::getClientIp();

Resources