Help with using MVC and Entity Framework

 
55   94.5
Dec 27, 2010
 
Hi guys,

I have come here after getting some assistance in the news groups, but I am looking for help with my current Design in finding a suitable Design Pattern to learn, and maybe apply to my current project.

I am doing a small application for a non-profit group, more as a learning experience for my professional work. I am a .Net developer, but never really made use of any Design Patterns. I basically hand crafted my work. My goal is to see if I can improve my project with ER and MVC.. but also, to learn.

My current application has an established SQL Server database hosted on a popular hosting server. I have a WinForms application (C#, .Net 3.5) which is installed onto numerous user-desktops. They then contact a classic ASP.Net WebService (never used WCF..) hosted on the hosting company's server, which in turn, connects to my SQL Server database.

I have a ClassLib project which is shared between the WinForms app, and then WebService, which contains the definition of a 'CommRequest' objects. The object has a few properties, as well as a 'RequestParameters List<String>' and a 'ReplyParameters List<String>. The WinForms app populates the properties, as well as maybe adds a few parameters to the List<>. I then have one method in my WebService, called 'Request'. It accepts a CommRequest object, and then returns the same object, once the operation on the server is complete.

On the web service, I have a Database class, which handles database connections and executes stored procs. I then have a DataAccessor for each of the database entities. Within the web service, there is also a EntityObject for each database table. This is all generated using MyGeneration. So I make a table change, and then regenerate the DAL.

So, lets say the application makes a request to see if it's the latest version. I build a CommRequest, and pass it to the WebService... The Request method accepts it, works out what the reuqest is for, and then calls a Service Layer method ... Maybe 'FirmwareService.CheckLatestVersion()'. That then calles the DataAccessor... which calls a stored proc... and then the data goes back up to the service layer, which rebuilds the CommRequest.ReplyParameters and then returns the object to the WinForms app.

As far as I know, I am using no Patterns here...

I find it easy to maintain.. easy to debug.. and I have followed a KISS principle. However... maybe something like Entity Framework within the Web Service would make life easier, and use more industry friendly Patterns?

Basically, I am looking for someone/people to help me work out where my current design falls short, and maybe guide me in using a better Framework or Design Pattern for my application.

I also have a Web Page that makes use of the Web Service and that is starting to get new features. So, I'm trying to ensure I am sharing code well, and following a nice neat OO design.

I have attached some example code of my generated DAL stuff.



All Data Accessors make use of this single method for Database calls: 

 public SqlDataReader ExecuteStoredProcedure(SqlCommand cmd) 
        { 
            if (_conn.State == ConnectionState.Closed) 
                    _conn.Open(); 
            cmd.Connection = _conn; 

            try
            { 
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 

                if (dr != null) 
                    if (dr.HasRows) 
                    { 
                        return dr; 
                    } 
            } 
            catch (Exception e) 
            { 

                LoggingService.WriteDebugLog("DB Error: " + e.Message); 
                LoggingService.WriteDebugLog("Procedure call was: " + cmd.CommandText); 
                foreach (SqlParameter sqlParameter in cmd.Parameters) 
                { 
                    LoggingService.WriteDebugLog(string.Format(" - Parameter name = [{0}], value = [{1}]", sqlParameter.ParameterName, sqlParameter.Value)); 
                } 
                throw; 
            } 
             

            return null; 
        } 


And this is an example of a Data Accessor call, in which we are requesting a DeviceObject to be returned, based on the PK: 

public static DeviceObject Get(int deviceId) 
{ 
        // Create the Database object, using default database (defined in config file).
        Database db = new Database(); 

        const string sqlCommand = "up_device_SelectBy_PK_device_id"; 
        SqlCommand cmd = new SqlCommand(sqlCommand); 
        cmd.CommandType = CommandType.StoredProcedure; 

        // Add parameters to the parameter cache.
        cmd.Parameters.Add(new SqlParameter("@device_id", deviceId)); 
        DeviceObject deviceObject = null; 

        using (SqlDataReader dataReader = db.ExecuteStoredProcedure(cmd)) 
        { 
                if (dataReader.Read()) 
                { 
                        deviceObject = ReadResult(dataReader); 
                } 
        } 
        return deviceObject; 
 } 



Read Result is a generic method used to read in the fields from the table, in to the objects properties: 

/// <summary>/// Returns an instance of type deviceObject read from a reader./// </summary>
private static DeviceObject ReadResult(IDataReader dataReader) 
{ 
        DeviceObject deviceObject = new DeviceObject(); 
        deviceObject.DeviceId = DbFunctions.GetInt32(dataReader["device_id"]); 
        deviceObject.DeviceTypeId = DbFunctions.GetInt32(dataReader["device_type_id"]); 
        deviceObject.DeviceUid = DbFunctions.GetString(dataReader["device_uid"]); 
        deviceObject.DateRegistered = DbFunctions.GetDateTime(dataReader["date_registered"]); 
        deviceObject.Deleted = DbFunctions.GetNullableDateTime(dataReader["deleted"]); 
 
        return deviceObject; 
} 

 



578   99.9
Dec 31, 2010
You might want to take a look at my code generator.  It creates n-Tier code based on your designs, but it does come with out-of-the-box code to generate standard n-Tier frameworks, or frameworks based on the Patterns In Action framework.

But this code generator generates an infrastructure that is scalable and easy to maintain, and you can create your own framework for future applications that you build.  So it could be a good place to start if you want some examples on how to build a good n-Tier framework.

Did I mention it's free and you have access to all the source code?  It uses the MyGenerationSoftware code generation tool for generating the code (which is also free).

Here are some videos so you can see what it is and what it does.

If you want a copy of the code generator, send me an email: info@mvccentral.net.  This version of the code generator contains code based on the DoFactory Patterns In Action framework and I'm only making it available to DoFactory members.  So send me your DoFactory user name so I can confirm you.

Thanks,

King Wilder