|
|
Some half baked ideas by Nilotpal about Tao, Zen and the art of converting coffee into code...
-
My last communique has always been quite important for me in all my previous organizations. And I had thought that I would say something profound in my last email when I leave. But I could not think of anything and so no email. I still can't think of anything that would be super interesting so I guess I will keep it simple
This is going to be my last blog post here. I have resigned from Microsoft and am moving on to seek my future else where. I never thought this day would come and am myself quite surprised.
My experience with Microsoft was amazing. I am always going to remember this company as "The Company". However, I would like to add that there is definitely certain things that I expect to change in Microsoft IT, Hyderabad.
I would look forward to coming back to Microsoft some day, perhaps in a different role or capacity in a different division. And so I will not say Goodbye. I will just say "Bye for now" and hope to return some day. Until then I will continue to blog at http://www.dasboard.in/nilotpal.
|
-
I got this error. I got no information on the internet as to how to get around it… 3 hours earlier: I wanted to create a sequence diagram from the code I have already written. Visual Studio 2010 comes with the sequence diagram feature. I decided heck why not use it. It creates some really cool sequence diagrams. I opened the same project in Visual Studio 2010, created the sequence diagram, closed the project. Went for a smoke. 30 minutes earlier: My test cases won’t run. the error I get is “Error loading xxx.vsmdi: D:\Projects\xxx\xxx.vsmdi” looked here, looked there looked everywhere. But didn’t look in the place I should have been looking. 10 minutes earlier: I built the test project. I got an error. “Project file contains ToolsVersion="4.0", which is not supported by this version of MSBuild. Treating the project as if it had ToolsVersion="3.5".” Well I should have been looking at this 30 minutes ago. I went to the test projects Properties, went to TargetFramework and checked. it was not selected at all. I selected Framework 3.5. After this all I had to do was close the project and open it and then run all my tests in the solution. They were all still green. Love it when that happens…!!
|
-
This evening I heard someone say that Left Outer Joins are not possible in Entity Framework or perhaps Left Outer Joins are possible, but they bring in all the data and then filter out the right table. I felt an urge to argue, but I resisted. I thought I would investigate before I waste time. I came up with some articles pretty quickly. -
-
-
-
-
I am sure these should resolve all questions related to Left Outer Joins. Now about the performance and the question about how the data is brought, I am pretty sure we can prove that LINQ Queries all support deferred execution. But we can always prove that using the SQL Query Profiler. Hope this helps.
|
-
When faced with the dilemma that what if I wanted to insert something and before I actually saved the changes into a persistent storage (database) by calling SaveAllChanges() method on the data context, I wanted to do a select, and also I wanted to make sure that this select is done on the just inserted objects, what should I be doing? Well generally we write a generic method that gets all the records for a generic object. it looks like this: IQueryable<T> result = null; string type = typeof(T).Name; //ObjectQuery query = entities.CreateQuery<T>(type); ObjectQuery query = Context.CreateQuery<T>(type); result = ( IQueryable<T>)query; return result; This will only query into the database. So if you have just recently inserted some records, but haven't yet committed them, they won’t appear in the results. However, if we provide another method (an overload perhaps) that handles local objects, this would be possible. When I say local objects, I mean objects that have been inserted into the Object Context and yet not been persisted into the database. this method would look something like this: IQueryable <T> result = null; string type = typeof(T).Name; return from stateEntry in context.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Unchanged) where stateEntry.Entity != null && stateEntry.EntitySet.Name == type select stateEntry.Entity as T; This way we give the option to the user of the API to choose which kind of select he wants to fire. A local one or one on the database.
|
-
This is not a problem that I feel you would encounter on a regular basis. I think I had a unique problem because of which I encountered this one. When you install an operating system it allows you to create a profile. Now I installed the machine at home. Which means I was not on the corpnet of Microsoft which means I could not log on to the domain. So when I created a profile named after my alias, it was considered as a local profile and not a domain profile. And a folder was created under “C:\Users” with my alias name. I am not sure if this makes any sense or not, but hey, I am a software program writer not a network administrator. So please excuse my jargon. Anyway, when I actually logged on to the domain with my brand new operating system the next day, a new profile had to be created for the domain. Now since there was a folder already with my alias name in “C:\Users” the brand new operating system create a brand new profile with in the format <alias.domain> so now there was a “.” in my profile directory name. What happens is this “.” sometimes creates problems in installing certain software. I got the error message saying “There is an invalid character in the directory path of My Documents”. I checked the path and could find only one anomaly. And no points (“.”) for guessing what. So I decided I will rename the profile directory. I straight away went to the directory and though I would rename it to something else. of course it wouldn’t let you. So I thought I would log on as a local administrator and then do it. Also I made sure that I start windows in safe mode. After Logging on I backup my documents, and delete the profile and assume that the next time I log on, it will automatically create the new directory for me. Guess what… Not that easy… So after a long research I found out that there are registry entries out of which windows reads the location of the profile directory. So if it doesn’t find the directory at the specified location, it creates a temporary directory and logs you on with a temporary profile. And after the log on is complete, it gives you that message. So here is what you do to save your life. Please do this only if you have already deleted your profile and did not take backup. If you are doing your research before you have messed with your machine, please skip these steps and refer below. -
Log on with local administrator account, preferably in safe mode. To boot the machine in safe mode, Click F8 during boot. -
Click Start + Run and type in regedit and then Enter. -
Open HKEY_LOCAL_MACHINE -
Open Software -
Open Microsoft -
Open Windows NT -
Open Current Version -
Open ProfileList -
Here you will see keys that belong to each profile. To find your problematic profile navigate to each key and refer to the ProfileImagePath entry on the right hand side. -
Once you find your problematic profile, just delete the key. This means delete the folder on the left hand side tree view that contains your alias in the ProfileImagePath entry. -
Restart machine. There is another way you can actually backup your profile and create a new one. Click on “Start”, Right click “Computer” and go to “properties” Click on “Change Settings” Go to the “Advanced” tab and click on User Profiles “Settings” button “Change type” will allow you to change your profile type between Roaming and Local Profiles “Delete” will delete that profile. Including the registry entries and everything. “Copy To” will back up your profile. It is always advisable to backup your profile from here rather than from anywhere else. Your profile contains more than you can see. Hidden Files, System Files, etc. This takes care of everything in a safe manner. Hope this helps.
|
-
-
Internal Logical File (ILF): An internal logical file (ILF) is a user identifiable group of logically related data or control information maintained within the boundary of the application. The primary intent of an ILF is to hold data maintained through one or more elementary processes of the application being counted. -
External Interface File (EIF): An external interface file (EIF) is a user identifiable group of logically related data or control information referenced by the application, but maintained within the boundary of another application. The primary intent of an EIF is to hold data referenced through one or more elementary processes within the boundary of the application counted. This means an EIF counted for an application must be in an ILF in another application. -
Data Element Type (DET): A data element type (DET) is a unique, user-recognizable, non-repeated field. -
Record Element Type (RET): A record element type (RET) is a user-recognizable subgroup of data elements within an ILF or EIF. -
File Type Referenced (FTR): FTR's are the combined number of Internal Logical Files (ILF's) referenced or updated and External Interface Files referenced -
External Input (EI): An external input (EI) is an elementary process that processes data or control information that comes from outside the application’s boundary. the primary intent of an EI is to maintain one or more ILFs and/or to alter the behavior of the system. -
External Output (EO): An external output (EO) is an elementary process that sends data or control information outside the application’s boundary. the primary intent of an external output is to present information to a user through processing logic other than or in addition to the retrieval of data or control information. The processing logic must contain at least one mathematical formula or calculation, or create derived data. An external output may also maintain one or more ILFs and/or alter the behavior of the system.” -
External Inquiry (EQ): An external inquiry (EQ) is an elementary process that sends data or control information outside the application boundary. The primary intent of an external inquiry is to present information to a user through the retrieval of data or control information. The processing logic contains no mathematical formula or calculation, and creates no derived data. No ILF is maintained during the processing, nor is the behavior of the system altered. -
General System Characteristics (GSC): The data and transactional functions discussed in earlier chapters together represent the total business functions that are delivered through the application being counted. Truly this is the logical representation of the functionality and does not address the physical implementation of the functions in an IT environment. The FPA method of IFPUG has captured the critical implementation features of typical applications through 14 general system characteristics. These encompass almost all the major implementation complexities that can exist, and through careful evaluation of each of the GSCs, the estimator can arrive at final function point count that includes logical as well as physical implementation properties of the application being counted. -
Value Adjustment Factor (VAF): Calculated ((Total Degree of Influence * 0.01) + 0.65) -
Calibration Factor (CF): general buffer depending upon practical environment -
Delivery Rate (DR): No. of function points a single developer will accomplish every month -
Days per person month (DPM): The no. of days a person will effectively work in a month. 20 working days in a month and Microsoft generally counts 75% utilization which means around 15 working days in a month.
|
-
Look around the web. You will find no Update method in the repository patterns for entity framework implementations published. There are different flavors of retrieves, mark for insertions an deletions but no update implementation. And curiously this is the most painful. There are 2 options that you have. But to explain both, I will start off with the repository design. We can get into a discussion about the pattern in case anyone is interested, all you have to do is instigate me by posting a comment.  So now your IRepository here has some generic functions : --- IEnumerable<T> All();
IEnumerable<T> FindAll(Func<T, bool> exp);
T Single(Func<T, bool> exp);
T First(Func<T, bool> exp);
void MarkForDeletion(T entity);
T CreateInstance();
void SaveAll(); --- These generic functions, if you notice, does not contain an update function. Now the 2 options that you have that I have talked about, are Option 1: Do not take care of your updates in the provider. Now this is simple. Do all the updates in the logical layer that calls the data provider. Something like --- Events dbEvent = null; IEnumerable<Events> dbEvents = this.DataProvider.All<Events>(eve => eve.EventID.Equals(communityEvent.EventID)); if (dbEvents != null && dbEvents.Count() == 1) { dbEvent = dbEvents.SingleOrDefault(); } else { throw new ApplicationException(MADConstants.GENERIC_DATA_ERROR); } dbEvent.DateModified = communityEvent.DateModified; dbEvent.Description = communityEvent.Description; dbEvent.EventEndDate = communityEvent.EventEndDate; dbEvent.EventName = communityEvent.EventName; dbEvent.EventStartDate = communityEvent.EventStartDate; dbEvent.IsApproved = communityEvent.IsApproved; dbEvent.LastModifiedBy = communityEvent.LastModifiedBy; dbEvent.NoOfRatings = communityEvent.NoOfRatings; dbEvent.Organizer = communityEvent.Organizer; dbEvent.ParticipatedBy = communityEvent.ParticipatedBy; dbEvent.RatedBy = communityEvent.RatedBy; dbEvent.Rating = communityEvent.Rating; dbEvent.Sessions = communityEvent.Sessions; this.DataProvider.SaveAllChanges();
--- The second option which i feel is most elegant is this: --- /// <summary> /// Updates /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entityName"></param> /// <param name="entity"></param> public void Update<T>(string entityName, T entity) { this.context.AttachTo(entityName, entity); } --- So all you have to do now is to call the functions like this --- DataProvider.Update<Customer>(“Customer”, customer); DataProvider.SaveAllchanges(); --- The third option is a little complex. Please refer to the diagram above, the task repositories that you will implement must contain the update function like this: public void Update(Customer customer) { context.Attach(customer); //Custom extensor method context.SaveChanges(); } This means you have all your Task Repositories that know the type of your entity. So a CustomerRepository would know that it is handling Customer Types and hence will take Customer as a parameter to the Update Method. This is because Attach method requires an argument that is of type System.Data.Objects.DataClasses.IEntityWithKey. Which means it will not accept type T because this does not have the entity key or the primary key on the basis of which it will know which entity / row to update in the database. The purpose of the third option is that all interaction with the database that is specific to a particular entity can be handled in the Task Repositories.
|
-
We decided to implement the repository pattern for the Entity Framework Implementation in our project. So last couple of weeks people have been asking me why the repository pattern is required. I mean what is it that this pattern does besides giving a layer of abstraction, and providing the CRUD methods, which are also available in C# as a language feature (LINQ). The answer is given aptly by Martin Fowler as, “A system with a complex domain model often benefits from a layer, such as the one provided by Data Mapper (165), that isolates domain objects from details of the database access code. In such systems it can be worthwhile to build another layer of abstraction over the mapping layer where query construction code is concentrated. This becomes more important when there are a large number of domain classes or heavy querying. In these cases particularly, adding this layer helps minimize duplicate query logic. A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection. Client objects construct query specifications declaratively and submit them to Repository for satisfaction. Objects can be added to and removed from the Repository, as they can from a simple collection of objects, and the mapping code encapsulated by the Repository will carry out the appropriate operations behind the scenes. Conceptually, a Repository encapsulates the set of objects persisted in a data store and the operations performed over them, providing a more object-oriented view of the persistence layer. Repository also supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers.” This has a no. of benefits. Simplifying complexity, removing redundant querying, making code more testable, etc. Let’s say you want to test your code without going into the database and destroying it completely. unless you use DBUnit, This pattern helps you a lot. All you need to do is change your SaveChanges() function from --- /// <summary> /// Saves all changes in the current context. /// </summary> public virtual int SaveAllChanges() { try { ((ObjectContext)this.Context).SaveChanges(); return 0; } catch { return -1; } }
--- to --- /// <summary> /// Saves all changes in the current context. /// </summary> public virtual int SaveAllChanges() { return 0; }
--- On doing this, your changes are not populated to the database and you can do all your testing in-memory. Now is it a good idea to change your code to just make it testable? Well that’s a good question that needs discussing at length. Maybe in another post…
|
-
Why do Entity Framework Function Imports do not have eager loading? Because when you use Entity SQL or regular Object context to retrieve data, it is a live connection. So whenever you say eager loading enabled, it gets the data from the database using the live connection. But if you want to do it using Function Import, EF doesn’t know where to get the data from, since the stored procedure that the function import maps to, doesn’t get that data. Perhaps in the later versions of EF, the team might add the feature which can mix and match the object context and the stored procedure to get the required data. But as of now it is not possible.
|
-
Entity Framework, Eager Loading and Lazy Loading, my own 2 cents. For those new to Entity Framework, we are talking about the patterns that you can use to load related objects. The strategy by which you would load entity objects that are related to the one in question. There is a beautiful resource that mentions it all. I do not think there is any elaboration required in that matter. But this is not the meat of the matter.
How do you design an application that uses Entity Framework as a data access strategy? There are a no. of things to consider. The apparent answer all over the Internet is the Repository Pattern (ref: http://blogs.infosupport.com/blogs/willemm/archive/2008/08/18/Using-ADO.NET-Entity-framework-with-the-repository-pattern.aspx, http://blogs.microsoft.co.il/blogs/kim/archive/2008/11/12/data-access-with-the-entity-framework.aspx, http://www.simonsegal.net/blog/2009/01/13/entity-framework-repository-specifications-and-fetching-strategies/). The purpose of the repository pattern is "A system with a complex domain model often benefits from a layer, such as the one provided by Data Mapper (165), that isolates domain objects from details of the database access code." All said and done, we write the repository to abstract the CRUD from the Application Layer. This way, simply put, we don't need to write separate routines to do CRUD behaviour on every entity type. The functions look something like this:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary> /// Returns the required querable object to the caller /// </summary> /// <typeparam name="T">Generic Object</typeparam> /// <returns>IQueryable<T></returns> private IQueryable<T> GetTable<T>() { IQueryable<T> result = null; string type = typeof(T).Name; ObjectQuery query = Context.CreateQuery<T>(type); result = (IQueryable<T>)query; return result; }
This routine is called like this.
////<summary> ////Return all instances of type T. ////</summary> ////<returns></returns> public virtual IEnumerable<T> All<T>() { System.Data.Linq.DataLoadOptions dlo = new System.Data.Linq.DataLoadOptions(); return GetTable<T>(); }
And this in turn is called like this:
public IEnumerable<Employee> GetEmployees() { IEnumerable<Employee> employees = this.dbProvider.All<Employee>(); return employees; }
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Now if you see, we have one function retrieve all types of entity objects. Now let's get to the actual topic in hand. Eager loading and lazy loading. I cannot opt for any of the mentioned patterns, because I do not have the type of the pattern that I am going to return at design time. I have already postponed that by using Generics. Just last night I learnt from Daniel Simmons that Framework 4.0 they are planning to add a property to Context object in order to opt in implicit lazy loading.
While I am still trying to figure out a good way of working around eager loading and lazy loading, as of now (before 4.0 arrives) I am firing separate queries to get all types of related objects. And interestingly, once I fire the queries for the related objects and enumerate them, the principal object at hand gets populated with related objects. So next time you want to reference the related objects, just go ahead and use the beautiful sentence completion features such as this: "txtAddressLine1.Text = employee.EmployeeAddress.First().Address.AddressLine1;"
|
-
So you are serious about your code quality huh... well StyleCop is a fantastic tool to ensure that. The best part is, it integrates with Studio 2008. It is simple. -
Go to This Location, click on downloads and download the latest version of StyleCop. As of now it is the version 4.3 -
Install StyleCop. It asks you information about integration. Let it integrate with Studio for the moment. -
Fire up Visual Studio -
Click on Tools and say Run StyleCop and look at the 1001 warnings that StyleCop gives. Now when you use StyleCop, most of the warnings are not what you really want to work upon. Like Documentation, for instance, who cares whether there is a blank space line above a comment or not...? So you can go to Solutions Explorer and click on Style Cop Settings. This will bring up the "Microsoft Style Cop Project Settings" Dialogue box which will allow you to customize your rules set. Allow you to enable / disable rules as per your own special requirements / whims / fancies / moods... :) It is a fun little tool and I could have written more about it but then I decided that I won't spoil the fun. You guys go ahead and explore.
|
-
Lately everyone in my team and around my bay have been talking about Entity Framework. Well since I have been an early adopter, writing my small apps using Entity Framework and fighting for it on my project, people have been coming to me to ask questions. I have been taking pride in answering them (happy to help), most of the times... until lately... since I have been working on too many things... I just came across an FAQ which pretty much covers it all (thanks Daniel...!). I thought I will add it to my browser favourites. But then I thought that would just help me and not everyone. Happy to help... :)
|
-
So what's this new buzz word... everyone has been talking about it... Essentially what we are doing is taking our applications and putting them in a cloud. Cloud meaning highly available servers which are provided by huge service providers such as Microsoft, Amazon, etc. Now since I strongly believe that it is business that drives technology, it is the business side that we need to understand first. why do we need it. I have my servers in house, I deploy my apps on my servers, I control and maintain them then why would I want it to be on servers that others control? It is like the analogy that my valuables are in my house, how can it be a better idea to go and put them somewhere else. Well the answer to that question is another question. Do you put all your money under your mattress in your house? Or is it a better idea to go and put them in the bank? There are many benefits in building the applications from the cloud. Let's take cost for example. The cost of hosting an application on the cloud is just a fraction of what you will have to incur in deploying the application on one of your in-house servers. Along with it comes the maintenance cost. Employing people to maintain the Servers, business continuity planning, and so on and so forth. The list goes on. The Cloud providers on the other hand can provide you all the same services at a fraction of the cost. They work on much much larger scales. So it makes sense for them too. Another interesting perspective is that, let's say you are an application vendor. You go and talk to a customer to sell your app. Let's say you convince the customer in buying the app. That means you have convinced one person. The person who takes the decisions. Let's say he consults a team before he takes the decision. Even in that case you have convinced that team. And most usually, the people who make the decisions are not the people who end up using your app. And there is no way for them, yet, to find out whether or not the app is good enough for them in their context. So then you go ahead and deploy the app and then start using it. Then the users, find out how much the application was worth in their context and they may or may not like it. Although this is the model we have been working on all these years, it doesn't look like a very effective model does it? Let's change the scenario. Same application vendor, but this time, he has his application as a software as a service, deployed on the cloud. He just has to go and say, "Well you don't have to guess, I don't have to convince you. Why don't you use the application yourself for a few days and find out?" The users use the app, they like it fine; if they don't, well they don't have to buy it. And of course there is the "pay as you go" model in Software as a service. Just stop paying when you stop using the software. And the best part is you don't have to go fight it out with your IT for deployment. It's all there in the cloud...!!
|
-
I had recently posted about a bug on SQL CE Entity Framework. Please note that the links mentioned in there for the bug and the hot fix are internal to Microsoft. The hot fix shall be released as a KB article in a couple of weeks.
|
-
I am working on this really smart, smart client application installer. Which smartly installs other pieces of another smart client application as and when required.
It makes sure that only those bits are downloaded that are not already installed. So it takes care of upgrades on its own. So basically we are writing our own "ClickOnce" functionality.
Why are we not using ClickOnce out of the box? Well that would require another post, but let's say we require much more granularity than what "ClickOnce" provides us, on this "Installation Manager", and put it at that.
Anyways, I am doing some book keeping using SQL Server CE on the client machine to maintain account of upgrades and installations. For this I am querying the SQL Server CE database and in the process and I encounter an error that says:
"The ntext and image data types cannot be used in WHERE, HAVING, GROUP BY, ON, or IN clauses, except when these data types are used with the LIKE or IS NULL predicates."
I am not even working on ntext data type. The select query goes something like this:
IQueryable <FileInfo> files = entities.FileInfo.Where(fileInfo => fileInfo.FileName.Equals(fileName));
And "FileName" is not an ntext datatype in the entity "fileInfo". It is an nvarchar.
Interestingly, if you fire the same query on the Management Studio (SQL Version of the query of course), it works fine. It only breaks when you use LINQ to SQL or EF.
After a little research I find out that there is a bug in SQL Server 3.5 RTM which was incidentally not resolved even in the "SQL Server Compact 3.5 SP1 English". And later there was an out of the bag release that was made to resolve this issue. So the Hotfix, cannot be installed directly. You need to uninstall "Microsoft SQL Server Compact 3.5 SP1 English" from Add Remove Programs before you install this hotfix.
Once the Hotfix is installed, your query will just work...!
In case if you are wondering whether you need to re-install "Microsoft SQL Server Compact 3.5 SP1 English"? Well, installing it back will remove the hotfix. So DO NOT DO THAT.
|
|
|
|