I've been writing .NET applications for several years but none of them have been especially complicated and I have managed to survive without using classes. Last year I was involved in a major development project which was delivered but the architecture
is a nightmare to maintain. I've moved on to developing something else using MVC3 and I am really enjoying it - part of this is down to the fact that I have started trying to use classes in a serious way and it's making life so much easier.
One thing I wanted to get opinions on is practical use of a class. So take this example. In the app I am working there is the concept of a Manager. A Manager has a number of attributes - their name, their IDs, stuff like that. A manager also has a list of
budget codes they can see information about.
Question 1 - the use of New(). The way I have it working now a user wants to see information about a specific manager. So in my app I do something like Dim myman = New MyManager() (I'm a VB.NET coder). And then in my class file in Sub New() I go and build the
view of the manager. So I go to database X and get their personal information, I go to database Y and find out which budget codes they can see. Practically speaking, should I be building the class this way?
Question 2 - expanding the class. My app needs a widget that shows the state of the biggest five budgets that the manager has in their portfolio. To get this data I have added a List property to the class, and in Sub New() I run a LINQ query to get the information
into a list. Then when I want to serialize it into JSON I just create a new instance of the manager and serialize the list property. Again, is this the correct way to use a class or am I making a big mistake?
Interested to hear opinions, even if they are along the lines of 'what the heck are you doing'. I've never had any formal training in this stuff and it's never too late to start.
I've been writing .NET applications for several years but none of them have been especially complicated and I have managed to survive without using classes.
I frankly doubt.More, you are using classes - pretty much of .NET consists in classes.
markperry
Question 1 - the use of New(). ...
ok
markperry
Question 2 -
Yes.
markperry
I've never had any formal training in this stuff and it's never too late to start.
Please see http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/
I've been writing .NET applications for several years but none of them have been especially complicated and
I have managed to survive without using classes.
Impossible. The entire .NET framework is build on top of classes. Even your web forms and win forms use classes.
markperry
Question 1 - the use of New(). The way I have it working now a user wants to see information about a specific manager. So in my app I do something like Dim myman = New MyManager() (I'm a VB.NET coder). And then in my class file in Sub New() I go and build
the view of the manager. So I go to database X and get their personal information, I go to database Y and find out which budget codes they can see. Practically speaking, should I be building the class this way?
I haven't coded in VB.NET in a long time, but I remember New being the constructor of a class. I don't believe that the Manager class should hold the responsibility of populating itself. It definitely shouldn't have knowledge of getting database access
code.
markperry
Question 2 - expanding the class. My app needs a widget that shows the state of the biggest five budgets that the manager has in their portfolio. To get this data I have added a List property to the class, and in Sub New() I run a LINQ query to get the information
into a list. Then when I want to serialize it into JSON I just create a new instance of the manager and serialize the list property. Again, is this the correct way to use a class or am I making a big mistake?
Interested to hear opinions, even if they are along the lines of 'what the heck are you doing'. I've never had any formal training in this stuff and it's never too late to start.
I think we need more higher level details in order to put you in the right direction.
I accept that the .NET Framework is built on top of classes (even declaring a String variable is in fact creating a new instance of the String class - correct?) and what I really meant was that I have never really explored building my own custom classes
before.
Voodoo - practically speaking, if the Manager class shouldn't populate itself then how should I do this? One of my requirements is to have all the information about the manager available at my fingertips without having to call a series of functions. I thought
that the idea of a class was to model a specific entity in the system?
Also I have read the example that Andrei Ignat puts on this blog (http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/) and he splits
his application into four levels - Data Access --> Business Logic --> View Model --> View and Controller. In his Business Logic layer he has two custom classes (Employee and EmployeeList) that he populates from the Data Access layer via LINQ. Then in his View
Model layer he has another class that looks like it retrieves information. Could one of you explain a bit better about how these two layers should work in practical terms?
Since you are not using classes I am assume you are using 'Modules' then in VB.NET? The problem with Modules (or static classes in C#) is they do not lend themselves at all to Object Oriented Programming and all the concepts that come along with it. I had
experience early in the days of .NET with VB6 converted .NET developers using a lot of Modules in fear of the big bad 'class' and OO coding. Once you see the light of doing development the OO way as opposed to procedural based coding (which often leads to
'Spaghetti code'), you will understand all the benefits of it's basic building block: The Class.
As a side note - Modules are only offered by MSFT in VS.NET for grandfathering code and for familiarity reasons. I never use them anymore; I would use a 'Shared Class' in VB.NET for say 'Helper' classes which cumulatively offer no consistent behavior and
need direct access without instantiation.
You probably have a lot to learn about Object Oriented Programming if you have not been using a class so I would begin at reading and immersing yourself in it. In fact I am surprised using MVC you would have been able to survive without using classes based
on all of the examples, but I guess its possible, Lastly, for a high level overview between classes and modules have a look to the following:
In his Business Logic layer he has two custom classes (Employee and EmployeeList) that he populates from the Data Access layer via LINQ. Then in his View Model layer he has another class that looks like it retrieves information. Could one of you explain a bit
better about how these two layers should work in practical terms?
The database Model is for me the same as the Classes Model. I adopt a Table Per Class idiom ( someone will not agree - but let this apart).
The Views ,usually, involves more classes. So that's why a ViewModel exists.
But let's take from the beginning: First it exists what the user wants. He want to assign an user to department.
From this you have 2 classes :User and Department. Also , for storing information, you have also 2 tables: User and Department.
Also, coming back to what the user wants( to assign an user to departments), you create a ViewModel that combines a User with a LIst of departments to the user can see what he wants to do.
markperry
Member
128 Points
42 Posts
Class design 101
Jan 12, 2012 03:24 PM|LINK
I've been writing .NET applications for several years but none of them have been especially complicated and I have managed to survive without using classes. Last year I was involved in a major development project which was delivered but the architecture is a nightmare to maintain. I've moved on to developing something else using MVC3 and I am really enjoying it - part of this is down to the fact that I have started trying to use classes in a serious way and it's making life so much easier.
One thing I wanted to get opinions on is practical use of a class. So take this example. In the app I am working there is the concept of a Manager. A Manager has a number of attributes - their name, their IDs, stuff like that. A manager also has a list of budget codes they can see information about.
Question 1 - the use of New(). The way I have it working now a user wants to see information about a specific manager. So in my app I do something like Dim myman = New MyManager() (I'm a VB.NET coder). And then in my class file in Sub New() I go and build the view of the manager. So I go to database X and get their personal information, I go to database Y and find out which budget codes they can see. Practically speaking, should I be building the class this way?
Question 2 - expanding the class. My app needs a widget that shows the state of the biggest five budgets that the manager has in their portfolio. To get this data I have added a List property to the class, and in Sub New() I run a LINQ query to get the information into a list. Then when I want to serialize it into JSON I just create a new instance of the manager and serialize the list property. Again, is this the correct way to use a class or am I making a big mistake?
Interested to hear opinions, even if they are along the lines of 'what the heck are you doing'. I've never had any formal training in this stuff and it's never too late to start.
http://markp3rry.wordpress.com
Tweet @markp3rry
ignatandrei
All-Star
134947 Points
21626 Posts
Moderator
MVP
Re: Class design 101
Jan 12, 2012 08:06 PM|LINK
I frankly doubt.More, you are using classes - pretty much of .NET consists in classes.
ok Yes.Please see http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/
voodoo9055
Participant
1873 Points
438 Posts
Re: Class design 101
Jan 13, 2012 11:39 AM|LINK
Impossible. The entire .NET framework is build on top of classes. Even your web forms and win forms use classes.
I haven't coded in VB.NET in a long time, but I remember New being the constructor of a class. I don't believe that the Manager class should hold the responsibility of populating itself. It definitely shouldn't have knowledge of getting database access code.
I think we need more higher level details in order to put you in the right direction.
markperry
Member
128 Points
42 Posts
Re: Class design 101
Jan 13, 2012 12:25 PM|LINK
Thanks for both responses.
I accept that the .NET Framework is built on top of classes (even declaring a String variable is in fact creating a new instance of the String class - correct?) and what I really meant was that I have never really explored building my own custom classes before.
Voodoo - practically speaking, if the Manager class shouldn't populate itself then how should I do this? One of my requirements is to have all the information about the manager available at my fingertips without having to call a series of functions. I thought that the idea of a class was to model a specific entity in the system?
Also I have read the example that Andrei Ignat puts on this blog (http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/) and he splits his application into four levels - Data Access --> Business Logic --> View Model --> View and Controller. In his Business Logic layer he has two custom classes (Employee and EmployeeList) that he populates from the Data Access layer via LINQ. Then in his View Model layer he has another class that looks like it retrieves information. Could one of you explain a bit better about how these two layers should work in practical terms?
http://markp3rry.wordpress.com
Tweet @markp3rry
atconway
All-Star
16846 Points
2756 Posts
Re: Class design 101
Jan 13, 2012 03:13 PM|LINK
Since you are not using classes I am assume you are using 'Modules' then in VB.NET? The problem with Modules (or static classes in C#) is they do not lend themselves at all to Object Oriented Programming and all the concepts that come along with it. I had experience early in the days of .NET with VB6 converted .NET developers using a lot of Modules in fear of the big bad 'class' and OO coding. Once you see the light of doing development the OO way as opposed to procedural based coding (which often leads to 'Spaghetti code'), you will understand all the benefits of it's basic building block: The Class.
As a side note - Modules are only offered by MSFT in VS.NET for grandfathering code and for familiarity reasons. I never use them anymore; I would use a 'Shared Class' in VB.NET for say 'Helper' classes which cumulatively offer no consistent behavior and need direct access without instantiation.
You probably have a lot to learn about Object Oriented Programming if you have not been using a class so I would begin at reading and immersing yourself in it. In fact I am surprised using MVC you would have been able to survive without using classes based on all of the examples, but I guess its possible, Lastly, for a high level overview between classes and modules have a look to the following:
Classes vs. Standard Modules:
http://msdn.microsoft.com/en-us/library/7825002w(v=VS.90).aspx
Hope this helps!
ignatandrei
All-Star
134947 Points
21626 Posts
Moderator
MVP
Re: Class design 101
Jan 15, 2012 07:04 AM|LINK
The database Model is for me the same as the Classes Model. I adopt a Table Per Class idiom ( someone will not agree - but let this apart).
The Views ,usually, involves more classes. So that's why a ViewModel exists.
But let's take from the beginning: First it exists what the user wants. He want to assign an user to department.
From this you have 2 classes :User and Department. Also , for storing information, you have also 2 tables: User and Department.
Also, coming back to what the user wants( to assign an user to departments), you create a ViewModel that combines a User with a LIst of departments to the user can see what he wants to do.