|
The biggest problem with singletons lies in the fact that it reduces parallelism. As you only have a single instance present, any threaded operations must be serialized in and out of the singleton, reducing the efficiency of the threading. They can also make it harder to unit test code because it can introduce global state.
|
|
|
|
|
i see, this really helps, in my case I've global state (the list of users), and i've to synchronize the access, so this wont help... i think i'll just rewrite this to use normal instances and a single shared user list, apparently, is the best solution at the moment, i'm working on this with a plenty of beginners and the only thing that will be sent to the appraiser, thanks
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)
|
|
|
|
|
Pete O'Hanlon wrote: The biggest problem with singletons lies in the fact that it reduces
parallelism. As you only have a single instance present, any threaded operations
must be serialized in and out of the singleton, reducing the efficiency of the
threading. They can also make it harder to unit test code because it can
introduce global state.
I don't agree.
The biggest problem with singletons is incorrect usage and from that overusage.
For your points...
For the first problem sometime the very nature of the need dictates serialization anyways. And in other cases there are ways that can allow multiple access.
As for the second problem there are trivial solutions for most problems of that nature, for example just implementing a Reset() method.
|
|
|
|
|
jschell wrote: For the first problem sometime the very nature of the need dictates
serialization anyways.
In that case, you shouldn't be parallelising the code at this point. This concern was raised specifically for the case where you have a parallel architecture that has to be throttled because of a singleton.
jschell wrote: As for the second problem there are trivial solutions for most problems of that
nature, for example just implementing a Reset() method.
So, we're talking about introducing a method just to get through a test; not for any other purpose.
I think you misunderstood my point. I wasn't arguing that you should never use a singleton; I was just showing what the weaknesses are. If you are comfortable that you understand the pattern sufficiently well, and that you know exactly how your code is going to be used, then you can make an informed decision. If you don't know what the weaknesses are, then you can end up causing all sorts of problems for yourself.
|
|
|
|
|
Pete O'Hanlon wrote: In that case, you shouldn't be parallelising the code at this point. This
concern was raised specifically for the case where you have a parallel
architecture that has to be throttled because of a singleton.
Not sure what you are referring to.
I took your response to suggest that you were addressing the Singleton Pattern, and not one specific instance of that.
Pete O'Hanlon wrote: So, we're talking about introducing a method just to get through a test; not for
any other purpose.
Yep.
|
|
|
|
|
They artificially restrict your ability to change your mind later, and don't really provide any benefit. You can always just instantiate one copy of an object if you only need one!
If you have a need for a single static instance of some stuff, have a static reference to it.
static class GlobalData {
public static ContactListManager ContactListManager { get; private set; }
static GlobalData(){
ContactListManager = new ContactListManager("datastore.xml");
}
}
That way, if you discover that you want to manage disparate contacts across several data sources and therefore it shouldn't be a singleton any more, it's much easier to change. It means that if you want a temporary separate instance for some reason (e.g. importing external data), you can do so.
|
|
|
|
|
Hi,
I have an 'in.xml' file which I would like to 'collapse' into an 'out.xml' file in c#. (Not necessarily asking for the code but the approach to do this ie. xslt or xmldocument add attributes) Many thanks in advance.
The files are as follows:
in.xml
<mySample xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<Phase1>
<startDate>2013-01-01</startDate>
<endDate>2014-12-31</endDate>
<capital>100000.00</capital>
<fundings>
<funding>
<registrar>Genentech Pty Ltd</registrar>
<assets>100000.00</assets>
<miscellany>150000.00</miscellany>
</funding>
<funding>
<registrar>Acme Pty Ltd</registrar>
<assets>25000.00</assets>
<miscellany>25000.00</miscellany>
</funding>
</fundings>
</Phase1>
<Phase2>
<capital>150000.00</capital>
<fundings>
<funding>
<registrar>Mont Blanc Associates</registrar>
<assets>50000.00</assets>
<miscellany>25000.00</miscellany>
</funding>
<funding>
<registrar>Acme Pty Ltd</registrar>
<assets>10000.00</assets>
<miscellany>10000.00</miscellany>
</funding>
<funding>
<registrar>Triple A Commercial</registrar>
<assets>10000.00</assets>
<miscellany>10000.00</miscellany>
</funding>
</fundings>
</Phase2>
<integration>
<value>6</value>
</integration>
<years>
<value>1</value>
<value>2</value>
<value>6</value>
<value>3</value>
<value>7</value>
<value>4</value>
<value>8</value>
<value>5</value>
</years>
<bandWidths>
<value>1</value>
<value>2</value>
</bandWidths>
<regions>
<region>
<name>Trevae, Srt, 3241</name>
<state>3</state>
<zodex>3241</zodex>
</region>
<region>
<name>Silua, Rev, 2940</name>
<state>2</state>
<zodex>2940</zodex>
</region>
</regions>
<sponsors>
<sponsor>
<name>Genentech Pty Ltd</name>
<spcode>7</spcode>
</sponsor>
<sponsor>
<name>Acme Pty Ltd.</name>
<spcode>15</spcode>
</sponsor>
<sponsor>
<name>Triple A Commercial</name>
<spcode>9</spcode>
</sponsor>
<sponsor>
<name>Mont Blanc Associates</name>
<spcode>1</spcode>
</sponsor>
</sponsors>
</mySample>
out.xml
<mySample xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<Phase1 startDate="2013-01-01" endDate="2014-12-31" capital="100000.00">
<fundings>
<funding registrar="Genentech Pty Ltd" assets="100000.00" miscellany="150000.00"/>
<funding registrar="Acme Pty Ltd" assets="25000.00" miscellany="25000.00"/>
</fundings>
</Phase1>
<Phase2 capital="150000.00">
<fundings>
<funding registrar="Mont Blanc Associates" assets="50000.00" miscellany="25000.00"/>
<funding registrar="Acme Pty Ltd" assets="10000.00" miscellany="10000.00"/>
<funding registrar="Triple A Commercial" assets="10000.00" miscellany="10000.00"/>
</fundings>
</Phase2>
<integration value="6"/>
<years value="1,2,6,3,7,4,8,5"/>
<bandWidths value="1,2"/>
<regions>
<region name="Trevae, Srt, 3241" state="3" zodex="3241"/>
<region name="Silua, Rev, 2940" state="2" zodex="2940"/>
</regions>
<sponsors>
<sponsor name="Genentech Pty Ltd" spcode="7"/>
<sponsor name="Acme Pty Ltd" spcode="15"/>
<sponsor name="Triple A Commercial" spcode="9"/>
<sponsor name="Mont Blanc Associates" spcode="1"/>
</sponsors>
</mySample>
----------------------------------------------------------
Lorem ipsum dolor sit amet.
|
|
|
|
|
An xslt would be a simple way to achieve this with a well defined schema. If you're not comfortable with xslt though, you could easily do this with something like XLinq as well.
|
|
|
|
|
Thanks.
Do you agree the following summarizes what I am trying to achieve?
Unless the child element has a child element of its own, make it an attribute of the parent.
----------------------------------------------------------
Lorem ipsum dolor sit amet.
|
|
|
|
|
It certainly looks like it.
|
|
|
|
|
Yeah, time to practice some XSLT-Fu.
|
|
|
|
|
i hate you. your reply is funny but limited in usefulness.
----------------------------------------------------------
Lorem ipsum dolor sit amet.
|
|
|
|
|
I disagree. I feel his post was the most usefull of all, instructing you to increase your skillset. This would have the effect of you being able to solve this particular problem in a far more efficient method than posting a question in a discussion forum.
|
|
|
|
|
Dave Kreskowiak wrote: increase your skillset
Actually, I assumed he was already at least somewhat familiar with XSLT; he seems to know what it is and that it is applicable to the situation.
I think what I meant to imply was that he should maybe become more confortable with XSLT.
Oh, and OP: we do have an XML/XSLT forum if you need assistance group therapy.
|
|
|
|
|
I was just joking around. Either way, it wouldn't hurt to increase his skillset anyway.
|
|
|
|
|
swjam wrote: funny but limited in usefulness
Ah, good, you know XSLT already.
|
|
|
|
|
I thought the I hate you was more indicative of XSLT kowledge!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
To quote Billy Joel: "You may be right."
|
|
|
|
|
Hello,
i have the following code, which redirects the
output from a unmanged dll into a file:
[DllImport("Kernel32.dll", SetLastError = true) ]
public static extern int SetStdHandle(int device, IntPtr handle);
FileStream filestream;
StreamWriter streamwriter;
void Redirect()
{
int status;
IntPtr handle;
filestream = new FileStream("logfile.txt", FileMode.Create);
streamwriter = new StreamWriter(filestream);
streamwriter.AutoFlush = true;
Console.SetOut(streamwriter);
Console.SetError(streamwriter);
handle = filestream.Handle;
status = SetStdHandle(-11, handle); // set stdout
// Check status as needed
status = SetStdHandle(-12, handle); // set stderr
// Check status as needed
}
But I don't want to write the output into a
file.
The output should be written into a winform-control
or a memorystream.
The dll runs in a different process.
The dll ist a "black box" for me,
so I can not modify the source-code.
How can I do that?
Regards
Andreas
|
|
|
|
|
|
Your code is using Console output so it will only go to a file handle, stdout , or whatever that is redirected to. If you want it to go to your form or memory stream then you need to change your code to use a streamwriter [^].
Programming is work, it isn't finger painting. Luc Pattyn
|
|
|
|
|
have you tried to change filestream = new FileStream("logfile.txt", FileMode.Create); to something like filestream = new MemoryStream();
(before you ask, YES, you need to change the declaration of filestream...)
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)
|
|
|
|
|
Hello,
i hope, i got some helpfull information here .
I got an industrial camera device and want to generate a stream which could be shown by the windows mediaplayer activeX control on an other device in the same network.
It's just for monitoring the images made by the camera on a HMI of our mashine.
Do you got any hints for me, how i could realize this?
I think i just need some starting information. ^^
like "use System.IO.Stream and do ..."
not a whole solution ...
I got the Images every time a hardware trigger occurs. The camera framework provides methodes to save it as png to disk. But I want to broadcast it directly to a stream on which the HMI can connect or just show.
|
|
|
|
|
It's just an idea:
If you already have a png-file you could use an internet browser as client to view the image.
I would create a simple html file containig the image with automatic reload tag for the page.
You could access the html-file via file sharing or you implement a little http-server. The HttpListener[^] might help you.
[Edit]
It is not the way you wanted (Mediaplayer...) but I think it's a solution according to your requirements
[/Edit]
|
|
|
|
|
i'm not sure about the possibilities in the HMI Designer software to integrate a html side.
to view the html file, i need a browser, right?
or a built in control in the HMI software, which shows html files....
but i think, there was something like that...
will have a look on it ...
Thanks for the tip
|
|
|
|