Hey,
it's me again, I know I have asked a lot last time, but I am new to C# and I think here's the right place to ask, u helped me very much.
I want to create an application with OOP, I have read a lot about it and I think I know what it is (Objects, Accesss Modifiers, ...) but I don't know how to use it in a real Application.
For example if u have a form "Customers", where u can add, list, edit and delete customers (CRUD) should I create on class for each form where I put all my functions in like
saveCustomer(vars), getCustomer(id), ... or should I create to classes like
Controller (where I put all my programing logic in, handle inputs...) and a Model (where I handle the database access, queries)
I hope I have explained it understandable and someone can help me.
A little example application would be also great.
Thanks
Loading
Sukesh MarlaPosted Aug 14, 2012, 1:27 PM
and each time you increase fields u need to keep on modifying every layer
Your Conroller, Model and UI
by making entites.
1.Function becomes more readable.
2. Modification in controller not required when new property comes, unless and until there is a change in logic.
as i said, Always go on grouping means encapsulating..and here are encapsulating all emplyee properties into a single container..
Just think in terms of real worlds
it will be difficult to 10 notebooks when they are separate but easy if you put all of them in a single container,
dont forget to mark this is currect answer
AlexanderPosted Aug 14, 2012, 1:21 PM
I have one last example, can you say me if am right?
Person
frmPerson
This is my form and I will just put my events and methods calling in there e.g.
btnAdd_click
PersonController Person = new Person();
if(Person.add(values of the input fields))
{
// do anything
} else {
// error
}
PersonController
public add(vars) {
// data Manipulation
PersonModel PersonM = new PersonModel();
PersonM.save(vars);
}
PersonModel
public save(vars) {
// save the person into the db with the data access layer
}
Is this right?
So why would I need to declare my vars like
Public ID {get; set}
Public Name {get; set}
If I call the method with the fields/vars as paremter.
I hope this was my last question about this topic and you know what I mean :)
Thanks
Sukesh MarlaPosted Aug 14, 2012, 1:06 PM
2. Always use separate dataaccesslayer. It saves our time by , becuase we are not required to write same sqlconnection and other sql codes again and again, and also you logic code become less cluttered.
you can use following dataaccesslayer if you want, its even easy to use.
http://www.c-sharpcorner.com/Downloads/Details.aspx?DownloadId=251
3.
3 steps means, i was just trying to explain you that,Try break your logic into multiple logics, such that at the end 1 function=1logic.
dont forget to mark this is currect answer
AlexanderPosted Aug 14, 2012, 12:52 PM
I think I got u, but I have some questions again.
1, Where do I create the stored procedures, right in the database (sql management studio) right?
2, Do I really need a data access layer? I mean it's just a class to handle my database (select, insert, ...) But I could do this also in my class with the sql statements, or?
3, Do u have any simple sample application with these 3 steps (Data Retrieval,DataManipulation and final dumping into databse)?
Thanks
Sukesh MarlaPosted Aug 14, 2012, 12:02 PM
1.Create Stored Procedures to instert/select/update Records like
Create Procedure InsertCustome
(
@CustomerName varchar(100)
)
begin
insert into TblCustomer values(@CustomerName)
end
2. Consider you have a datatable with a column say EmployeeName,EmployeeId and so on..
Instead of writing like this
A]
DataTableObject.Rows[0][0]
Write
DataTableObject.Rows[0]["EmployeeName"]
B)
string flag;
.
.
.
flag="Edit";
.
.
.
If (flag=="new") {//new operation}
else {//Edit}
Use
Enum CurrentFlag
{
New,Edit
}
.
.
.
CurrentFlag flag;
.
.
.
Flag=CurrentFlag.Edit;
.
.
.
If(flag==CurrentFlag.New){//New Operation}
Else {//Edit}
3.
And you are right about Classes, I tried to explain u same thing..
Keep on grouping thing, By Braking them into smallest units
i)1 logic=1 functiom
ii)GroupOf(Simlar Functions )=1 class // Here I mean all person methods into one class, all Product into second…Each of which internally use SQLHELPER class will does tasks related to Sql
You can find such DataAccessLayer easily on internet, Find this one.
http://www.c-sharpcorner.com/Downloads/Details.aspx?DownloadId=251
iii)
Group of SimilarClasses=1 namespace.
-----------------------------------------
Always make your logic smaller and most imoirtantly separate from other
example:
1.In textbox u will enter a number
2.U double it
3.Then Store Final Result into database.
Means 3 logic,
Data Retrieval,DataManipulation and final dumping into databse
Dont forget check this is is currect answer
AlexanderPosted Aug 14, 2012, 11:35 AM
thanks for your fast reply.
What do u mean with this
"3.Create Stored Procedures for Database operations.
4.Try to avoid accessing elemnts using indexes use names,ids etc.(even if possible use enums)." ?
Can u please give me an example?
And u said I should group all functions of one type into a class e.g.:
sqlDataClass.cs
getCustomers(), saveCustomer(vars), getProducts(), ...
But I thought I should group all functions of one Class e.g. Person or Product into a class,
then otherwise I have all functions of all different Objects in one class.
In PHP I often used Frameworks like CakePHP and Zend they were all MVC frameworks and it was easy to understand for me. I created a Model, a Controller and a View for each page and it was easy for me to understand which code comes in which class.
Hope you know what I mean.
Thanks.
Sukesh MarlaPosted Aug 14, 2012, 8:41 AM
OOPS is not Technology, Its a concept, Its a methodology.
If you are compltly new then i would like to share some some points/tips , follow it and thats it.
1.if you write a block of code once dont write it again
2.Always be lazy while writing code,
3.Create Stored Procedures for Database operations.
4.Try to avoid accessing elemnts using indexes use names,ids etc.(even if possible use enums).
5.At intial stage i will not suggest 3 tier, Just follow a simple Rule
One Function -> One logic
and
Also group all the functions of one type into one class, like all Maths related functions to Maths Class,All database related functions to class say Sqlhelepr..
Your Response is important to use. Please Check This is Currect Answer, if it helped