I am now working with a little bit of unit structure, and I have come in many scenarios where two references are the same unit Etc., so I am thinking that maybe I am not opening / closing the best method.
At present, I basically open DBContact on each of its controllers. The original MVC App was originally installed, which means that I have a private DBBankText field on the controller, and I Override the controller's disposal method for disposal.
However, I sometimes call DB in some other sections, which can call from within the controller, which also has an open reference.
Is there any way to use an open reference without a clear handler? This does not really mean passing through a dozen different ways for me to go around a DBCT context reference.
Use of Dependency Injection
As Others Have said and will probably have to repeat, 'right way' is often considered dependent injection.
In my latest project, I have organized things so that I can almost complete the project and it has become so easy that I am doing it myself (rather than using the injector) . A major factor in this is strictly adhering to this structure:
WebProject | | | Data Services | | | ViewModels EntityModels
All data services are accessed through a single DataServiceFactory instance during a unit of work, for which an example of MyDbContext is required Another aspect is a completely unstable The application has been designed - this means that I do not have to distinguish between firmness in my code.
Without Dependence Injection
He said, maybe D is not right for you on this project. Maybe:
- You do not plan to write unit tests
- You need more time to understand di
- Your project structure already has EF Is deeply integrated
In ASP.NET MVC, the unit of work often matches the request fully with the lifetime - that is, the HTTPS consort. Current
. As a result, instead of using the AI, you can freely request a single single copy per repository. There is a classic singleton pattern with the current reference in the form of backing store, your DbContext
:
public class repository proxy {get private static HTTP contact CTX} ; }} Private Static Guid repoGuid = typeof (MyDbContext). Dog; Receive public static MyDbContext context {{MyDbContext repo = Ctx.Items [repoGuid]; If (repo == zero) {repo = new MyDbContext (); Ctx.Items [repoGuid] = Results; } Return Repo; }} Public Static Zero WiFi Contact () {MyDbContext repo = Ctx.Items [repoGuid]; If (repo = null) repo Save Changes (); }}
If you are feeling particularly lazy, you can also save change
yourself (you still have to observe the side effects Manually call for, such as retrieving the ID for a new item):
Public abstract class Extended Controller: Controller {Protected MiddetTontext Reference} {get repositoryProxy.Context; }} Safe Override Zero On-Off Acne (ActionExecuted Contact Filter Kontex) {Repository Proxy. SaveEfkotext (); Base.OnActionExecuted (filterContext); }}
No comments:
Post a Comment