Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HOW TO Implentation on NHibernate with ASP.NET MVC 2

I have been working on a MVC application for the past couple of days. In this I am using couple of dropdownlists and wanted to if what I have done is a good practice. I have around 5-6 dropdowns which are pulled in from the database. There dropdowns have a ID and description field. I am able to get the dropdowns filled in with out a problem. However when I am listing the master table is when I am having performance problem.

All the dropdowns selections are stored as integer in the database so I have the field also as in the BaseModel (Class that is mapped to HBM). When I list all the records from the database, predictably I get a integer in the record. So far I have no issue in the performance.

I wanted to display the description for the selected item so I create a model class for the dropdowns and have a method in the model which will talk to the database and get the description based on the selection. The problem is this is slowing down the page load. I wanted to know if I need to make design change to get this loaded faster. The following is the code I have

MasterList1 Table (State and County are integers in this table) State Dropdown (Master table having all states with ID) County Dropdown (Master table having all counties with ID)

BaseModel Classes for Nhibernate

MasterList1 State County

Model Class

MasterList1Model StateModel CountyModel

Repository Class MasterList1Repo StateRepo CountyRepo

View MasterList1

In the view I am calling a string proprty in the BaseModel class. In the property, I am placing a call to the Model class which in turn places a call to the Repo to get the string. Here is the method in Repo class.

        public ApplicationTypeMaster GetByID(int ID)
    {
        using (ISession session = NHibernateHelper.OpenSession())
        {
            return session.Get<ApplicationTypeMaster>(ID);
        } 
    }

    public string GetApplicationTypeByID(int ID)
    {
        return GetByID(ID).ApplicationTypeDescription.ToString();
    }

I am not sure how to improve this. Any suggestions ?

like image 946
vikramjb Avatar asked May 23 '13 08:05

vikramjb


1 Answers

Do you know the ViewModel approuch? It is a class that represents what a View need to show. You can create a ViewModel class with theses proeprties you need and type your Views with this class, fill it with NHibernate and then show it, for sample:

public class ApplicationTypeMasterViewModel
{
   public int Id { get; set; }
   public string Name { get; set; }
   public string CityName { get; set; }
   public string StateName { get; set; }

   // other properties you need

   public ApplicationTypeMasterViewModel() { }   
}

in your Service Layer, try something like this:

public ApplicationTypeMasterViewModel GetByID(int ID)
    {
        using (ISession session = NHibernateHelper.OpenSession())
        {
            return session.Query<ApplicationTypeMaster>()
                            .Fetch(x => x.City).ThenFetch(c => c.State)
                            .Where(x => x.Id == ID)
                            .Select(x => new ApplicationTypeMasterViewModel() { 
                                                    Id = x.Id, 
                                                    Name = x.Name, 
                                                    CityName = x.City.Name, 
                                                    StateName = x.City.State.Name
                                                    // other properties you need
                                            })
                            .FirstOrDefault();
        } 
    }

You could do it using Lists or Single objects.

In the controller you call this method and pass the ViewModel to your View and render it:

<p>
    <label>Name: </labe>
    <%: Model.Name %>
</p>

<p>
    <label>City: </labe>
    <%: Model.CityName %>
</p>


<p>
    <label>State: </labe>
    <%: Model.StateName %>
</p>
like image 60
Felipe Oriani Avatar answered Sep 24 '22 18:09

Felipe Oriani