0

I've a doubt, I admit I'm a beginner in c# and software architecture. I'm working on an ASP.NET MVC4 + EF5 and I'm trying to structure my app using a three tier architecture.

In my BL I created those two viewmodels:

Word

namespace BL
{
  public class WordViewModel : IWord
  {
    [Required, ReadOnly(true)]
    public int WordId { get; set; }
    public string LanguageId { get; set; }
    public int VocabularyId { get; set; }

    public LanguageViewModel Language { get; set; }    
    public VocabularyViewModel Vocabulary { get; set; }    
  }
}

Vocabulary

namespace BL
{
  public class VocabularyViewModel : IVocabulary
  {
    [Required, ReadOnly(true)]
    public int VocabularyId { get; set; }
    public string Name { get; set; }
    public List<WordViewModel> Words { get; set; }
  }
}

Debugguing I noticed (and it's obvious) that there is a kind of "nested loop", I mean a Word contains a Vocabulary, that contains a list of words and each word contains a vocabulary, that contais a list of words ... and so on.

Following the tutorials, it seems to me that what I wrote is correct .. but I'm thinking at performance and memory consuming .. is it a problem? (but should be that my desing is wrong as well, I hope not)

Furthermore, but it's not a big issue, I'm using AutoMapper to map the object provided by my DAL layer (actually WordRepository and VocabularyRepository) to my viewmodel. It's easy to create nested mapping, but again, is it memory consuming?

Thank you .. I know I'm a newbie and I'm trying to do my best :)

Davide
  • 1,305
  • 3
  • 16
  • 36

1 Answers1

2

You should try to avoid bi-directional relations in your models. Usually it not necessary and they need a lot of additional code to be maintained. For example, I don't see any code making sure that when a word is added to a vocabulary, the reference to the vocabulary is set on the word model. This way the model could easily get in an invalid state.

In this case I don't think you need to have a reference to the vocabulary in the word model. The vocabulary could be retrieved by the repository, and then you would have access to the words through the List property (EF will load them for you).

Also, you seem to confuse viewmodels and models. Models are a representation of the problem domain, these models usually get saved to the database. Viewmodels are specially tailored classes which contain all information for a specific view, they usually live in the web layer itself because they are closely coupled to the view. Viewmodels could contain information retrieved from multiple model objects. Automapper is often used to create viewmodels from model objects.

Repositories are classes which retrieve and store models. They look like this:

public class VocabularyRepository
{
  public Vocabulary Get(int id)
  {
    ... 
  }

  public void Save(Vocabulary vocabulary)
  {
    ...
  }
}

There's a lot to learn here, hope this will put you in the right direction. Check this question for more info on asp.net mvc architecture: https://softwareengineering.stackexchange.com/questions/30348/best-practices-for-mvc-architecture

Community
  • 1
  • 1
Robin van der Knaap
  • 4,060
  • 2
  • 33
  • 48