473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C#/OOP Frustrations:)

Hello All,

I'm starting to learn C# and OOP to become a better programmer, however I;m getting frustrated. It's tough, but what is making it really tough for me is trying to do everything with objects in mind. Here is a typical example of a scenario that I could choose different ways to approach. I have a class called Pet. Pet has an insert, update, delete, get all, get specific methods inside the class. I have a form that has the pet properties (fields) on it. I want to insert the pet (fields) properties inside a database when someone clicks the Insert Button. The first method I tried was to use the ObjectDataSource control. It doesn't work, it gives me errors on the ControlID, then when I add a control ID, it gives me a different error "Target Invocation Exception" on the ObjectDataSource.Insert() method.

So...my second thougt is to abandon the ObjectDataSource idea completely and use the methods of the object when events such as Button clicks occur. That would mean, for every button click, add, getall, edit, delete - the same object would need to be created and I would used it's proper method. It seems expensive, redundant, and not how one should use objects, but at the same time, it seems easier to understand. What are your thoughts? Thank you for all responses - frustrated, but hopeful!!!

Sep 7 '06 #1
3 2154
Mel
In the basic way.

Create your pet class

public class Pet
{
private string color;

public string Color
{
get { return color}
set { color = value;}
}

}
then create a collection of pets

public class PetColllection : List<pet>
{
// now you can use the list to add, delete, etc...

}

Now create your new objectdatasource using the collection.

This is the very basic, hopefully it will get you started.

"Shawn Ferguson" <SF******@cscc.eduwrote in message news:%2****************@TK2MSFTNGP05.phx.gbl...
Hello All,

I'm starting to learn C# and OOP to become a better programmer, however I;m getting frustrated. It's tough, but what is making it really tough for me is trying to do everything with objects in mind. Here is a typical example of a scenario that I could choose different ways to approach. I have a class called Pet. Pet has an insert, update, delete, get all, get specific methods inside the class. I have a form that has the pet properties (fields) on it. I want to insert the pet (fields) properties inside a database when someone clicks the Insert Button. The first method I tried was to use the ObjectDataSource control. It doesn't work, it gives me errors on the ControlID, then when I add a control ID, it gives me a different error "Target Invocation Exception" on the ObjectDataSource.Insert() method.

So...my second thougt is to abandon the ObjectDataSource idea completely and use the methods of the object when events such as Button clicks occur. That would mean, for every button click, add, getall, edit, delete - the same object would need to be created and I would used it's proper method. It seems expensive, redundant, and not how one should use objects, but at the same time, it seems easier to understand. What are your thoughts? Thank you for all responses - frustrated, but hopeful!!!
Sep 7 '06 #2
Hi Shawn,

Great news on learning the C# and OOP stuff, trust me this stuff seriously rules but if you are moving over from a functional style it can be difficult at first to see the point.
Don't know about the ObjectDataSource (that .NET 2? Man i'm really missing out.....) but I might be able to help out with the OOP element.
To be honest the whole creating the object every operation doesn't sound to non-oop, it sounds a bit like the classic Java Petshop example. This recommands creating an object called PetADO that understands how to save your pet so that you can forward requests onto that.
E.G:

public class Pet
{

public void Save()
{
PetADO ado = new PetADO(this);
ado.Save();
}
}

I personally don't like this pattern though. I prefer to keep database concerns on the outside of the domain objects.

-<opinion>

The problem I think you are having with your understanding of OOP is that your pet object has become the world.
The persistance and displaying of the pet object should not be a concern of the pet itself. It should be more concerned about:

Yap(int loudness);
IrritateOwner();
BegForFood();
Eat(Food food);

And so forth. What you need to do is create a different object that is interested in saving stuff.
Remember that the objects saved in your database are the currency and domain of the program, they are the objects that other, more important objects use to perform tasks for the user.

In terms of data structure I usually create a DataStore object that understands all of the entities in a program in relation to persistence (saving, updating, deleting etc) and that deals with the persistence concerns.

For example: (this is not a complete example)

public class PetForm:Form
{

private IServiceProvider services;
private IDataStore dataStore;

public PetForm(IServiceProvider services)
{
this.services = services;
dataStore = (IDataStore) services.GetService(typeof(IDataStore));
this.listBox.SelectedItemChanged+= new EventHandler(listbox_SelectedItemChanged);
UpdateList();
}

/// TODO: Cache to improve performance
private void UpdateList()
{
this.listBox.DataSource = dataStore.GetList(typeof(Pet));
}

private Pet SelectedPet
{
if(listBox.SelectedItem != null)
{
return (Pet) listBox.SelectedItem;
}
else
{
return null;
}
}

private void listBox_SelectedItemChanged(object sender, EventArgs e)
{
if(SelectedPet != null)
{
BindPet(SelectedPet);
}
}

public void BindPet(Pet pet)
{
this.txtName.DataBindings.Clear();
this.txtName.DataBindings.Add("Text", pet, "Name");
}

private void btnNew_Click(some args)
{
IList list = this.listBox.DataSource as IList;
Pet pet = new Pet();
pet.Name = "New Pet";
dataStore.Save(pet);
list.Add(pet);
UpdateList();
}

private void btnUpdate_Click(some args)
{
if(SelectedPet != null)
{
dataStore.Update(SelectedPet);
}
}

private void btnDelete_Click(some args)
{
if(SelectedPet != null)
{
dataStore.Delete(SelectedPet);
UpdateList();
}
}

// etc
}

That kind of thing. (you can improve the implementation more by using currency manager and using the list as the argument to DataBindings.Add())

-</opinion>

In general all these decisions are up to you, you can develop using tables and ADO.NET, use pure SQL and either map to tables or map to objects in the datalayer or even use a ORM.
I just don't recommand getting your objects to take on too many concerns.

HTH

Simon

"Shawn Ferguson" <SF******@cscc.eduwrote in message news:%2****************@TK2MSFTNGP05.phx.gbl...
Hello All,

I'm starting to learn C# and OOP to become a better programmer, however I;m getting frustrated. It's tough, but what is making it really tough for me is trying to do everything with objects in mind. Here is a typical example of a scenario that I could choose different ways to approach. I have a class called Pet. Pet has an insert, update, delete, get all, get specific methods inside the class. I have a form that has the pet properties (fields) on it. I want to insert the pet (fields) properties inside a database when someone clicks the Insert Button. The first method I tried was to use the ObjectDataSource control. It doesn't work, it gives me errors on the ControlID, then when I add a control ID, it gives me a different error "Target Invocation Exception" on the ObjectDataSource.Insert() method.

So...my second thougt is to abandon the ObjectDataSource idea completely and use the methods of the object when events such as Button clicks occur. That would mean, for every button click, add, getall, edit, delete - the same object would need to be created and I would used it's proper method. It seems expensive, redundant, and not how one should use objects, but at the same time, it seems easier to understand. What are your thoughts? Thank you for all responses - frustrated, but hopeful!!!
Sep 7 '06 #3
I agree with Simon. The simplest (fewest classes) OOP model of a
business object like the one you describe would be:

1. One class for the Form, which handles display considerations, and
knows how to get information out of the form fields to build a Pet.

2. The Pet class itself, which is pure business logic and knows nothing
about Form controls or database storage.

3. A class that knows how to persist Pets into the database and read
them back again.

In my implementations, I have even more classes (!):

4. A PetValidator, which is the brains of the PetForm. It knows how to
validate user input, which fields should appear / disappear based on
values of other fields, and stuff like that. It also knows how to build
a Pet out of valid inputs (how to call the constructor for Pet). It
communicates with the PetForm using events and properties (or, in .NET
2.0, object property bindings).

5. A PetCollection, which contains only Pets, and knows how to filter /
search itself in ways specific to Pets.

I'm sure that other implementations may have even more classes.

Try starting with the first three, though, and see how it goes.

Sep 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Adrienne | last post by:
This page validates XHTML-Strict and the CSS validates as well. Opera and Netscape 7.0 display it correctly. There should be a film strip on the left side, bordered in red, a list of links at...
4
by: Matthew Crouch | last post by:
As noted before, I'm not a JS programmer, but I ought to be able to get a couple simple things done with it and now I've spent two weeks on them. Main reasons: - seems like JS is all-or-nothing....
10
by: Sahil Malik | last post by:
This is a C# project. My code highlighting decides to dissappear randomly. With that goes away my intellisense, ability to copy paste, and my sanity. What is going on? - Sahil Malik...
4
by: AWesner | last post by:
For readability sake I’m going to first state that: LF = Line Feed CHR(10) CR = Carriage Return CHR(13) Since Rich Text Format is a standard formalized by Microsoft Corporation I get to ask...
9
by: S Moran | last post by:
im starting to think everything became A LOT more complicated with .net as opposed to vs6. how can i access a textbox on a form from another form? for example i have a button on form1 and when...
0
by: =?Utf-8?B?d25kcmRvZw==?= | last post by:
1. what are the maximum number of guides available in PPt08? There seem to be separate limits on horiz and vert guides, and one I've maxed out (20 horiz + 20 vert?), the next guide I add deletes...
1
by: sean_walsh | last post by:
Hi From classic ASP, I had a custom error handling situation that was quite simple. Errors were all redirected to Error.asp. This page would check 2 settings, EmailErrorMessage and...
11
Airslash
by: Airslash | last post by:
Hi, I'm sorry if this is not the correct forum, but since I'm programming in C++ this looked like the best place. I'm trying to make a nice DLL file for the company where we place all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.