Thursday, November 28, 2013

Dependency Injection

Simply Dependency Injection means giving an object its dependencies, instead of them itself having initializing them. Dependency Injection is useful for testing as well. Because it allows dependencies to be mocked or stubbed out. 

Types of Dependency Injection ##

Constructor injection
Setting dependencies through constructor

Setter injection
Setting dependencies using Setter method

Interface injection 


http://stackoverflow.com/questions/12246438/constructor-injection-and-initialization-of-dependencies-being-injected
http://programmers.stackexchange.com/questions/190120/should-i-use-dependency-injection-or-static-factories


What is a Dependency Resolver
Allows you to abstract away Dependency Injection implementation 
http://stackoverflow.com/questions/20086440/asp-net-mvc-4-dependency-resolver

What is interception


You can use Unity for Dependency Injection in .NET. Read this comprehensive article set on unity.

Dependency Inversion in JavaScript

http://stackoverflow.com/questions/5349003/dependency-inversion-principle-in-javascript

Resources

https://www.youtube.com/watch?v=FuAhnqSDe-o

http://stackoverflow.com/questions/13442312/when-do-i-use-abstract-class-or-interface-in-dependency-injection

http://stackoverflow.com/questions/13442312/when-do-i-use-abstract-class-or-interface-in-dependency-injection

https://richnewman.wordpress.com/about/code-listings-and-diagrams/dependency-injection-examples/dependency-injection-example-interface-injection/


Wednesday, November 13, 2013

Other resources for Entity Framework


Difference between Eager loading and Lazy loading


http://stackoverflow.com/questions/3485317/entity-framework-4-single-vs-first-vs-firstordefault


Bulk Inserting in Entity Framework
http://stackoverflow.com/questions/5940225/fastest-way-of-inserting-in-entity-framework
http://stackoverflow.com/questions/6107206/improving-bulk-insert-performance-in-entity-framework

...............

  • By default, EF interprets a property that's named ID or classnameID as the primary key
    • using ID without classname makes it easier to implement inheritance in the data model
  • Navigation properties are typically defined as virtual so they can take advantage of certain EF functionality such as lazy loading. 
  • DatabaseGenerated is an attribute you can define on primary key. It says you'll manually enter the primary key rather than Database generating it.
  • The main class that coordinates EF functionality for a given data model is the database context class.
    • You create this class by deriving from System.Data.Entity.DbContext class
    • In your code you specify which entities are included in the data model

    • This code creates a DbSet property for each entity set. In EF, an entity set typically corresponds to a database table, and an entity corresponds to a row in the table.
    • Above, SchoolContext is the name of the connection string you define in Web.config
      • You can also pass the connection string itself (link)
      • If you don't pass anything to base, EF assumes the connection string name is the same as the class name
    • You can write a Seed method for EF to generate data


Useful methods

ObjectContext.SaveChanges()

Persists all updates to the data source and resets change tracking in the object context. Returns no. of objects in an added, modifled or deleted state when SaveChanges was called

link1, link2, link3


Configuring Entity Framework
You can configure entity framework options from the config file. Starting from EF 6 you can use Code based configurations.

sources

Monday, November 11, 2013

Interesting Findings ASP.NET

HttpClient
Use HttpClient for Downloads. It is a new feature. Makes it easier to download files in a separate thread.
Handling 404 in ASP.NET MVC Correctly (SO)  

HttpServerUtility (MSDN)
Provides helper methods for processing Web Requests. Methods and properties of HttpServerUtility are exposed through Server property  of HttpContext.


Working with Images in ASP.NET MVC (SO)

ASP.NET Scaffolding
It is a code generation framework for ASP.NET web applications. Scaffolding can make your development time less. See  the overview from ASP.NET

Automatic Deployments
You can do automatic deployments to IIS using Micrsoft Web Deploy. Chcek ScottGu's article on that. 

JSON Formatting
Razor View Engine


Dependency injection with Unity

http://www.asp.net/web-api/overview/advanced/dependency-injection

Saturday, November 9, 2013

Interesting Things on C#

Func vs Action

  • Func returns values from the delegate (LINQ .Where())
  • Action does not return anything (LINQ Foreach())

Fluent interfaces

https://www.simple-talk.com/dotnet/.net-framework/fluent-code-in-c/


Asynchronous Programming 

Using async and await 

Fluent validations


https://fluentvalidation.codeplex.com


IDisposable interface

http://stackoverflow.com/questions/6967108/is-idisposable-dispose-called-automatically
http://stackoverflow.com/questions/45036/will-the-garbage-collector-call-idisposable-dispose-for-me
Implementing IDisposable correctly


IDisposable vs Destructor (SO1, SO2)
IDisposable vs finalize (SO, MSDN)

Logging 

https://code.google.com/p/elmah/

Preprocessor Directives (link)
Provides instructions to the compiler. Used to help in conditional compilations.

Type Inference
C# used to be a strongly typed language. You need to specify the type of the variable explicitly. But from C# 3.0 type inference was introduced. with var keyword. This is very useful in LINQ and sometimes also increases  the readability of the program (See here, here and here). var is also called implicitly typed local variable.

Read more here, here (SO), MSDN, var and performance.

dynamic type 
you can use dynamic type to by-pass compile time type checking. It's type can be changed at run time hence these are resolved at run-time. dynamic type behaves like object.

dynamic can be used to make poorly designed external libraries easier to use. See here and here.

MSDN

Tuesday, November 5, 2013

Routing in ASP.NET MVC

Enables you to map URLs that does not map to any specific files in a web site. You should add default MVC routes to Application_Start event in Global.asax file as shown in the example below


check MSDN page ASP.NET routing for more information.



Provides a collection of routes for ASP.NET routing. This class is derived from Collection<RouteBase>. 

You can define custom routes by using RouteBase class. See a tutorial.

introduced in Web API 2 to overcome challenges faced with creating routes with conventional based routings. Uses attributes to define routes.


References

Powered by Blogger.


Software Architect at Surge Global/ Certified Scrum Master

Experienced in Product Design, Software Engineering, Team management and Practicing Agile methodologies.

Search This Blog

Facebook