|
I couldn't find a good resouce that gives the introduction on about Provider Model, what is it used for and how can one build. Does anybody have a usual link. I searched but getting more confused seems like its a very detail topic but i want to learn first about the basic idea and then drill down in detail afterwards.
|
|
|
|
|
I am building a chat with file transfer support. I use normally the StreamReader and the StreamWriter built over the NetworkStream, but when i want to send data from a file i would like to use NetworkStream directly, to write bytes without encoding/decoding. If i don't use direcly the NetworkStream (convert all my byte[] to char[], then sending, and converting back after receive) things goes fine.
...
NetworkStream networkStream = socketForServer.GetStream();
StreamReader netStreamreader = new StreamReader(networkStream);
StreamWriter netStreamwriter = new StreamWriter(networkStream);
...
//The writer program:
byte[] buf = ...
netStreamwriter.WriteLine(someString);
netStreamwriter.WriteLine(buf.Length);
netStreamwriter.Flush();
networkStream.Write(buf, 0, buf.Length);
networkStream.Flush();
...
//The reader program:
myString = netStreamreader.ReadLine();
size = Convert.ToInt32(netStreamreader.ReadLine());
byte[] buf = new byte[size];
networkStream.Read(buf, 0, size);
...
This does not work, oh it does for the first times (randomly each run) and then unexpected happens, it cuts parts and reader program reads thrashes because of desincronization with the sending program. Is there another way of sending/receiving raw data ? What am i missing...
|
|
|
|
|
OK, I have a class "Dog" with methods play() and Euthanize(). I have a class Child that must be able to "Play" with "Dog", but not "Euthanize" it. I have a class "Vet" that must be able to "Euthanize" "Dog".
I do not want to isolate "Dog" and "Vet" in their own assembly and make "Euthanize" an internal method of "Dog". I also don't want to nest Dog in Vet.
So, How can I enforce these relationships. I do not trust the child to not play vet and "Euthanize" dog, so it must be IMPOSSIBLE for the Child to call "Euthanize" on "Dog".
I don't know if simply giving the child an IPlayable iterface and the Vet an IEuthanizable interface will be restrictive enough as Child could cast IPlayable to Dog and thereby access its "Euthanize" method.
You used to be able to accomplish this with "Friend" classes in C++. There are no friend classes in C#. Am I forced to either put "Vet" and "Dog" in the same assembly and make Dog.Euthanize internal or to nest "Dog" in "Vet" (which doesn't make sense from an object-oriented standpoint because I want classes "Pound" and "PetStore" to be able to create "Dog" objects without any knowledge of the "Vet" class).
Any ideas?
Thanks!
Ian
|
|
|
|
|
Simple. Euthanize() is not a method of Dog , but is a method of Vet , where you pass in a parameter of type Dog . Actually, a better model is if you pass in a parameter of type IAnimal where Dog implements IAnimal . That way, the Vet can't Euthanize(Child) , as much as he/she may want to.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Does that also prevent the child from doing whatever the vet can do?
if have the vet as:
vet.Euthanize(IAnimal)
and
child.Play(IAnimal)
how do you prevent the child from 'playing Euthanize' with the dog?
Maby I have now lost the concept?
|
|
|
|
|
Your question doesn't fit the code you posted. Unless you code Play to kill the dog, nothing will happen. There is no such thing as PlayEuthanize unless you code the child class to be a sociopath.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Dave Kreskowiak wrote: Simple. Euthanize() is not a method of Dog, but is a method of Vet, where you pass in a parameter of type Dog. Actually, a better model is if you pass in a parameter of type IAnimal where Dog implements IAnimal. That way, the Vet can't Euthanize(Child), as much as he/she may want to.
I thought about this approach as well. Euthanize() would seem more appropriately placed in the Vet class, I agree. However when a Vet euthanizes a Dog , there needs to be some way to mark the dog as, um, dead. Whatever means a vet has to do this must be hidden from a child.
My thought is that judiciously used interfaces should do the trick here.
|
|
|
|
|
I understand what you are saying, but the vet still has to interface with the dog to kill it, right? What prevents the child from interfacing with the dog in the same manner the vet interfaces it?
|
|
|
|
|
Edmundisme wrote: I don't know if simply giving the child an IPlayable iterface and the Vet an IEuthanizable interface will be restrictive enough as Child could cast IPlayable to Dog and thereby access its "Euthanize" method.
This will sound trite, but the simple answer is to make sure that your Child code doesn't do that.
Interfaces are good for enforcing the kind of contraints you've described; they limit how the outside world views an object thereby constraining what actions you can perform on them. However, if someone (another programmer) is going to abuse the contract you've given them by only exposing your objects through a limited interface... well, they've broken the contract and all bets are off.
Other than that, you could have some type of key that has to be passed to the Euthanize method. Only a vet would posses such a key, so in theory only a vet could euthanize a pet. Just make sure that the key is hidden from the child.
I'm curious about this problem, however, and will enjoy reading other answers. Hopefully, they will be more helpful.
|
|
|
|
|
Yeah, I was afraid of that. I've been wondering if I've been trying too hard to solve a problem that doesn't need solving. Perhaps using interfaces is enough.
It's interesting that you suggested using a "key". I suggested this option to one of my coworkers but it sounded a bit strange. So, either it's not a strange solution or I'm not the only one with strange ideas! Is there a pattern that describes this solution?
|
|
|
|
|
Edmundisme wrote: It's interesting that you suggested using a "key". I suggested this option to one of my coworkers but it sounded a bit strange. So, either it's not a strange solution or I'm not the only one with strange ideas!
Edmundisme wrote: Is there a pattern that describes this solution?
I'm not aware of any pattern, but one may exist.
I was thinking, though, if you want to model this so that it's more inline with the Vet/Pet metaphor, maybe instead of some generic "key," you would use a "medicine" object that is passed to Euthanize. And only a vet would have access to the medicine. But the role of the "medicine" object would be the same as a key: An object not obtainable by a child that must be passed to the Euthanize method.
Anyway, erm, as you can see, I'm not at a loss for strange ideas!
|
|
|
|
|
We are actually using a type like you suggest as the key, although it's difficult to explain the details in the pet/vet world because my problem doesn't exactly fit the metaphor. Thanks for your ideas!
|
|
|
|
|
Edmundisme wrote: because my problem doesn't exactly fit the metaphor.
Obviously since a Child would have a Dog instance not an IPlayable instance. In your real problem the Child would have an IPlayable instance and "should not" cast it to Dog since it could be a Cat. Upcasting is indicative of bad design. Languages do not protect against bad design or bad use. A Client should "read" a socket not "close" it, but nothing stops the developer from writing bad code that closes the socket, then he posts a message on CodeProject "Help Urgent why my code not work".
Edmundisme wrote: We are actually using a type like you suggest as the key
I would not advise a Rube Goldberg[^] design as an attempt to thwart bad developers from abusing a good design. Two wrongs don't make a right.
led mike
|
|
|
|
|
Although I agree this approach seems strange, what makes it "wrong"?
Also, this wouldn't be a problem if C# had friend classes or allowed you to make private types at the namespace level. Any idea why the CLR does not allow friend classes or private namespace types?
|
|
|
|
|
To elaborate on this approach, what I am doing matches this metaphor:
The factory class DogFactory has several methods:
GetLab()
GetPoodle()
GetCocker()
GetMutt()
This factory class encapsulates the instantiation of the classes:
class Lab : Dog
class Poodle : Dog ... etc.
Each Dog-derived class requires an IVet reference during construction. I've nested a private Vet class (implements IVet) in the DogFactory class and DogFactory has an instance of this private nested class. When a method is called to get a dog, the factory passes its instance of Vet (implements IVet) to the Dog-derived class's constructor:
Public GetLab()
{
return new Lab(this.vet);
}
By automatically managing the relationship between the dog and the vet, much vital logic is removed from the engineer's list of responsibilities. So I really want the factory to be used to create Dog objects. The engineer is not prevented from implementing IVet and creating a Dog object without using DogFactory, but because he would have to implement his own vet, he is less likely to do this. Even if he decides to implement his own vet, he will by this time have become aware of the DogFactory and will not be coding ignorantly.
Thoughts?
|
|
|
|
|
Edmundisme wrote: but because he would have to implement his own vet, he is less likely to do this
Less likely to do it than cast the IPlayable to a Dog and then cast that to an IEuthanizable then call the Euthanize() method if he doesn't want to kill the dog? I don't think so. You have overly complicated (Rube Goldberg) the design to arrive at an arguably nominal barrier to improper use.
Your continued use of the Dog/Vet metaphor is not helping. I thought we already established that in your real problem the Child would not have a Dog but an IPlayable. If that is not the case then my posts are probably not relevant.
led mike
|
|
|
|
|
I have reposted an explanation of my problem and solution in this forum because I want to hear what others might say about it. I have removed the metaphor and explained the actual scenario.
Have a peek if you're interested. I would love to hear your take.
Thanks!
|
|
|
|
|
Does anyone know if when I Load an assembly using Reflection I also have to load all of it Dependencies?
This is the behavior I am currently seeing – but was hoping things would be a bit simpler.
Cheers
Rich
|
|
|
|
|
AJ123 wrote: Does anyone know if when I Load an assembly using Reflection I also have to load all of it Dependencies?
Yes. You could try using ReflectionOnlyLoad[^] /ReflectionOnlyLoadFrom instead, to load just that assembly.
|
|
|
|
|
I want 2 change hue saturation of gif image how can I do it??? Can anybuddy help me??????????
THANX
|
|
|
|
|
Hello, I am the creator of a game called SharpKonquest (http://sourceforge.net/projects/sharpkonquest). I want to create a new version that support multiplayer through Internet, but I dont know how do this. I have think to create a TCP connection between server and client, but I don't know how synchronize the clients with the server. Exist some methodology or some class or tutorial that can help me?
Thanks
-----------------
Recuerda, no hay arma mas mortífera que una palabra brotada de un corazón noble, y un par de huevos que la respalden.
|
|
|
|
|
I haven't used this much so it might so I need some help on it.
I have a xml file and its schema file "xsd".
I can load the schema and load the xml but
I am only interested in 6 elements out of the xml file,
and there are a ton of different elements in the xsd file.
Here is the question:
Can I create a new xsd file with only the elements I am interested in and only read those elements out of the xml file?
God Bless,
Jason
Programmer: A biological machine designed to convert caffeine into code. Developer: A person who develops working systems by writing and using software.
[ ^]
|
|
|
|
|
What do mean by load xml and load schema?
You can use a XSLT to extract the elements of interest and as long as the other elements are optional in the schema you could use it to validate the xml.
only two letters away from being an asset
|
|
|
|
|
Mark Nischalke wrote: What do mean by load xml and load schema?
I can create a new XMLReader with its schema and 'XMLRead.Read()' the entire file into an object.
Thanks for the hint on XSLT that looks like what I needed.
God Bless,
Jason
Programmer: A biological machine designed to convert caffeine into code. Developer: A person who develops working systems by writing and using software.
[ ^]
|
|
|
|
|
The correct way would be to define the schema file in such a way as the elements that you do not need are optional.
File Not Found
|
|
|
|