|
This question is more about programming methedology than anything else.
The question is simpily which is better out of the following programming methods:
This is a C# project.
There are five different classes in the project.
There are 2 classes that that need to be used by other classes. There may be more classes used by others in the future.
Communications method 1:
Any class that needs to use another class, simpilly calls methods from the other class. The class methods are called from are passed into the first class when it is instanced.
e.g.
Class A = class that writes to files.
Class B = class that needs to use files.
so when creating class B
private B beta = new B(A);
Communications method 2:
There is an overall Communications class.
Every class that needs to call upon methods in another class is required to use the comms class to do so.
e.g.
Class A = class that writes to files.
Class B = class that needs to use files.
Class C = the communications class.
so while in class B,
c.comms(<class to="" talk="" id="">,<items passed="" in="">);
Which would you use?
Or would you use something else?
(If this is the wrong place, feel free to tell me)
Munty
|
|
|
|
|
How about letting any class just create objects from the class that it needs?
---
b { font-weight: normal; }
|
|
|
|
|
The 2 classes may not need be as tightly bound as you may think.
There is really no reason to combine them.
In .NET there are 2 separate classes that may have similar purpose as yours and are separate:
StreamReader and StreamWriter.
|
|
|
|
|
Munty
What you are stumbling towards is a controller class, which is commonly used as part of an MVC (Model/View/Controller) pattern. This means that whenever you need to do something, you call the controller and let it decide how to accomplish the task.
Suppose, for instance, that class A is responsible for writing to a file and that class B can only read from the file when it is closed. Now, you could end up coding this so that you write to the file and then, when you need to read the file, you will have to check to see if the file is closed or not. This is fairly simple, and you could place the code to check the file handle in class B or in the code that is calling class A.
Unfortunately, this means that you have an implicit dependency in class A on class B, which means that every time that you want to change the behaviour of B, you have to change the behaviour of A. This is OK(ish) if you have the code to both A and B, but if somebody else has coded them, this is not great.
A better method is to move the controlling logic into the controller class C. This way, you have decoupled the dependencies.
In short - read up about patterns. They will serve you well.
Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world."
Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that."
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Pete,
At the moment, what actually happens is something like this:
Class A, B and C are seperate mainly for UI reasons (They cover totally different areas of the project)
Class A, B, C all need to edit files. All have roughly similar requirements, so I moved all the file editing things into Class D, so I wouldnt have to copy and paste a couple of hundred lines around.
Class D is what you use to open up the GUIs for classes A, B and C.
The reason behind my question is that last night I suddenly thought that it would be better if I had a dedicated communications class, incase I needed to edit anything (e.g. add methods, change methods, etc).
Anyways enough of the background.
Two more things to ask:
1) Any thing you recommend as reading materials?
2) Do you see anything majorly wrong with the communications acting something like this?:
Communications Class and all other classes are instanced by a master class.
Class A calls:
Controler.talk(ID<s>,Object)
Controller class:
gets object. depending on ID does different thing to object.
e.g. converts object to String[]
sends modified object (e.g. String[]) to method in specific class (as identified by ID)
waits for response from method. (If method is not void)
Returns response as object.
--
Yeah it does mean forcing conversions from Object to whatever you need both in the communications class, and when it's returned to the original class, but it seems the best way to do it to me, rather than having a bunch of methods in the communications class that take in different sets of variables.
Edit:
Oops, accidentially made it strikethrough.
-- modified at 11:34 Thursday 26th October, 2006
|
|
|
|
|
I don't see anything wrong with this approach - hence my comments about the MVC pattern. Basically, this is the pattern you seem to be following with all commands, etc, routed through the controller. The controller is responsible for updating the model and informing the view(s) that it needs to change.
I would recommend the classic GoF (Gang of Four) book on patterns and then search amazon for a book on C# patterns.
Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world."
Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that."
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Thanks for answering my questions.
Gah! The GoF book is expensive on Amazon, but ah well, it has been cut by 34% and it will probably be useful next year too.
Munty
|
|
|
|
|
Seeing that you live in the UK, you could always try Computer Manuals
Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world."
Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that."
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Cheaper on Amazon currently
(Though I'm bookmarking the other site)
|
|
|
|
|
|
Pete O'Hanlon wrote: an MVC (Model/View/Controller) pattern
That's a five!
led mike
|
|
|
|
|
Thanks.
Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world."
Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that."
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hi All,
I need to do correct https request and get the Response Page.
So I have used Msxml.dll for this.
My code is given below:-
MSXML.XMLHTTPRequestClass lobjRequest = new XMLHTTPRequestClass();
lobjRequest.open("POST","https://test.abc.com?jsessionid="+_strSession, false, null, null);
lobjRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
string lstrRequest="Mime Type=application/x-www-form-urlencoded"+
"&user="+lstrUserName+
"&password="+lstrPassword+
"&page=%2Foctxml%2FIndexPage.jsp"+
"&title=TXT0969";
//lobjRequest.send(null);
lobjRequest.send(lstrRequest);-->Error Occured after this..
This is working fine for http request Pages.But when I execute the program ,I have taken the following error.
The download of the specified resource has failed
So Please kindly tell me to solve this problem.Can any one Please tell me to do correct https request and Get Correct Response using C#.Net?
Thanks in advance
|
|
|
|
|
pubududilena wrote: using C#.Net?
The HttpWebRequest class provides support for the properties and methods defined in WebRequest
and for additional properties and methods that enable the user to interact directly with servers using HTTP.
Do not use the HttpWebRequest constructor. Use the System.Net.WebRequest.Create method to initialize
new HttpWebRequest objects. If the scheme for the Uniform Resource Identifier (URI) is http:
Create returns an HttpWebRequest object.
http://msdn2.microsoft.com/en-gb/library/system.net.httpwebrequest.aspx[^]
led mike
|
|
|
|
|
Hi,
Thanks for reply.
Suppose that in the response ,there are many javascript functions.So after the javacript row ,I can not see response completely.I think that JavaScripts are not working when I request like this.So can you kindly tell me is there a way to execute javascripts and get complete response?.
For Ex:-
In a Tag there is JavaSripts like this:-
buildAvailabilities(startcity,endcity);..
This is not execute in the response page.
Thanks in advance.
|
|
|
|
|
pubududilena wrote: This is not execute in the response page.
Umm yes because the browser executes it and you have no browser in this case. It seems you might have counted on some "magic" occurring when you planed your technical solution. Magic is not a part of software solutions. You really should do some investigating into these technologies for the purpose of understanding them before you embark on a project like this.
led mike
|
|
|
|
|
Can you help me, I have had to decompile some code as they have lost the code and the programmer who created the code is no longer with the company.
Not sure how the code look like before it was compiled and i have not done any work on Anonymous Methods, which i belive it was.
Can someone help me?
<br />
public class UpdateController<br />
{<br />
[CompilerGenerated]<br />
private void <BeginUpdating>b__0()<br />
{<br />
this._updating--;<br />
}<br />
<br />
public IDisposable BeginUpdating()<br />
{<br />
this._updating++;<br />
return new DisposeHelper(new DisposeHelper.DisposeDelegate(this.<BeginUpdating>b__0));<br />
}<br />
<br />
public bool NotUpdating<br />
{<br />
get<br />
{<br />
return (this._updating == 0);<br />
}<br />
}<br />
<br />
<br />
private int _updating;<br />
}<br />
<br />
public class DisposeHelper : IDisposable<br />
{<br />
public DisposeHelper(DisposeDelegate dispose)<br />
{<br />
this._dispose = dispose;<br />
}<br />
<br />
public void Dispose()<br />
{<br />
this._dispose();<br />
}<br />
<br />
<br />
private DisposeDelegate _dispose;<br />
<br />
<br />
public delegate void DisposeDelegate();<br />
<br />
}<br />
Thanks...........
|
|
|
|
|
Yes, it is anonymous methods, but what is your question? You can easily run this through the debugger and be able to trace the fairly simple process.
|
|
|
|
|
i cant get the code to compile?
|
|
|
|
|
What errors do you get?
Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world."
Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that."
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Are you using .NET Framework 2.0?
|
|
|
|
|
I'm using .net 2.0 and VS 2005.
When i try and build the code i get the gfollowing errors
Error 1 Invalid token '<' in class, struct, or interface member declaration C:\C# Code\UpdateController.cs 9 22 test
Error 2 Invalid token '>' in class, struct, or interface member declaration C:\C# Code\UpdateController.cs 9 36 test
Error 3 Class, struct, or interface method must have a return type C:\C# Code\UpdateController.cs 9 37 test
Error 4 Identifier expected C:\C# Code\UpdateController.cs 17 77 test
Hope this helps
|
|
|
|
|
|
Hi,
I want to know about the stringbuilder and its use. Can it be used to read a text file line by line for further processing. Right now i m using array to read the whole contents of a text file and then processing it line by line using the array index. Is it possible by using stringbuilder. It is required to process each line in the text file seperately.
Thanx in advnace
|
|
|
|
|
stringbuilder can be used where string concatenation occurs.
|
|
|
|