http://blog.brentmckendrick.comNodeJS RSS ModuleTue, 20 Mar 2018 03:58:03 GMT60When I built GraphDiff it was to solve a simple issue that I see many in the .NET community are facing. Over a multi-tiered environment I needed to transfer aggregate roots from one layer to another and determine what had changed. The proposed solution has proven successful in my projects and the projects of many others.

We are now at 2000-3000 downloads on nuget and I am excited to see where we can take this project (if I can once again find time to spend on it)

I have started on a POC today to allow for attribute style mappings on models, which will also include an API for registering default configuration mappings for an entity on app start / bootstrapping (like a fluent api)

This will allow you to define your DDD Aggregate Roots in code and have EF adhere to this configuration.

// Save it
context.UpdateGraph<Car>(car);
// Load it
// This will add the correct includes so that the whole aggregate is retrieved in one operation
var graph = context
.AggregateQuery<Car>()
.FirstOrDefault();

No configuration is needed as it is read off the attributes. This is just a proof of concept right now but will allow for Aggregates to be clearly defined in your app either by attributes or a fluent api.

This should cover many scenarios by default, but will also allow mapping overrides should the need arise where the full aggregate is not to be updated/retrieved.

Let me know what you think, and thanks for using GraphDiff.

]]>http://blog.brentmckendrick.com/graphdiff-aggregate-mappings/6f4ae0fb-e884-4cd8-bfe2-e9b1dc5094e6Sun, 01 Jun 2014 13:12:52 GMTAbout a year ago I wrote a directive to create a nice tooltip validation form for AngularJS that works cross browser. I used it in a project and then I completely forgot about it... until today! It's called xt-form and it provides AngularJS with realtime cross-browser tooltip validation like so:

So I've been quite busy and just realized there are many comments I haven't had a chance to view yet. I will try and get to them in the next week. This will include some updates to GraphDiff, as I've seen some great pull requests which I will need to migrate, and I have other bugfixes which I had planned to migrate earlier but then had problems with my PC at home.

For now though I'll post a link to my latest project called Topic Reel http://www.topicreel.com. I don't often post about my front-end dev work but I truly love working the whole stack.

The stack I have used for this project is AngularJS, NodeJS & MongoDB. I've built applications with these technologies before but I really wanted to work on something that I could release as my own.

The idea was to create an easy to use real-time front page which aggregates popular posts/images/tweets across multiple social media feeds and allows for searching across the real-time Internet as a whole.

I should warn that it's beta and it's still very much in the early stages but I have big ideas for where it could head, if given the time that is!

Anyways if you can please do check it out and if you like it let me know, or find any problems that's even better to know.

Also I have a virtualized grid built for AngularJS which I'm not quite ready to release yet but it basically contains most of what ng-grid provides but uses html markup in the page rather than javascript templating, it also allows for binding events as attributes much like a Silverlight grid control. It seems more 'Angular' to me than the way ng-grid expects things to work. Perhaps I will have some time soon to work on that too.

Cheers guys,
Enjoy coding.

]]>http://blog.brentmckendrick.com/latest-update/e5845386-842a-4b02-82e4-573f62e48f0dThu, 26 Sep 2013 18:04:12 GMTHi, This post will just be a quick update on the progress of GraphDiff.

It's great that quite a few people are using/looking into GraphDiff. I have finally found some time and I am updating the code with numerous bug fixes and new features. I will be able to spend more time on this project now so if you have any issues please add them to github here .

There is a new official package on nuget called 'RefactorThis.GraphDiff' the only breaking change is a namespace change from GraphDiff to RefactorThis.GraphDiff. This package is a newer version and will receive all future updates you can get it here .

I also want to quickly clarify what AssociatedCollection and AssociatedEntity does as I've had some questions.

Associated/Owned clarification

If a person has a list of friends and you want to update the list of a friends for a person, but not update the entities inside that collection (such as that friend's first name) then that friend collection is an associated collection and can be written like this:

if an update has been made to the friend entity we don't want it to be saved, however we do want to save the fact that the person now has a new friend. This is what the associated collection does. An owned collection on the other hand will state that all of the entities inside of the friends collection are owned by the parent entity and will be updated, so the first name change will be saved to the database.

Now only performs updates of the parent and nested entities when needed (better support for auditing and concurrency scenarios)

Bugfix for complex graphs of collections of collections

Supports proxy objects (no lazy loading is done from within GraphDiff, all entities needed are loaded with one query) however the code did not support proxy objects.

Supports reloading entities that have been attached with the GraphDiffConfiguration.ReloadAssociatedEntitiesOnAttach configuration option. This is useful in cases where you may want to return the graph once saved. EF (by design) will returned the attached object not the object as it truely is in the database as the object exists in its local cache. The easy way to get around this is to simply make the database calls on a new context. If this is unavailable to you then you can set this configuration option and GraphDiff will ensure that any associated entities are updated from the database, ensuring you always receive the latest copy of all entities.

]]>http://blog.brentmckendrick.com/updates-to-graphdiff-with-new-scenarios-supported/1827cb14-233e-41cc-b878-4cd2c66d7848Sun, 14 Jul 2013 11:14:59 GMTCurrently i'm working on a project to move an existing Silverlight application into AngularJS and there is one thing I've been missing while using Visual Studio that I had using Webstorm and that is syntax highlighting and auto-completion of angular directives.

I've made additions to the HTML 5 schema template in visual studio to get this working with Intellisense.

]]>http://blog.brentmckendrick.com/angularjs-visual-studio-2012-intellisense/514ce0d7-a48a-441e-8f66-50bc37467d62Sat, 16 Feb 2013 19:43:34 GMTLooking for a complete solution for automatically updating a graph of entities using the Entity Framework? Read On!

So today I'm finally going to post something that we have actually been using live on our production code for quite some time, and the good news is that it is working beautifully. I'm introducing GraphDiff - an extension method allowing for the automatic update of a detached graph using Entity Framework code first.

Working with detached graphs of entities we quite often found that it was cumbersome to use the Entity Framework to manually map all of the changes from an aggregate root to the database. By aggregate root I mean a bunch of models which are handled as one unit when updating/adding/deleting.

Below I will describe my proposed solution to this problem of automatically updating a detached graph consisting of multiple add/delete/update changes at any point in the graph. This should work for all sorts of graphs, and allows for updating entities with associated collections and single entities.

I find its always clearer with some code so lets try an example:

Say you have a Company which has many Contacts. A contact is not defined on its own and is a One-To-Many (with required parent) record of a Company. i.e. The company is the Aggregate Root. Assume you have a detached Company graph with its Contacts attached and want to reflect the state of this graph in the database.

At present using the Entity Framework you will need to perform the updates of the contacts manually, check if each contact is new and add, check if updated and edit, check if removed then delete it from the database. Once you have to do this for a few different aggregates in a large system you start to realize there must be a better, more generic way.

Well good news is that after a few refactorings I've found a nice solution to this problem. The proposed extension method below handles the whole diff for you in a nice convenient package.

using (var context = new TestDbContext())
{
// Update the company and state that the company 'owns' the collection Contacts.
context.UpdateGraph(company, map => map
.OwnedCollection(p => p.Contacts)
);
context.SaveChanges();
}

Using the above code a diff will be run between the provided company graph and one retrieved from the database. The bounds of the graph are defined by the mapping which above is the company entity itself and its child Contacts. Only entities within these bounds will be included in the database diff.

The retrieval code makes use of the provided mapping configuration to get all data needed in one query at the start of the process, thus making the process quite efficient.

From this diff the algorithm will add/update/delete depending on what action needs to be performed and commit all of these changes in one batch at the end of the algorithm.

There are 2 different scenarios that this extension method must cater for. One is the situation above where you have a One-To-Many or One-To-One where the right hand side of the relationship is owned by the parent. This is defined within the mapping as an OwnedCollection/OwnedEntity.

The other scenario is that you have a Many-To-Many, or a One-To-Many and the related record exists in its own right as it not a defined part of the aggregate you are updating. That scenario is defined as an AssociatedCollection/AssociatedEntity.

The mapping configuration can handle many complex scenarios with ease. For example:

I have a company aggregate which is made of address objects and contact objects. On the contact model there is a list of accepted advertising materials (List) which are managed elsewhere in the system and not a part of the company aggregate. The list of AdvertisingOptions that are associated with the contact still needs to be updated however when managing the company as an aggregate.

using (var context = new TestDbContext())
{
// Update the company and state that the company 'owns' the collection Contacts.
context.UpdateGraph(company, map => map
.OwnedCollection(p => p.Contacts, with => with
.AssociatedCollection(p => p.AdvertisementOptions))
.OwnedCollection(p => p.Addresses)
);
context.SaveChanges();
}

The associated collection line tells the update method that the actual entities that are AdvertisementOptions should not be marked as changed - even if the objects provided are different from the database values. We do however want to update the relation so that it reflects the fact that the contact has the provided AdvertisementOptions and this is performed during the update.

Hope this helps you as it did our team. If you would like to help build the project please leave me a comment.

]]>http://blog.brentmckendrick.com/introducing-graphdiff-for-entity-framework-code-first-allowing-automated-updates-of-a-graph-of-detached-entities/4e2d813e-3832-4cad-9863-2a48f160b5d9Tue, 11 Dec 2012 22:29:54 GMTIt's a little hard to see how to group by a sub type in Linq to Entities. Since the discriminating field (when using table per hierarchy inheritance) or table name (when using table per type inheritance) is of course not exposed past the ORM layer. This essentially leaves us with the type of the object to discriminate sub types.

If we try to use GetType() in a LINQ to Entites query we get an exception of the sort:
"LINQ to Entities does not recognize the method 'System.Type GetType()' method, and this method cannot be translated into a store expression."

On the other hand however EF can recognise the 'is' keyword. Now lets see how to group by using this new found knowledge.

Scenario: A PERSON is subdivided into ADULT, TEENAGER, etc like below:

Now how do I find the count of people who are adults and teenagers in one LINQ query? (without ToList() as this no longer performs the query in SQL)
The trick is to to use nested ternary operators inside of your GroupBy keySelector parameter using the 'is' keyword.

This would obviously give me an anonymous type (A1) which contains a property of List'(T) called Addresses where T is anonymous type (A2). The key here is that the type instantiated is in fact a List'(T)

Now lets say I have a set of data transfer objects. One is PersonDTO which has a property of type List'(T) where T is an AddressDTO as shown below.

I get an Exception : System.NotSupportedException - "LINQ to Entities does not recognize the method 'System.Collections.Generic.List1[t] ToList[t](System.Collections.Generic.IEnumerable1[t])' method, and this method cannot be translated into a store expression.

Now all of you LINQ to Entities guys are screaming "You can't call ToList() inside of a nested projection!". OK, I can see that the ToList() will not be able to be translated to an SQL query, obviously if I leave it out then my code will not compile since PersonDTO.Addresses is a List'(T). If I change the PersonDTO.Addresses field to an IEnumerable and remove the ToList() then it works! (and by reflecting the runtime type it is in fact populated with a generic list). So the list IS being instantiated anyway.

Now I know this projection could be done in memory and I would not have any problems calling ToList() because it would not need to be translated into an SQL query, however this defeats the purpose as I am trying to write an SQL query.

Now to the point!:

An anonymous type can be created with a nested collection property of type List.

A concrete type can be created with a nested collection of type IEnumerable and a List will be created and populated for this variable

A concrete type with a nested property collection of a List (or any derivative of type ICollection) can NOT be projected to (because of the ToList() issue)

Now it seems to me that since in all of these cases a List is created anyway that perhaps when the expression is mapped to an SQL function the call to the nested ToList() should be ignored? Simply by doing this all of the problems would be solved. (Please correct me if this assumption is incorrect). Better yet what if it WASN'T ignored but removed from the SQL expression and stored for later use so that we could also call ToArray(), ToList(), etc. and change the type which is being returned from the EF materialization? Eg.

]]>http://blog.brentmckendrick.com/entity-framework-tolist-in-nested-type-linq-to-entities-does-not-recognize-the-method/c95b814e-62fe-4400-98d5-099c4c2cfb91Fri, 20 Jul 2012 10:46:52 GMTSo a strange behaviour in the implementation of the HtmlHelper.DropDownListFor extension method was recently brought to my attention where the order of 'looking for' the selected value of the list seemed incorrect.

I would assume that the selected value would always be the selected value from the strongly typed model property (as I have clearly stated by providing a lambda which results in returning that property) but it seems the selected value will not always be the selected value from the strongly typed model as I would expect.

Given the above code what would the value of the drop down list be?
a) "Smith"
b) "Tim"
c) "Hello"

Well I would expect the SelectList constructor to create a list with a selected value of "Smith" then I would expect the DropDownListFor extension method to set the selected value to be "Hello" as provided by my model.

In fact what happens is that the value from the ViewBag is taken in priority over any others!
The answer to above is actually b).

To be honest using a strongly typed view I don't think I would ever want the extension method to take the value from the ViewBag implicitly.

Now if I remove the Title parameter in the ViewBag as done so below, what would you expect?

Using the above logic the value would be "Hello" which is correct. However this doesn't give us an option to override the selected value as the SelectList's selected overload does the work before handing the object off to the DropDownListFor extension method which it then ignores.

Finally if the ViewBag property is null AND my Model's property is null then the SelectList's selected value will be unchanged and the result will be "Smith".

Now I looked into TextBoxFor and EditorFor, etc and they work as expected. It is only the ListBoxFor and DropDownListFor extension methods which are first checking for the value in the ViewBag before the Model.

Just something everyone needs to be aware of. I would consider this a bug. Be sure to check your naming as we had a page title in the viewbag called "ViewBag.Title" and a "Person.Title" property on our strongly typed Person model. The select list tried to set the selected value of the list to be the page's title!.

]]>http://blog.brentmckendrick.com/asp-net-mvc-3-dropdownlistfor-strange-behaviour-for-determining-selected-option/da22bb1f-b84b-4c55-8e40-37c952a60a57Wed, 02 May 2012 13:29:03 GMTThe concept of Event Aggregation was first introduced to me as I read through the PRISM toolkit (Composite Application Guidance) from the Microsoft Patterns and Practises team.

It allows you to modularise your code and reduce dependencies between modules, I love it.
An example of how it benefits web applications would be:

Consider a simple 2 column web page layout where I have a div with content on the left (LeftBarRegion) and a div for the more content (MainContentRegion) on the right. Lets say that these two regions are defined in a Layout page (or Master Page) and the inner content of the MainContentRegion is changed by a JQuery ajax call. I have a LeftView and RightView which would fill the appropriate regions in my layout.

Now how do I get the view in the LeftBarRegion to talk to the view in the MainContentRegion and vice versa without knowing anything about the implementation details of each view? What if I also want to notify the parent layout page from either of the inner containers?

Welcome to the power of EventAggregator. We could have any content we like in either of the regions above and they can still talk to each other in a de-coupled fashion.

UPDATE: Thanks to Eli Weinstock-Herman for pointing out the fact that Find should return null if no result is found (SingleOrDefault instead of Single). Cheers Eli.

Hey guys,

I've been back in the coding seat lately creating a new generic repository for a system that we are building. I've made some improvements to the FakeDbSet that I posted about earlier Here.

I want to add some notes to the previous post which are long enough they warrant a new post. Firstly, IT IS MUCH EASIER if you do not use foreign keys in your objects but instead use 'association' object references. This means you will not have to co-ordinate two different fields when setting up test data. Of course EF does this for you when connected to the database but in memory you would have to do this yourself.

Secondly the implementation of find was quite hard, though I believe I have come up with an elegant generic solution. If you look at the IDbSet documentation MSDN you will see that Find() expects the keys to passed in "the same order that they are defined in the model".

If I use reflection to find my key properties I can then iterate through the keys and ensure that each object given in the find method equals the value of that key, as shown below.

Now of course this is being done in the Add method not the commit method as the database would. For my purposes this makes no difference. If however you want the key generation to be done on commit then you need to keep an un-comitted list inside of the FakeDbSet and then when commit is called you would iterate the list generating id's for each element and then adding them to the 'comitted' list.

]]>http://blog.brentmckendrick.com/generic-repository-fake-idbset-implementation-update-find-method-identity-key/277158ea-9c70-4886-be32-99e3a8f9b6d6Wed, 30 Nov 2011 09:29:29 GMTWe have run into an issue in the WSFederationAuthenticationModule class within WIF.

Basically the issue comes when the application requesting authentication from your STS sends a redirection url without a trailing slash. The slash cannot always be guaranteed as the return URI is created from the accessed application URI. Therefore if a user bookmarks the wrong URI or a link doesn't have the trailing slash you will get the above exception message.

In trying to fix this problem I had a look through the WSFederationAuthenticationModule's public event listings. None of the events that we could see occurred before the offending code in OnAuthenticateRequest().

The workaround listed on Microsoft Connect was quite bad as it forced us to check every single request in each RP to ensure that the trailing slash was in the URI, and if not redirect back to itself with the trailing slash.

I came up with a simpler solution to the problem which only needs a change to the STS itself. Since we couldn't get into a public event before the offending code we added a HttpModule to the pipeline and ensured it ran before the WS modules.

This custom http module simply checked the 'ru' return URI field (within the wctx entry in the query string) for a trailing slash and if none was provided redirected back to itself with the trailing slash in the 'ru' field.

I've updated the Microsoft Connect website for the new work around.

]]>http://blog.brentmckendrick.com/windows-identity-foundation-a-signinresponse-message-may-only-redirect-within-the-current-web-application/0a186884-7ae4-458e-8632-622d373830a8Mon, 14 Nov 2011 12:12:05 GMTWell I've been back from the holiday for a month or so. Been staying away from the blog for a while as I've had so much work to do since I got back that I need to prioritise.

I'm currently working on starting a new project in ASP.NET MVC 3 so you might see a few posts pop up about that. At the moment we are just going through the early stages of release planning and story estimation.

Stay tuned.

]]>http://blog.brentmckendrick.com/getting-back-into-it/d0d23d38-cad5-4e84-84be-a0e41bf7863cMon, 07 Nov 2011 12:45:17 GMTHi,
I've received messages and I know a few of you are waiting for a good tutorial on multi tenancy.

I've been busy working as I'm going on a trip and will be away for the next 6 weeks. So it's going to have to wait, we haven't gone live with my changes I've made at work yet as they are not complete however in development it is working very nicely.

I'll write up some posts when I get back. Basically my solution for the back-end involved:

MEF for drop-in client overrides of business logic in a services layer

EF 4.1 for POCO classes implementing an interface which allows for a hash set of key,value pair custom fields

This gives us a flexible back-end. As for the UI we have PHP, Webforms and MVC so that's taking quite a bit of work to change. There are already great tutorials on multi tenancy MVC so I'd refer to those.