Sharing DbContext between multiple methods and classes

יום ראשון, ספטמבר 21, 2014

I've been working with EntityFramework for more than 3 years and I love the technology. For many cases it simplifies the work of accessing the database. It general, it is recommended to keep EntityFramework DbContext manipulation at the DAL level and not let it propagate to higher layers (BL and Presentation) So, suppose I am using the repository pattern. The application does not access the DbContext object directly but rather is using a repository object which encapsulates all the data access logic. For example, public class LoginRepository {     public Login GetLoginByID(int loginId)     {         using (MyDbContext context = new MyDbContext())         {             var login = context.Logins.Where(l => l.ID == loginId).Single();             return login;         }     }     public void DeleteLogin(int loginId)     {         using (MyDbContext context = new MyDbContext())         {             var login = GetLoginByID(loginId);             context.Logins.Remove(login);             context.SaveChanges();         }     } } This is quite a straightforward implementation. Let's use it from a Main method static void Main(string args) {     try     {         LoginRepository logins = new LoginRepository();         logins.DeleteLogin(1);     }     catch (Exception e)     {         Console.WriteLine("ERROR: " + e.ToString());     } } Surprisingly, the following error is reported The object...

Building Single Page Application – Bundle Orderer

יום שישי, דצמבר 27, 2013

In my previous post I discussed a way to arrange scripts in a single page application. Short story: each application layer is associated with exactly one bundle. Assuming a vertical dependency between layers, we can just include the bundles in the right order and any time we add a new file to a layer that file is automatically added in the right position with respect to other files in other layers. For example, below is a common bundle configuration in my single page applications bundles.Add(new ScriptBundle("~/bundles/lib")     .IncludeDirectory("~/Scripts/Lib", "*.js", true)); bundles.Add(new ScriptBundle("~/bundles/server")     .IncludeDirectory("~/Scripts/Server", "*.js", true)); bundles.Add(new ScriptBundle("~/bundles/bl")     .IncludeDirectory("~/Scripts/BL", "*.js", true)); bundles.Add(new ScriptBundle("~/bundles/views")     .IncludeDirectory("~/Scripts/Views", "*.js", true)); bundles.Add(new ScriptBundle("~/bundles/app")     .IncludeDirectory("~/Scripts/App", "*.js", true)); Please note that each bundle is configured to include a specific directory. This means that when you add a...

ASP.NET MVC 4 and Single Page Application

יום רביעי, יוני 6, 2012

A few days ago Microsoft released ASP.NET MVC 4 Release CandidateAs usual I was anxious to see what's new for the new release and quickly moved over the release notesGood news, Web API continues to evolve and coding web controllers looks even easierBad news, and this was quite a surprise for me, the "Single Page Application" project is no longer part of ASP.NET MVC 4. Not sure if it will be part of future MVC release. The official statement looks something like that: "SPA won’t ship with the final MVC 4 release, ...
no comments

What’s new in ASP.NET 4.5 – #2 Session at John Bryce

יום ראשון, אפריל 29, 2012

Today I delivered a 3 hour session about ASP.NET 4.5 at John Bryce college. This is the second session about ASP.NET 4.5 and like the first one I displayed the major new features of the Microsoft web platform. Specifically I presented the new ASP.NET Web Forms data binding mechanism, KnockoutJS, Web API, Bundling and Minification, Visual Studio 11 IDE enhancements and ASP.NET MVC 4 new mobile support. All samples which were presented during the session can be found here Hope to see you in the next session too


יום שישי, מרץ 23, 2012

If you haven’t watch this video yet then go do it now !!! IMHO, one of the most interesting videos lately. Steve Sanderson from MS introduces ASP.NET MVC 4 combined with Upshot, Knockout and History. Using these technologies you can much easier develop a Mobile/Desktop like applications for the web. Knockout allows you to bind the local data to the UI, Upshot manages the interaction with the server and even can save data locally in case server is down and History manages the browser navigation system for you. One aspect that feels like missing is...

Are you using Cassette?

יום שלישי, מרץ 20, 2012

ASP.NET 4.5 Developer Preview introduces a new capability to manage scripts bundling and minification. You can read about it here. However, the current implementation lacks a very important aspect of scripts management which is managing the dependencies. As you know, when referencing a single script file you also need to reference all its dependencies. Wouldn't it be nice if someone automatically injects the right dependencies? Enter Cassette world ... Cassette is an HttpModule which seats inside ASP.NET pipeline and monitors requests for scripts and CSS. Cassette can automatically bundle and minify your code and even more interesting it can parse each...

Implement an HTML table using DIVs

יום שבת, דצמבר 24, 2011

This is quite an old topic but still when browsing through some popular forums I still encounter the following question: "How can I implement a table using DIVs and control the width of each column?" Before digging into the implementation I would like to share my thoughts regarding the old TABLE vs. DIV war:In the old days web developers used TABLE tag for displaying a tabular data and for controlling the layout of the page.These days it seems as everyone hates TABLE and tries to use DIV instead for both tasks. I don't share the same feelings.I use TABLE for tabular...

Using Razor Outside of the Web

יום חמישי, נובמבר 10, 2011

Does the following task sound familiar to you: "Upon work completion the system will send a notification e-mail to relevant recipients" ? Two weeks ago I had a chance to implement this kind of scenario once again. Of course, part of the requirement is to send a very informative E-Mail which means sending not a plain text but rather an HTML. It goes like this: Read the content of an E-Mail template file Modify the template with real time information Send the modified email to clients Bullet #2 is not an easy one. You can use XSLT (if you are lucky enough to master it). Else, you...
no comments

Exclude A Filter

יום שישי, אוקטובר 28, 2011

In my last ASP.NET MVC MSDN session I was asked a simple question: "How can we disable a filter for a specific action, assuming it is already defined at the class level?" Of course, having this ability can really ease the maintenance of our application. Without this ability we need NOT to define the filter at the class level and then define at each action level. Well, I did not have any simple question. However, I claimed that such an infrastructure can be easily developed using the excellent ASP.NET MVC extensibility support. Is that so? Let's try do it Full sample code...


יום רביעי, ספטמבר 28, 2011

Two days ago I delivered another ASP.NET MVC 3 MSDN session.As usual, we started with the basics to allow engineers not familiar with ASP.NET MVC to understand the major concepts behind it During the second part of the session, I demonstrated several subjects that are considered more advanced such as, Custom Action Result, Custom Model Binder, Custom Metadata Provider and other Custom XXXIn addition, I had the chance to introduce some really nice tools such as Knapsack and ASP.NET Mini Profiler All samples which were presented during the session can be found at my SkyDrive public folders I really enjoyed delivering this...