This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi I'm using Lightspeed 5 in an MVC5 project that includes a Web API controller. I'm just getting started with Web API so I want to know the syntax for making use of my Lightspeed unit of work from within the Web API controller. The project is working fine in terms of using Lightspeed from within the MVC controllers by having each controller inherit from a base controller that inherits LightSpeedControllerBase. eg.
What is the equivalent of the above for a Web API controller? (I'm assuming that I need to create a baseWebAPIController that inherits a Lightspeed Web API controller base - correct?) Many thanks. |
|
|
We don't have an out of the box base controller for Web API. You can either use explicit scoping in each method where required e.g.
Or if you want to have the UnitOfWork as an accessible property you could either create your own base or implement it on your controller by having the UOW scoped at the request level by having it stored in Request.Properties, e.g.
|
|
|
Hi Jeremy, thanks for your reply. If I use your first example, what is the parent class for "context"? (As in context.CreateUnitOfWork()) That object isn't available within my Web API controller so I'm wondering what base class I need to import. Thanks. |
|
|
Ah yep, that refers to an instance of LightSpeedContext or probably more usefully LightSpeedContext So create one e.g.
where LightSpeedContextName refers to the named context in configuration to use for the configuration settings.
|
|
|
Hi Jeremy, Is the context safe to share between request? I have projects where the datalayer ORM classes are hidden from the rest of the project, the business layer just returns pure POCOs. In these projects the business layer is used in both multithreaded web and single threaded desktop envrionments. If I keep a static Lightspeed context reference and then per database action (say create customer, update customer, delete customer) and use the static context to create units of work will I be safe? Thanks, Frank |
|
|
The context can be shared as it just represents configuration info however if you do change some of the values on it (e.g. Logger, Cache) then this would be applied to all UnitOfWork instances which have been created by that context and this could cause unexpected behaviour. The context itself is not specifically thread-safe so if you were modifying any values on the context (e.g. a connection string) you would want to do so with some locking applied if you need to serialise the writes. Typically for a web app you would have a single static context instance available (either a singleton or a single injected instance) for creating new UnitOfWork instances from.
|
|