I'm a PHP programmer that has applied for an ASP.NET job. They gave me an assignament to make an application (of my choice) that implements a multi-tier arhitecuture.
I have alot of dillemas. As I understand, multi-tier is a concept that doesn't have a universal form and everyone should decide what is best for them. However, they advised me that the presentation layer should not have any refferences to the data access layer, which makes sense. But...
I created a new project with Add new project (that has controllers and views) who is a presentation layer. Now, in the presentation layer, there is also a class library that uses Ninject to inject dependencies for the entire application called NinjectIoC. NinjectIoC has to have a refference to the presentation layer project in order to inject dependencies directly in the controller as an argument. It also has to have refferences to all the other layers (DataAcessLayer, BusinessLayer etc...) order to bind them to their dependencies.
The main problem is that presentation layer project has to also have a refference to the NinjectIoC to create the StandardKernel inside Global.asax which creates a cirucullar dependency and is not permitted.
The only solution is to add a refference to the presentation layer project of all the layers (including DataAccessLayer) which, as I understand, is a bad thing. But, that is the only way to bind all the interfaces of all the layers and execute it inside Global.asax.
Am I thinking wrong?
EDIT:
NinjectIoC has Ninject installed and has a refernce to all the layers in order to bind them across the application. It has to have a reference to the UI in order to be called in Global.asax
UI has to have a reference to NinjectIoC so it can call it in Global.asax for controller binding.
I tried to create an intermediary class library that has a reference to NinjectIoC. That library is referenced in the UI. The problem is that that also creates a circular dependency beacuse NinjectIoC has to have a reference of the UI in order to bind the controllers.
Multi-tier can simply mean that there is a DAL, a BL, and a UI Layer. And the requirement to "not reference the DAL in the UI Layer" can simply mean that your UI layer (MVC4 app) can only reference the BL. This is simple to achieve, for example like this:
An UI Project (MVC4)
An Entities project (Class Library): define here the entities used, and reference it from all the necessary layers (this helps avoiding circular references)
A BL project (Class Library): this must reference the Entities, and the DAL project
A DAL project (Class Library): this references the Entities project
This is the classical, most simple, multi-layered project.
In the final compilation, of course, the indirect dependencies will include the UI project and the 3 libraries, but you don't need to add a reference to the DAL in the UI.
Another different question is using the IoC pattern. In this case, to solve the circular references problem, I recommend you to define separately projects of "Interfaces" and projects of "Implementations" for each layer. Something similar to the previous structurem, but with this changes:
An UI Project
An Entities project
A BL Interfaces project
A project that implements the BL interfaces
A DAL Interfaces project
A project that implements the DAL Interfaces
You need to define which is the main project. It's usually the UI project. This project is the one that will have all the dependencies.
In this case, the UI project depends directly on the BL Interfaces project. And the BL Interfaces will depend on the DAL Interfaces. In a few words, your UI project will only have direct dependencies with the BL Interfaces.
The question is that, when you try to run the code, it will need to solve this dependencies, i.e. find the implementation of the interfaces, and there dependencies. This is the "compositio root" of your application, and it's where you need to register the dependencies. I.e, this is the place where you need to define which concrete implementation will be use for each interface. And, if this implementation depends on other interfaces, you have also to define their implementations. Depending on the framework (I don't know if you can do it with NInject) you can do this dynamically, without the need to add references to the implementation projects. However, even if you have to include references to all the other implementation and interfaces projects you'll have not circular dependencies, and your UI will have not dependencies on the DAL or implementation layers. You only need them for registering them in the IoC Container, which is a very different question. (If you use constructor injection, you get the maximum possible decopuling using this project structure).
Keeping one or several separated entities projects allows you to avoid circular references, and direct dependencies between project. For example, if you defined the entities in your DAL project, nad you use them in your UI project you'd need to add a reference to the DAL project. This problem dissapears if the entities are declared in a separate project.
The pure IoC, like the "Onion Architecture" goes far beyond this by defining the entities and necessary interfaces of dependencies in the main project (UI project), then implementing these dependencies in other projects, and solve them dynamically, to avoid circular references. I.e. all the other projects depend directly or indirectly on the UI project, and not the other way round. In this case you need to solve the dependencies dynamically to avoid the circular references.
As you can see there are many options, and I've shown you several examples of working solutions.
Related
At this moment I have a dynamic abstract factory with a singleton implementation of the class Factory where I store all the factories. Every factory has it's own types which there can be created instances out.
Those factories also got the RegisterType methode which I have created in order to register types from outside this project (DLL).
I want to register a type from outside the project. But in order to do that, ProjectA and projectB has to know information about each other.
So my question is, how can projectA and projectB know about each other without getting this error "Circular Dependency on References".
This blog post from Steve Smith directly addresses your question I believe.
Re-reading your question, the link I posted is not relevant, as you are dealing with 2 projects.
With most projects (web, desktop, even most apis) you typically have a project that is your entry point, your other project should contain dependent code or infrastructure code, there should be no bi-directional couple of the 2 (or more) projects.
It might be appropriate to use an IoC framework and register your dependencies, you would do this in lets say ProjectA, and provide to your factories types from ProjectB.
Be sure to abstract by using interfaces when passing these factories (and other types) into ProjectB where the implementation resides from ProjectA, then these interfaces can sit in ProjectB and not need any backward dependency.
I know this question might seem to be answered before, but I feel that the answer varies from case to case, so after reading several posts, I'm not sure in my case which is the best for my architecture.
I have a Component Library that has a Data Model and basic functionality that should be available to any application implementing this component.
I have a boundary for this component which has an interface IReader to load and process files from the disk and IDataMapper to provide Database access and CRUD operations.
a few other interfaces for specific functionality like IObjectComparison to compare objects, IXMLSerialization fro XML serialization.
I'm not sure where to store the definition of these interfaces.
The options are:
1)- Within the core Library, then when I write the implementations I will have to include the implementation libraries within this core component with I'd like to maintain decopled from the implementations.
2)- In a separate library project (Assembly). All interfaces there and included to the core component and included by the implementation libraries.
3) - In the implementation Libraries, then the core component will have to include the implementation libraries.
The only case where it seems reasonable decoupled is if I put all interfaces in a separate assembly library where Core component includes and any implementations I might need.
What do you guys think are Pros/Cons of the best option?
All I want to achieve is a decoupled architecture.
So when I do
Constructor:
CoreComponent(IReader Reader, IDataMapper Mapper)
new CoreComponent(WindowsReader, SQLServerMapper)
and don't have to include WindowsReader or SQLServerMapper into the Core Component
Cheers.
I would go for option 1 - Core Library as it is accordance with how we do in DDD. In DDD we used to put IRepository interfaces in Domain Layer instead of DAL or any other such layer.
DIP says the higher level component would own the interface, as Wikipedia says...
where interfaces defining the behavior/services required by the high-level component are owned by, and exist within the high-level component's package.
This is most common practice but not a strict rule.
Option 2 is fine but you need to refer two DLLs in other projects but with option 1 only one reference is needed. Option 3 is not appropriate.
Hope it would help. Thanks.
I have used Ninject in a small project, but am now converting a larger web app to mvc and need help with using Ninject. In the new solution, I have the mvc site and have split some of the functionality out into separate class projects, for example, my ReportGenerator.
I would like to use Ninject within ReportGenerator to resolve the dependencies it has, but I do NOT want the MVC project to know about the internal workings of ReportGenerator. So where would I create the bindings/kernel?
I've looked at other questions such as: Referencing Ninject in class library in ASP.NET MVC 3 application but that answer seems to state that the bindings are setup in the MVC project which I don't want.
Can someone point me to sample code of how to configure/run Ninject inside a class that's referenced by an MVC project that will also use Ninject?
You should register and resolve all your components in the Composition Root. This is unrelated to Ninject, and this advice will hold for all DI containers.
The Composition Root is the startup path of the application where you wire everything together. You would typically place this composition root inside your startup project; in your case your MVC project.
You shouldn't typically be worried about this, because since your MVC assembly itself depends on the details of another assembly, doesn't mean that your UI logic (your controllers for instance) depend on those details. As you know the controllers should simply depend on abstractions. Don't confuse your logical architecture (separation of layers) with the physical architecture (how code is separated on disk during deployment). The Composition Root is logically separated from your MVC end project, although it could be located in the same assembly.
The Composition Root is typically the code path of your application that the runtime will run first, and it must know everything about everyone. In a console application the your startup project would typically be really, really thin, and contain not much but just the Composition Root. Because of the architecture of ASP.NET applications, this is typically much harder to achieve, for instance because the startup project is the web application and it contains all kinds of types (controllers, views, etc) that need to be resolved. Because of this, it is quite usual for web applications to have the Composition Root integrated into the web project itself. Again, this is not something to worry about, because the mere fact doesn't make your code more tightly coupled.
Things gets different however, when you have a business layer that gets reused by multiple end applications (both a WCF web service and MVC app for instance). To prevent code duplication you would move the shared registrations out of the MVC and WCF composition root and place it in a special 'bootstrapper' assembly that sits on top of the business layer (and all layers below). This can be as simple as having a static class with a static method that takes in an existing Kernel instance and makes the business related registrations (most DI frameworks have features for this, but they are rather useless in most of the case and a static public method will do just fine). Each Composition Root can create their own Kernel instance, make registrations, pass the instance on to the BL bootstrapper, do -perhaps- some more registrations after that, and store the kernel for use by the application.
But even with multiple end applications, they will still each contain their own specific wiring (since every application is different) and thus have their own Composition Root.
I am currently developing a Windows service and I am am looking to use MEF to compose all of the services components at run time. The data access module (project) is using Entity Framework 4.1 and a Domain Service Class to perform CRUD operations on the entity model.
The problem I have is how to share the models outside the DAL project when composing the DAL into the worker class in the main project.
For exmaple this is one of the methods in the DAL contract interface
Function GetInspectionFaults() As IQueryable(Of InspectionFault)
This interface is currently in the DAL project (not the shared/referenced project containing the other contract interfaces) as it needs references to the entity model for the POCO types.
How do I shared these POCO types?
Phil
Although it might be a bit more work, you might want to consider having a separate set of models (view models if you will) that you have in a shared assembly (maybe a common/contracts assembly). This would enable your parts to utilise a common set of types which are not explicitly dependent on your DAL.
In regards to separation of concerns, I wouldn't recommend exporting your DAL directly, but provide an abstraction to it via something like the Repository pattern. A repository will handle the communication with the DAL and mapping from your domain to your view models.
You can export and import your repository wherever you need it, which means your parts are not dependent on a specific data source. This makes your code more robust, and more testable.
Due do LinqToSql not being appropriate for Many To Many relationships I am in the process of deciding to move to NHibernate (Fluent NHibernate) unless convinced otherwise...
Project Structure: UI (Mvc2 app with Ninject wiring up all services to controllers, and repositories to services), DomainServiceLayer (all util, helpers, services, domain model etc) and my Repository Layer for persistence. I have a another project call Model which basically exposes the entities, which all projects reference.
Basically I am creating my mappings within the Repository Layer with references to NHIbernate and Fluent NHIibernate, I hope to expose the interfaces to the Domain Service for querying and persisting data. How do I wire up the iSession, where do I wire it up? Any example code, what project should I put it in? Ideally I want to keep this within the Repository Layer... Is it worth learning NHibernate and going through all this?
I recommend looking at the blog posts of Bob. He describes in detail how to use the repository pattern in Ninject using NHibernate. I planned adding an example in the near future to the sample application comming with the MVC exptension as this question comes up again and again.
http://blog.bobcravens.com/2010/06/the-repository-pattern-with-linq-to-fluent-nhibernate-and-mysql/
http://blog.bobcravens.com/2010/07/using-nhibernate-in-asp-net-mvc/
http://blog.bobcravens.com/2010/09/the-repository-pattern-part-2/
Typically I have an NHibernateSessionFactory which is a singleton that has an OpenSession method and I bind ISession typically like this.
Bind<ISession>().ToMethod(context =>
NHibernateSessionFactory.Instance.OpenSession()).InRequestScope();
This method just calls through to ISessionFactory.OpenSession
You can put this into a NinjectModule in your repository layer, which your app can load when it creates the Kernel.
I do the configuration in the Application Layer (i.e. the top layer) as the configuration differs between applications. But it can be useful to break out some of the configuration into classes stored in the Repository Layer.
I open and close the session with an HttpModule.