{"A duplicate value cannot be inserted into a unique index. [ Table name = TESTRECORD,Constraint name = PK_TEST__000000000000030E ]"}

Topics: Writing modules
Jul 29, 2012 at 10:58 AM

I use a controller as Restinterface.

 

The Service which is called by the controller is ISingletonDependency

 

on every call into the service i write data into my testtable.

_myService.Value.CreateItem((myTYpe)baseObject);

if i call the controller from multi "clients" in parallel i get a constraint exception.

 

{"A duplicate value cannot be inserted into a unique index. [ Table name = TESTRECORD,Constraint name = PK_TEST__000000000000030E ]"}

what can be the reason and how can i prevent this?

 

Coordinator
Jul 29, 2012 at 12:36 PM

Do not write singletons unless you absolutely have to, which you most probably don't.

Jul 29, 2012 at 1:31 PM
Edited Jul 29, 2012 at 1:32 PM

but i have to since the service has a list and every call is saved to that unique list.

on the one hand i write it to database on the other side i keep the info in the list

Coordinator
Jul 29, 2012 at 1:37 PM

That doesn't mean you need a singleton. Singletons are bug farms. The list of content items is a unique list. Is it managed like a singleton? No. What you have to manage here is at best concurrency, and singletons are a horrible way to do that.

Jul 29, 2012 at 2:03 PM
Edited Jul 29, 2012 at 2:16 PM

actually i dont get what you mean. where do i find the example you suggest?

 

i think i need a singleton since i want to exist only one list with all items in there.

 

e.g. you want to listen with that serviceadditionally on a tcp port for incoming event. how will you handle it if its not singleton?

Coordinator
Jul 29, 2012 at 3:13 PM

Orchard is a content management system. It manages content items. The list of content items is a unique list, exactly like yours. But it would be madness to manage it from a singleton. I'm suggesting that you have no more reason to use a singleton here than we would for content items. There is absolutely no reason why your service class would need to be a singleton. Instead, you should fetch this list from the database as needed, and apply caching as necessary.

Singletons are evil.

Jul 29, 2012 at 7:40 PM
Edited Jul 29, 2012 at 8:05 PM

orchard.codeplex.com:

 

Orchard is a free, open source, community-focused project aimed at delivering applications and reusable components on the ASP.NET platform. It will create shared components for building ASP.NET applications and extensions, and specific applications that leverage these components to meet the needs of end-users, scripters, and developers. Additionally, we seek to create partnerships with existing application authors to help them achieve their goals.

 

Okay i understand now that I shall use the database for that case.

but what with a service that is waiting for an incoming event triggered via tcp?

Coordinator
Jul 31, 2012 at 4:21 PM

Same thing: caching is your friend.