How do interfaces solve circular dependencies? - circular-dependency

If you search for how to resolve circular dependencies, the answer is almost always "use interfaces". I know how this technique can be used to make a circular dependency work, but I do not understand how that is solving a circular dependency.
Say I have 2 classes View and Presenter which reference each other. Now I apply the "solution" and create the interfaces IView and IPresenter. View does not reference Presenter anymore, but IPresenter; Presenter references IView instead of View.
I set up View, which needs an IPresenter.
To get an implementation of IPresenter I need to set up Presenter.
To set up Presenter, I need an IView.
To get an implementation of IView, I need to set up View.
The circle has gotten bigger, but it is still there. View and Presenter still depend on each other, just not directly. However, every answer I have seen is absolutely sure that the circular dependency is now resolved. What is my misunderstanding here?

Circular dependencies can be resolved using DI but only using "delayed" or "property" injection, not "constructor" injection. You would need to construct the objects first, then set properties on them to inject the dependencies. A DI container that supports property injection could abstract this detail away.

These circular dependencies are not being solved.
The standard answer in these situations is to assign the dependency after construction. This solves the problem that these circular dependencies create, but not the circular dependencies themselves.
It's probably mostly a language issue where people just say "I solved the circular dependency between X and Y" instead of "I solved the issue we had because of the circular dependency between X and Y".

Related

JSON Circular Structure in Entity Framework and Breeze

I have a horribly nested Entity Framework structure. A Schedule holds multiple defaults and multiple overrides. Each default/override has a reference back the schedule and a "Type". The Type has a reference back to any defaults or overrides it belongs to. It's messy, but I think it's probably the only way I can do what's required.
This data ends up in a browser in the form of Breeze entities. Before I can process these when saving them back at the server, I have to turn them back into JSON, which unsurprisingly trips the dreaded "Uncaught TypeError: Converting circular structure to JSON".
Now there are a number of perfectly good scripts for removing these circular structures. But all of them seem to replace the circular references with some sort of placeholder so they can be re-created as objects. But of course Entity Framework doesn't recognise these, so can't work with them.
I'm at a loss as to what to do at this point. Simply removing the circular references rather than replacing them doesn't seem to help as it can result in structures shorn of important data.
I've also tried changing my EF queries to only get back specifically the data required, but it insists on giving me absolutely everything, even though Lazy Loading is set to false and I have no .Include statements in my queries. But I feel this is solving the wrong problem, since we're bound to want to deal with complex data at some point.
Is there any other way round this?
EDIT: I've solved this temporarily by investigating the object and removing the circular properties by name. But I'd still like a generic solution if at all possible.
Seems like you are after serialization mode. find out serialization mode in properties in your designer screen and set it to unidirectional. this will solve your serialization issue.
Hope that helps!!!
Not sure I understand the question. You should never experience any kind of circularity issue, regardless of the complexity of your model, with a Breeze SaveChanges call. (Breeze internally unwraps all entities and any circularities before serializing). If you are seeing something different then this would be a bug. Is this the case?

How can I extend a bundle more than once?

In my Symfony2 app I'm having a very basic bundle named AnimalsBundle() with a very basic entity.
I can successfully extend this bundle by creating a new bundle MammalsBundle() via Bundle Inheritance. However, it is not possible to register one further bundle InsectsBundle() that also extends the AnimalsBundle(). Whenever I'm trying to do this, Symfony throws a
[LogicException]
Bundle "AnimalsTextBundle" is directly extended by two bundles "MammalsBundle" and "InsectsBundle".
So out of the box it's obviously not allowed. First of all, I'm not really sure why this is not allowed and - most important - how can I solve this?
I know it's been more than year now, but I just came across your question and the answer maybe useful to someone anyway..
Symfony doesn't allow a bundle to be extended directly by more than one bundle, simply because if two bundles are overriding the same files, it wouldn't be possible to determine what bundle should be used. However you can achieve what you want by doing the following :
AnimalsBundle <|---- MammalsBundle <|----- InsectsBundle
This way InsectsBundle indirectly has AnimalBundle as a parent and can override files from it.
I know, that this has been a log time since the question has been asked, but for those in need of an answer I'll suggest the following:
1) Try to avoid bundle inheritance except the cases you are 100% positive that you need to.
2) Given the example in question, the better setup will look smth like this: you have a CreatureBundle, which consists mostly of abstract classes and interfaces. Make each descendant bundle depend on CreatureBundle and implement each Creature specific code with those abstract classes and interfaces in CreatureBundle.
Based on personal experience I can tell that managing dependencies is much easier task than managing inheritance issues in case something goes wrong. If you'll ever need to alter the ancestor bundle's logic, you'll save yourself lot time by not having to dig through inherited code and alter the same logic in every descendant bundle.
Edit: Although my suggestions might lead to tighter coupling and basically contradicts latest Symfony's 'best practices' guide (which states that 'single bundle per app' is a best practice), in the end you'll realize that this approach eventually makes code maintenance easier.
I can't think of a use case where you could possibly need to do this. Bundles are meant to be almost like standalone applications. You have the dependency injection container at your disposal if you need resources from another bundle.
Perhaps you should re-think your project structure.

How to determine if a component is resolvable from an IHandlersFilter implementation

I am trying to write a Castle Windsor v3 IHandlersFilter implementation that will filter out handlers that cannot be resolved. This is in an effort to optionally restore the old (pre-castle 3) behavior of ResolveAll (How to revert to old CollectionResolver behavior in Castle 3?)
My question is, from the SelectHandlers method, what is the best way (if any) to determine which of the input IHandlers are resolvable? I have experimented with the IHandler.CanResolve method, but it takes parameters that are a bit more advanced than I typically deal with, so I'm not sure how to use the method properly.
You can check handler.CurrentState == HandlerState.Valid
However be aware this is based on what Windsor can determine statically, therefore the answer may not always be 100% accurate (for example some components may have dynamic dependencies, so they will appear as unresolvable here, yet you'll be able to successfully resolve them when you try).

What is the best practice when you have read-only properties and cannot use the objectdatasource?

I have started a web forms project using nHibernate and objectdatasources; however, I've learned that there are some limitations that I understand but do not know to handle. These limitations include 1) objectdatasources require parameterless constructors and 2) properties of the business object cannot be read-only.
The problem I'm having is that a class in my business layer sets a property that should never change and I'd like to make sure this never happens. I'd like to set that property when the object is created, but make the property read-only so people cannot set it later. I'm not sure it's relevant to the issue, but I am using a repository class as well.
I guess I could simply make the property read/write, but I think that the business layer should enforce the rule of never changing the property. Is there a way to use objectdatasources without relaxing my business rule that a property must be read-only. If I cannot use objectdatasources, is there another best practice that does not include copying the logic of creating objects, setting their properties based on form values and then saving?
Thanks for any insight on this issue. I'm sure people have come across this in the past so I'm just looking for some direction in how to best use data sources for a web forms site. Also, any references to books or articles related to handling not so typical issues would be helpful. It seems like everything I've been looking at has you building CRUD screens...
Sean
You can map the nHibernate to fields. This may help NHibernate : map to fields or properties?
This way you should be able to have RO properties.

Getting JSON from Jersey with circular dependencies

I am writing a service that uses Jersey and JAXB. My classes are annotated with #XMLRootElement, #XMLElement, etc. I have a circular dependency between two classes, so I have annotated the circular dependent property with #XMLTransient. So when I call my service I get xml as the default, which works perfectly. However, when I try using JSON, I get repeated lines like:
{"name":"dere","entries":[{"points":0,"wins":0,"losses":0,"ties":0,"leaderboard":{"name":"dere","entries":[{"points":0,"wins":0,"losses":0,"ties":0,"leaderboard":{"name":"dere","entries":[{"points":0,"wins":0,"losses":0,"ties":0,"leaderboard":{"name":"dere","entries":[{"points":0,"wins":0,"losses":0,"ties":0,"leaderboard":{"name":"dere"," ... etc.
So it seems there is a problem with circular dependencies when I am using JSON. I would like to avoid the circular dependent item from showing up in the JSON output, like it is done in XML (because of the #XMLTransient annotation).
Can anyone provide any insight on how I would be able to achieve this?
Use #JsonIgnore instead of #XmlTransient to break the circular dependency.
I had a similar problem as you and this did the trick for me.
It's probably worth checking out Kris Zyp's JSON Referencing proposal. It was invented specifically to handle multiple references and circular references in JSON data.
(Note: Despite the article title, Dojo isn't required. The original proposal was on json.com, but that site is inaccessible to me at the moment.)
How you would implement this technique in Jersey is, unfortunately, an exercise left to the reader.