Reputation: 2790
Im designing an application from the ground up. I do this as a study object to get my coding skills better and better. In this application I have two entities:
Adding a country is not hard. I pass the viewmodel in the business layer, the CountryService, convert it in an entity and persist it.
Secondly I need to add a city. To do this, I have a viewmodel that has a CountryId and the fields for city. I pass the viewmodel to the CityService. There I need to do a couple of things. I need to retrieve the correct country entity, convert the city data to a city object, add either the city object to the country or add the country to the city (via the AddXXX method Nhibernate propose). Then I need to save the City with country via the city repository or save the country with city via the country service and then in the country repository.
With the mindset of having 1 function per method I'm kind of lost how to structure this kind of functionality.
Upvotes: 0
Views: 87
Reputation: 22424
Instead of retrieving the country in the city service why not just pass in the CountryId
into the city service method and use:-
var city = new City
{
Name = "London",
country = session.Load<Country>(countryId)
};
session.save(city);
This does not hit the database and physically return the record, it simply allows you to set the foreign key.
That said another way is to embrace Nhibernate in your MVC project and expose the session, rather than abstracting it completely away. This train of thought is gaining momentum.
Upvotes: 2