Search code examples
c#asp.net-mvcarchitecturetelerik-open-access

Need Architectural Advice for Large scale .Net MVC Project


I will try to explain in as much detail as possible. There may be similar questions here on SO and I've gone through all of those but none of those have what I needed.

So, I'm starting out with a large scale C# MVC5 based Web Project and I want to organize everything in as much decoupled way as possible. For the database part I'm going to use Data Access ORM from Telerik (Previously known as Open Access) because I will be using MySQL for my project.

So far I have organized everything as below. I have defined solution level folders to divide the projects because I think there may be a possibility to have more projects in one layer in future.

**Solution**: td
- Business (Folder)
-- td.core (Project) (Contains Services and ViewModels)
-- td.interfaces (Project)
- Data (Folder)
-- td.data (Project) (Contains Database Models i.e. Telerik, Repository, Context Factory and Unit of Work class)
- Presentation (Folder)
-- td.ui (Project) (MVC5 Project, also Implemented IoC here)
- Shared (Folder)
-- td.common (Project)

Generally, when you bind models in your MVC project, if you have just one project in your solution, it works pretty easily without an issue.

i.e. in a MVC Controller

var obj = new TempClass();
return View(obj.getAllUsers());

and then in the corresponding view you use this at the top

@model (model type here)

When I separate all these layers in their own projects as mentioned above. The data layer would be the one directly communicating with the database hence I will generate the Telerik Data Access rlinq schema in my Data node where it will also generate the classes for the tables in my database (Default config)

Now, from the setup above, from the controller I'm supposed to call the Business layer to fetch the data and which will communicate with the Data node.

The question is that in the controller and in the view I will need the data types / references of the model I'm binding to. So, should I keep my automatically generated classes still in the Data node or can I move ONLY the generated classes to the Shared Node and then use those for the binding in the Controller/View? Which one is going to be a good practice? as I don't want to reference the Data nodes directly in the controller otherwise there is no point in separating everything like above.

Another quick question. I would be integrating so many third party APIs via REST/SOAP. In which layer should these best fit?

If anyone has any other Architectural suggestion or something that I'm missing here, please do suggest.

Thanks in advance everyone.

UPDATE!!!

Please see my updated architecture above.

Here's what I did so far.

  • I have added Repositories, Services and IoC.
  • In my Global.asax, I'm initializing the IoC which configures the Services etc for me.
  • My controller has an overloaded constructor now having the service from the business layer as the parameter.
  • Controller calls the service to get the data and the service calls the repository for it.
  • I have followed the generic repository path instead of creating repositories manually for each type
  • For 3rd party APIs, I will use the data layer and business later won't know where the data came from. It just needs to ask what it needs.
  • All this was made easier with the help of a dedicated Interfaces project which is being referenced from both the Business and Data layers when needed. Because as both want to implement abc interface I cannot declare it in either Business or Data layer since there would be circular referencing then which prevents me to reference both (Business/Data) projects to each other.

So, with the help of above changes, I can easily do what I want now and Everything is working perfectly as I want. Now the last question I have is

Is there any flaw in this architecture?


Solution

  • For a domain-centric architecture where it's easy to add another type of UI or change persistence technology and where business classes are easily testable in isolation, here's what I'd do :

    • Business layer with no dependencies. Defines business types and operations.

    • Data layer with data access objects/repositories that map from database to business types. You can also put your third party API accessors and adapters here. Depends on Business layer where repository interfaces are declared.

    • No Shared layer. Business types are the basic "currency" that flows through your system.

    • UI layer depending on the data access interfaces declared in the Business, but not on the Data layer. To decouple UI further, you can introduce an additional UI-agnostic Application layer.

    You can read more about this at Onion Architecture or Hexagonal Architecture.

    As it is, your architecture is pretty much data-driven (or Telerik Data driven) since the business and UI layers are tightly coupled to the Telerik schema. There's nothing wrong with that, but as I said in my comment, it enables other things such as quick development from an existing database schema, over full domain decoupling, framework agnosticism and testability.

    Whether your Telerik generated model lives in the Data or Shared module makes little difference in that scenario IMO. It is still the reference model throughout your application and your controllers will be coupled to it anyway. The only thing I would advise against is moving the generated files manually - if it can be automated all the way, definitely do it or don't move the files at all.