3
\$\begingroup\$

I have the following model:

namespace Site.Models.Country
{
    public class Country
    {
        public string CountryCode { get; set; }
        public string CountryName { get; set; }
        public string CountryUrl { get; set; }
    }
}

In a separate model folder for a different view and controller, I have a different model like this:

namespace Site.Models.Directory
{
    public class DirectoryProfileView
    {
        public List<Country> Countries { get; set; }

        public DirectoryProfileView()
        {
            this.Countries = new List<Country>(Country.GetCountryRegions());
        }
    }
}

Is it correct for me to use the Country model from a different model ? I tried adding using Site.Models.Country;

However for the following to work:

public List<Country> Countries { get; set; }

I need to call by

public List<Country.Country> Countries { get; set; }

My two questions are:

  1. Is this correct? I don't really want to be creating another model exactly like my first Country model.

  2. Also, any feedback on my naming conventions would be appreciated too.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I think this is a valid question, but perhaps best suited for migration to the codereview stack exchange site. \$\endgroup\$ – SpaceBison Aug 28 '13 at 12:25
3
\$\begingroup\$

I think you need to read up a little on namespaces. Both of your namespaces don't make sense unless you will have multiple types of countries and directories.

Your namespace should be if you will have multiple countries

namespace Site.Models.Countries

otherwise it should be

namespace Site.Models

In regards to naming your view models, I would think on how you will use these models in future. If you are likely to have a page for creating, updating, removing and viewing the country, then you might well have the following view models:

  • DisplayCountry
  • UpdateCountry
  • RemoveCountry
  • SaveCountry

Some people choose to reuse single country model, but I've seen this cause more trouble than benefit. For example, in the RemoveCountry model I'll have only two properties:

public int Id { get; set; }
public string Name { get; set;

The first property is purely for data removal purposes - I assume that you'll pass id to your data layer eventually. Second property is mainly for better UX journey. You might use it for notifying user that they are about to remove a country. E.g., in your view you might have

<h2>Warning</h2>
<p>Are you sure you want to remove @Model.Name</p>
\$\endgroup\$
1
\$\begingroup\$
  1. It's ok to do that, classes are there to reuse them.

  2. I think the Country part in namespace is redundant and if it is a model that you just use in your view you can name the class CountryViewModel.
    About the other class: It's hard to say if there are any other related classes in that namespace or not but it seems reasonable.

\$\endgroup\$
0
\$\begingroup\$

Unless you have several country related classes don't see the point of having a Country namespace.

In case you do have several country related classes, call your namespace different so is not the same as the name of the class.

Without knowing the rest of the classes is hard to suggest a proper name, but just changing Country by CountryModels could do the trick.

\$\endgroup\$
4
  • \$\begingroup\$ Is this the recommended way of coding the model namespaces and is it common to share models in this manner ? Re the name you mean namespace Site.Models.CountryModels ? \$\endgroup\$ – Tommo1977 Aug 28 '13 at 12:28
  • \$\begingroup\$ Valid solution. But the reason you are having problems is because you a namespace and a class with the same name, and because of that you need to provide a disambiguation to let the compiler know wich one you are referring to. \$\endgroup\$ – svrx Aug 28 '13 at 12:31
  • \$\begingroup\$ @Tommo1977: I'm no sure what you mean by "share models". Models are expected to be related to each other, there is nothing wrong with that. \$\endgroup\$ – Claudio Redi Aug 28 '13 at 12:32
  • \$\begingroup\$ @ClaudioRedi OK I understand \$\endgroup\$ – Tommo1977 Aug 28 '13 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy