Мое приложение MVC ASP.NET структурировано правильно?

Моя конфигурация Vim не делает этого. Вы могли бы попробовать python.vim сценарий, доступный из этой ссылки: http://www.vim.org/scripts/script.php?script_id=790

5
задан Brandon 28 September 2009 в 01:17
поделиться

2 ответа

_someRepositoryOrService.Add(id, classA, classB);

I would say you couple your repositories with presentation layer. This shouldn't be. Your repositories should only work with entities. Next, notice how your Add method

public void Add(int id, ClassA classA, ClassB classB)

breaks Separation of Concerns (SoC). It performs two tasks:

  1. map view data into entities
  2. save to repository

Obviously the first step should be done in presentation layer. Consider using model binders for this. It can also help you to solve the constructors problem, since your model binders can be made aware of the construction requirements.

Check also this excellent post by Jimmy Bogard (co-author of ASP.NET MVC In Action) about ViewModels. This might help you to automate mapping. It also suggest a reversed technique - make your controllers work with entities, not ViewModels! Custom action filters and model binders are really the key to eliminate routine that that don't really belong to controllers but rather an infrastructure detail between view and controller. For example, here's how I automate entities retrival. Here's how I see what controllers should do.

The goal here is to make controllers contentrate on managing business logic, putting aside all the technical details that do not belong to your business. It's techical constraints that you talk about in this question, and you let them leak into your code. But you can use MVC tools to move the to them infrastructure level.

UPDATE: No, repositories shouldn't handle form data, that's what I mean by "coupling with presentation". Yes, repositories are in the controller, but they don't work with form data. You can (not that you should) make form work with "repositories data" - i.e. entities - and that's what most examples do, e.g. NerdDinner - but not the other way. This is because of the general rule of thumb - higher layers can be coupled with lower ones (presentation coupled with repositories and entities), but never low level should be coupled to higher ones (entities depend on repositories, repositories depend on form model, etc).

The first step should be done in the repository, that's right - except that mapping from ClassX to EntityX does not belong to that step. It's mapping concern - an infrastructure. See for example this question about mapping, but generally if you have two layers (UI and repositories) they shouldn't care about mapping - a mapper service/helper should. Beside Jimmy's blog, you can also read ASP.NET MVC In Action or simply look at their CodeCampServer for how they do mapping with IEntityMapper interfaces passed to controller constructors (note that this is more manual and less-work approach that Jimmy Bogard's AutoMapper).

One more thing. Read about Domain Driven Design, look for articles, learn from them, but you don't have to follow everything. These are guidelines, not strict solutions. See if your project can handle that, see if you can handle that, and so on. Try to apply this techniques since they're generally the excellent and approved ways of doing development, but don't take them blindly - it's better to learn along the way than to apply something you don't understand.

4
ответ дан 14 December 2019 в 04:43
поделиться

Я бы сказал, что использование DTO и упаковка Entity Framework вашими собственными методами доступа к данным и бизнес-уровнем - отличный способ. Вы можете написать много кода, но это лучшая архитектура, чем притворяться, что код, созданный Entity Framework, является вашим бизнес-уровнем.

Эти проблемы на самом деле не обязательно связаны с ASP.NET MVC каким-либо образом. ASP.NET MVC в основном не дает никаких указаний о том, как выполнять доступ к вашей модели / данным, и большинство примеров и руководств для ASP.NET MVC не являются достойными производственными реализациями моделей, а на самом деле являются лишь минимальными примерами.

Похоже, что вы на правильном пути, продолжайте.

В конце концов, вы используете Entity Framework в основном как генератор кода, который не

4
ответ дан 14 December 2019 в 04:43
поделиться
Другие вопросы по тегам:

Похожие вопросы: