Entity Framework - Intro

0

Category :

When you use EF it by default loads each entity only once per context. The first query creates entity instace and stores it internally. Any subsequent query which requires entity with the same key returns this stored instance. If values in the data store changed you still receive the entity with values from the initial query. This is called Identity map pattern. You can force the object context to reload the entity but it will reload a single shared instance.

Any changes made to the entity are not persisted until you call SaveChanges on the context. You can do changes in multiple entities and store them at once. This is called Unit of Work pattern. You can't selectively say which modified attached entity you want to save.

Combine these two patterns and you will see some interesting effects. You have only one instance of entity for the whole application. Any changes to the entity affect the whole application even if changes are not yet persisted (commited). In the most times this is not what you want.

You can always think about context as about Unit of work. Create context when you open Edit dialog, load data for editation, modify data, save changes, close context. In server scenario it depends on situation. Sometimes it is enough to have context in single method but if you want to make multiple operations working with data you can share the context among multiple methods. Again - unit of work. Things which should be handled together should use single context.




refs
http://stackoverflow.com/questions/3653009/entity-framework-and-connection-pooling/3653392#3653392
http://msdn.microsoft.com/en-us/magazine/ee335715.aspx

Inheritance with EF (compares 3 techniques)

http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx

Bitwise operations

0

Category :

Bitwise operations........some craic!!
----------------------

int test1 = 3; // binary: 0011
int test2 = 2 // binary: 0010
int flagTest = test1 | test2; // binary result: 0011 | 0010 = 0011

0011
0010 OR
----
0011

flagTest = 8; // 1000
flagTest |= 3; // 2 = 0011
// flagTest is now binary 1011

AutoApprovalFails fails = AutoApprovalFails.Success;

// this ngb/coach level is disabled
fails |= AutoApprovalFails.HasTutorsInTraining;

fails |= AutoApprovalFails.InvalidCourseLevel;

string message = string.Empty;
foreach (Enum value in Enum.GetValues(typeof(AutoApprovalFails)))
{
    // eg, if 0100 in 1110;  0100 & 0111 = 0100
    if (((AutoApprovalFails)value & fails) == (AutoApprovalFails)value)
    {
        message += EnumDescriptor.GetDescription((AutoApprovalFails)value);
    }
}