|
After further digging, I've realized I may be combining my data layer with my domain layer. These concepts are a bit foreign to me, but I've realized there is a reason for separating these layers in order to avoid the sorts of design problems I'm encountering.
I have my domain layer model of Employee and I'm trying to do the queries for LDAP in the same object. I realize now I need to separate the data access functions from the Employee class.
Now I'll need to look into how the data access functions remain generic enough yet still allow you to populate the objects. Do the data access functions just exist in a "flat layer" or do they exist as objects themselves? I would think the data access methods would need to exist as extensions of the object they are to be used with.
Thanks to those of you who have read through my design issues.
|
|
|
|
|
Mark McArthey wrote: Now I'll need to look into how the data access functions remain generic enough yet still allow you to populate the objects.
There are a number of different approaches, none of them are a silver bullet. You have hit at one of the stickiest problems in software design. Take a look at ORM[^] for one of the big names in this problem space.
|
|
|
|
|
If you have your employee class built then you can do the following:
public EmployeeCollection
{
// You can use any collection but dictionary will help you spot them easier through their id
private Dictionary<string, Employee> _employees;
public EmployeeCollection()
{
this._employees = new Dictionary<string, Employee>();
}
public void Load()
{
// Ask the DAL class to get all the employees
EmployeeCollectionMgr manager = new EmployeeCollection();
this._employees = manager.Retrieve();
// The manager can either grab the records from the database and create employee objects, add
// to the collection and return the collection OR
// the manager can grab all the employee ids from the database and then create an employee object and
// ask the employee object to load itself.
// In the first approach you need one trip to the database but you will do some of Employee's work
// In the second approach you will need 1 trip per employee to the database and one to get all the ids
// but Employee will do all the work
// You can have many other methods here depending on the domain model at hand.
}
}
CodingYoshi
Artificial Intelligence is no match for Human Stupidity.
|
|
|
|
|
I have a very simple question:
If the View is supposed to know the Model (that is, the View has a field of type Model),
why would I need the controller if the view can modify and get responses from the model directly?
|
|
|
|
|
Quake2Player wrote: why would I need the controller if the view can modify and get responses from the model directly?
Maybe you don't, maybe the pattern and everyone that uses it is wrong.
Or
Maybe you need to study Object Oriented Design Principles[^] more to understand the why, and more.
Your choice.
|
|
|
|
|
You didn't quoted my entire message.
In fact, the question starts with an "if", so you are entering an "If" without checking the condition.
I understand that one wants to decouple (sorry bad english) the VIEW from the MODEL.. but if the VIEW already contains the MODEL (that is, a composition relationship), you already have the coupling there. As I see it, you've already losed the idea of encapsulation.. because another person working with you could type "m_model." and he would see all the public methods that allow him to modify the model directly from there (from the view). So, again, once you've decided to add the model to the view (Model m_model), I don't really get how the controller helps, in fact it makes the view to take a long road for something it could do (GIVING THAT IT CONTAINS THE MODEL) in less steps.
And btw, a pattern is not a good pattern "just because".
|
|
|
|
|
|
Quake2Player wrote: I have a very simple question:
If the View is supposed to know the Model (that is, the View has a field of type Model),
why would I need the controller if the view can modify and get responses from the model directly?
You don't necessarily. Many frameworks combine the controller and view into one, e.g. MFC's Document/View architecture.
|
|
|
|
|
Thanks,
Another question:
What would be better: That the view works only with strings/numbers or that it works with objects from the model, for example
Creating an account:
a vs b:
(a) User press a button for creating an account after filling the form -> The form sends all the data(strings primarily) with an event (CreatingAccountRequest) -> the controller listens this event -> the controller creates an account object -> adds it to the model
(b) User press a button for creating an account after filling the form -> The form creates an account object -> The form sends an account object with an event (CreatingAccountRequest) -> the controller listens this event -> the controller adds it to the model
Viewing an account:
a vs b:
(a) The form uses the Tag property of controls to contain the objects from the model (for example an Account in a ListViewItem' tag)
(b) The form uses the Tag property of contrls to contain an identifier of the object of the model (for example an Account id)
|
|
|
|
|
Quake2Player wrote: (a) User press a button for creating an account after filling the form -> The form sends all the data(strings primarily) with an event (CreatingAccountRequest) -> the controller listens this event -> the controller creates an account object -> adds it to the model
(b) User press a button for creating an account after filling the form -> The form creates an account object -> The form sends an account object with an event (CreatingAccountRequest) -> the controller listens this event -> the controller adds it to the model
I definitely like (a) better than (b) because it decouples the view from the model; the view in the case of (a) is completely ignorant of the model. If the model changes, it's less likely that the view will be affected.
|
|
|
|
|
Thank you,
Any opinion about the second thing, SHOWING/DISPLAYING ?
|
|
|
|
|
Quake2Player wrote: Any opinion about the second thing, SHOWING/DISPLAYING ?
(a) The form uses the Tag property of controls to contain the objects from the model (for example an Account in a ListViewItem' tag)
(b) The form uses the Tag property of contrls to contain an identifier of the object of the model (for example an Account id)
I'd prefer (b) for the same reason. That way the objects stay inside the model where they belong; the view has no direct knowledge of them. If they change structure at some point, it's less likely to affect the view.
This is just my opinion...
The boundaries of each subsystem, in this case the model, view, and controller, should appear as flat as possible to each other. That is to say each subsystem has a nice, flat API that the other subsystems can access. So instead of something like this, for example:
SomeObject obj = model.GetSomeObject(int id);
obj.SetValue(123);
You have this:
model.SetValue(id, value);
In the second example we have no knowledge how the model represents the values inside. All we want to do is set the values or whatever.
|
|
|
|
|
For a few reasons:
1. Separation of Concerns (SOC): The model should be what the name states--just a model. It should not have other logic. Any logic which is outside the domain model of the "model" and is used for controlling should go in the controller.
2. A pattern is to build vocabulary amongst developers. You do not have to implement it this way. And if you don't, then you are no longer using MVC so you can not tell other developers, "I used MVC pattern here but I decided to throw out the controller."
3. There are many occasions wherein designers have decided to put the controller logic within the model, for example, many .NET classes do databinding this way. DataTable sends an event when data is changed, and the DataGridView handles the event.
Your question below about option a and b, I would go with option b and pass the object over. But instead of asking the object for its state using properties, I would do the following:
Lets say I have Employee class
public class Employee
{
public string Name
{get;}
public string LastName
{get;}
public string FullName
{get;}
public int Age
{get;}
// I will have one method as below which will return the state in a structure in one shot
public EmployeeProperties GetState()
{
EmployeeProperties props = new EmployeeProperties();
props.Name = this.Name;
props.LastName = this.LastName;
props.Age = this.Age;
return props;
}
}
public struct EmployeeProperties
{
// The fields for the struct
}
There is no right and wrong way. It is like art, you might have a more elegant solution than anyone has ever had.
CodingYoshi
Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
|
|
|
|
|
Nice! Didnt thought on that solution..
Instead of having large methods with a lot of parameters... Thank you
|
|
|
|
|
Hi All,
I am planning to develop a client server application . Can anyone let me know the best way of defining the message protocol between client - server.
|
|
|
|
|
There is no best way, it depends entirely on what you need to communicate between the client and server.
|
|
|
|
|
Member 4047183 wrote: Can anyone let me know the best way of defining the message protocol between client - server.
Requirements
|
|
|
|
|
In the past I have used datatables as my data "entity". So if I wanted a list of rooms for a house I have a stored proc for GetRoomsForHouse(HouseID) and this would reside in the Rooms class.
Now with List<> I am moving to lists of objects as my data "entity". My current structure (LoadObject) is up the FK path ie Fitting inherits from Room from House from Street etc. So getting a fitting (stool) will get 4 records from the database to load the ingeritance tree, this is fine and works well if there you are working with 1 stool.
Now I want all the fittings in Room101, this potentially generates 4 x #n fittings calls to the database to load the List<fittings>. This is still reasonable but when I want a list of all fittings in 60 storey tower block it gets a little overwhelming.
In the past I would simply have a proc to get all fittings by a number of different criteria and use the datatable to load the display controls. So I have a number of option to follow:
1. Continue with the proc to datatable - this precludes using a List<> to populate data list controls
2. Use the LoadObject method and wear the database calls - worst case and will not be implemented
3. Use a proc to get the data IE Fittings RoomName,HouseNo,StreeName etc and create a class to service a List<fittingsforxxxx> with the results from the proc.
4. Split my design of an object to display/process where display gets only the data used to load into list controls. The process object would be a full inheritance tree of a single object (Fitting).
I want to use 3 but it means there will be a plethora of classes in the app to support display. 4 may be the better option as it standatdises on the object rather than the proc.
The amount of code is not an issue as I will use my code generator to build it all.
2nd issue
When dealing with lists of objects should the list reside in the parent or the child eg:
Room has a List<fittings> and while the select for the List is on Fittings the List is on Room - Parent has the list.
Fittings has a List<fittings> for a Room and retains the List on the Fittings object - Child has the list.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I don't understand your question. Is your question basically that you know how to load one object from the database but loading multiple objects seamlessly from the database (or any storage) you are having trouble with?
CodingYoshi
Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
|
|
|
|
|
I thought I had wasted my time with this question.
No I have no problem managing the data it is the design I am tossing around. When dealing with a list of something do I create the list in the something class or in the parent class.
2 classes, Department and staff. I now want a list of staff for the department. Is the best design I am after.
Departmant
List of staff for department(this.DepatmentID)
Staff
Get for Department(DepartmentID)
Or both, hah thats probably the right way. Staff gets the list for a department because it is a list of staff and department calls the same method on staff to get the list of staff for itself.
The problem is I use stored procs ALL the time so there would be a proc out there called StaffGetForDept(@DeptID) and deciding on the owner class for the proc is sometime an issue.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I don't like the idea of asking the Staff object to return a list of Staff objects. A staff should have methods and properties for a Staff but not a list of Staff. I would take one of the collection classes (if using .NET or something similar if using else) and extend it like below:
public class StaffCollection : List<Staff>
{
private Department _department;
// Inject the object with the department at construction
// Now this staff collection realizes its department
public StaffCollection(Department dept)
{
this._department = dept;
}
public void Load()
{
// Load this from the database. I will create a DAL and pass a reference of this to it so it can load it for me.
}
// You can put many other methods--based on your requirements--here. For example, find all employees who earn more than 100K and it will return a bunch of employees who wear pink shirts and have spiked up hair
}
Here is the department class
public class department
{
// put everything for department here: email, name etc.
private StaffCollection _staffs;
public Department()
{
this._staff = new StaffCollection(this);
}
public void LoadStaff()
{
// You can check first if already loaded or just reload everytime
this._staff.Load();
}
}
You will still need a Staff class and might need a DepartmentCollection class.
CodingYoshi
Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
|
|
|
|
|
CodingYoshi wrote: I would take one of the collection classes
Now that makes sense, I have no collection classes in my design, all the lists (I usually use datatables) come from the single class, this is b/c of my reliance on datatables (and recordsets of course) in the past.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi,
I think I've got a god object antipattern problem, or not, please tell me:
I have a class Admin that contains several lists of things that might interact each other,
for example: It contains a list of Messages, Accounts, Contacts, Filters etc.
The problem is that some of them interact with each other.. for example, the AddMessage method applies all the filters in the filters list.
The Admin class is pretty big.. because it haves Adds methods, Deletes methods, ..so it has like 2*(fields that are objects or lists of objects) methods
Do i have a god class or is it well justified?
In case I do have a god class, should I split the Admin class in several Administrators? like AccountsAdmin, ContactsAdmin, FiltersAdmin, ..? or what?
Thanks
|
|
|
|
|
I would have an Admin PROJECT and a class for each of your entities (accounts, contacts etc)
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
You didn't get the problem.
I DO have a class Account, Message, Filter, etc.
|
|
|
|