1

I'm starting a new project and I don't know how can I share the DbContext to all the elements involved represented in the schema I have build to exemplify for the Brand entity.

My first approach would be to pass the DbContext in all the constructors but I faced a problem right away in the Helpers class where some of them don't need to connect to the DB so why inject the context.

enter image description here

Patrick
  • 2,995
  • 14
  • 64
  • 125
  • What's the problem in injecting the DbContext only in classes that need it? – Steven Feb 25 '15 at 15:43
  • I'm afraid that the context gets all over the place, declaring more then one context and not been able to save data because there are more then one context active. Above all, to find a good way to complete the structure with a good practice. – Patrick Feb 25 '15 at 16:59
  • Okay, let.me rephrase that. What's the problem in injecting the same DbContext instance during the request into all classes that need a DbContext? – Steven Feb 25 '15 at 17:54
  • Related: https://stackoverflow.com/questions/10585478/one-dbcontext-per-web-request-why – Steven Feb 25 '15 at 17:55

2 Answers2

0

If u want to use a shared context and u r using spring then u can use the sharedContextParam property and those beans will be initialized once and accessible to every WARS. Just google out the syntax for thesharedContextParam...

Art
  • 414
  • 7
  • 24
  • Suppose you have an EAR in which you have many WAR's so you can make ur WARS as skinny and refer to and get in each Web.xml you can give the path for shared context. You can easily get this up and working by reading any example on Google. If you still face issue then please post here. – Art Feb 25 '15 at 14:51
  • I have done this for Hibernate shared Context where I need Hibernate to be shared across multiple WAR's. – Art Feb 25 '15 at 14:52
0

I will suggest a slightly different approach. I prefer the following architecture if you are working with POCOs and willing to use a custom DBContext.

enter image description here

user3021830
  • 2,784
  • 2
  • 22
  • 43
  • Hi thanks. What is the difference between Services and Business Layers? Where do you create the context? – Patrick Feb 25 '15 at 15:00
  • I thought that Services meant to be Web services/WCF/RESTfull services to provide abstraction and lower platform dependency. Am I wrong? If so you will be accesing data through WCF and won't be in need of initializing context from the UI layer.Context creatin ans scope will be used in Business Layer which will provide formatted data to the service layer. – user3021830 Feb 25 '15 at 15:05
  • Services is intended to operate the DB. So you create an empty constructor in the Business Layer classes and initialize the DB and get the context there? – Patrick Feb 25 '15 at 15:12
  • I use some code like http://tinypic.com/view.php?pic=1zg6aoo&s=8#.VO3okfmsUmQ in my BL. – user3021830 Feb 25 '15 at 15:22
  • But if you need to manage 2 or more service entities, you create a context for everyone of it? and if you need to share data between them how can you garanty the same context to save changes? – Patrick Feb 25 '15 at 15:41
  • Well I construct my Model as composite Models or use composite DTOs for this spesific purpose (actually for all purposes, I would like to hide my class structure behind the service). For example If I need Personel data with Educational data and Service records I create a PersonDTO which has three properties PersonData (of type Person entity/model), EducationalData (of type List entities/models) and ServiceData (of type List entities/models) – user3021830 Feb 25 '15 at 15:48
  • Hi, and where to you store the DTO classes? – Patrick Mar 04 '15 at 14:50
  • Since DTOs are shared by all layers Shared seems to be OK. – user3021830 Mar 04 '15 at 14:52