Visual studio feeds

All Visual Studio blogs in one place

Subscribe

Enter your email address:

Delivered by FeedBurner

Increase your website traffic with Attracta.com

Google+

Contact

Email:
Subject:
Message:
Anti-spam: How many eyes has a typical person?

Follow us on FB

Search

C# and MongoDb tips – Part 3

AddThis Social Bookmark Button

Changing values ina collection
You can update/ save values in a collection using the Save, Update or FindAndModifymethods. The Save method is a combination of Insert and Update. If theId member of the document has avalue, then it is assumed to be an existing document and Save calls Update on thedocument. Otherwise it is assumed to be a new document and Save calls Insert afterfirst assigning a newly generated unique value to the Id member.
[TestMethod]
public voidSaveShouldSaveTheChangedEntityToCollection()
{
    var database = GetDatabaseInstance();
    var collectionSettings =database.CreateCollectionSettings("Employees");
   collectionSettings.SlaveOk = true;
    var employees =database.GetCollection(collectionSettings);
    var query = Query.And(Query.EQ("FirstName","Betty"), Query.EQ("LastName", "Green"));
    var employee = employees.FindOne(query);
   employee.FirstName = "Rachel";
   
employees.Save(employee);
    query = Query.And(Query.EQ("FirstName", "Rachel"),Query.EQ("LastName","Green"));
    employee= employees.FindOne(query);
    Assert.IsTrue(employee != null&& employee.FirstName == "Rachel");
}
The Update method is used to update existing documents.
 [TestMethod]
public voidUpdateShouldUpdateTheChangedEntitiesMacthingTheQueryBuilderConditionToCollection()
{
    var database = GetDatabaseInstance();
    var collectionSettings =database.CreateCollectionSettings("Employees");
   collectionSettings.SlaveOk = true;
    var employees =database.GetCollection(collectionSettings);
    var query = Query.And(Query.EQ("FirstName","Rachel"), Query.EQ("LastName","Green"));
    employees.Update(query, Update.Set("FirstName","Betty"));
    query = Query.And(Query.EQ("FirstName", "Betty"),Query.EQ("LastName","Green"));
    var employee = employees.FindOne(query);
    Assert.IsTrue(employee != null&& employee.FirstName == "Betty");
}
FindAndModifyalways updates a single document, and you can combine a query that matchesmultiple documents with sort criteria that will determine exactly whichmatching document is updated. In addition, FindAndModifywill return the matching documentsand if you wish you can specify which fieldsof the matching document to return.
[TestMethod]
public voidFindAndModifyShouldFindAndModifyTheCollectionInAtomicOperation()
{

C# and MongoDb tips – Part 2

AddThis Social Bookmark Button

Filtering acollection
To retrieve documents from a collection use one of the variousFind methods. FindOne returns the first document it finds (when there are manydocuments in a collection you can't be sure which one it will be).
[TestMethod]
public voidFindOneShouldReturnTheFirstEntryInTheCollection()
{
    var database = GetDatabaseInstance();
    var collectionSettings =database.CreateCollectionSettings("Employees");
   collectionSettings.SlaveOk = true;
    var employees =database.GetCollection(collectionSettings);

    var firstEmployee = employees.FindOne();
    Assert.IsTrue(firstEmployee.Id != default(int));
}

If you want to read a document that is not of the type use the FindOneAs method, which allows you tooverride the type of the returned document.
[TestMethod]
public voidFindOneAsShouldReturnTheFirstItemInTheCollectionOverridingTheTypeOfReturnedDocument()
{
    var database = GetDatabaseInstance();
    var employees = database.GetCollection("Employees");
    var employee = employees.FindOneAs(typeof (Employee));
   
Assert.IsTrue(employee != null&& employee is Employee);
}

The Find and FindAs methods take a query that tells theserver which documents to return. The query parameter is of type IMongoQuery.IMongoQuery is a marker interface that identifies classes that can be used asqueries. The most common ways to construct a query are to either use the Querybuilder class.
[TestMethod]
public voidFindOneShouldReturnTheFirstItemInTheCollectionBasedOnQuery()
{
    var database = GetDatabaseInstance();
    var collectionSettings =database.CreateCollectionSettings("Employees");
   collectionSettings.SlaveOk = true;
    var employees =database.GetCollection(collectionSettings);
    var queryDocument = newQueryDocument("FirstName","Betty");
    var employee = employees.FindOne(queryDocument);
    Assert.IsTrue(employee != null&& employee.FirstName == "Betty");
}

[TestMethod]
public voidQueryBuilderCanBeUsedToFilterEntriesInCollection()
{
    var database = GetDatabaseInstance();
    var collectionSettings =database.CreateCollectionSettings("Employees");
   collectionSettings.SlaveOk = true;
    var employees =database.GetCollection(collectionSettings);
    var query = Query.And(Query.EQ("FirstName","Betty"),...(Read whole news on source site)

jQuery Samples

AddThis Social Bookmark Button
Here are the jsfiddle samples that John Papa and I covered in our jQuery Fundamentals workshop at DevConnections last week. These were a few of the samples we wrote on the fly (so they’re not “perfect”) using http://jsfiddle.net and wanted to share. Additional jQuery samples covering selectors, DOM manipulation, Ajax techniques, as well as sample applications can be found here. You can also view the talks John gave at the conference here.  Code and slides from my talks can be found at the following links:

InfoPath, custom WCF Service, Word and Open XML SDK (outline)

AddThis Social Bookmark Button
This upcoming series of articles promise to be far more exciting than the title. Scenario How do you use an InfoPath form document to populate a word document with content controls.    Steps: Building a Word document template with Content Control place holders Use Content Control toolkit to bind values to place holders in the template to an embedded custom XML (hey, isn't an InfoPath document an XML file?) Creating a WCF service to take a Word template document, and replace the custom XML, by
the power of Open XML SDK (2.0, of course). Invoking the WCF service from within an InfoPath document to generate a word template version of itself! Finally, a bit of discussion on where Word Automation services fits in the bigger picture, as well as thoughts on best practices.
Drop a comment below if you have any specific questions relating to these steps.  Specifically, if you can think of a far better title for this series. Otherwise, stay tuned!  :-)...(Read whole news on source site)

What’s new with T4 in Visual Studio 11 Beta?

AddThis Social Bookmark Button
Our beta release has been available for a couple of weeks now, so I thought it was about time I updated folks on what’s new for T4. Everything below is in addition to the features and changes I noted in my previous post, What’s new with T4 in the Developer Preview of Visual Studio 11? OK, here goes… Directive processors are MUCH easier to build and more flexible
Directive processors are T4’s plug-in mechanism.  They allow you to inject code into a template
to pass and initialize parameters, or provide standard constructs within a template in a language-agnostic manner. However, they’ve been a bit fiddly to set up in the past and haven’t seen a lot of use.  This time, we’ve made them much cleaner and easier. Directive processors can now be deployed as simple MEF components in a standard Visual Studio VSIX container. This means you can easily publish them on the Visual Studio gallery. There are new attributes [DirectiveProcessor] and [SupportedDirective] to make this easy. Look out for...(Read whole news on source site)

9 jQuery Samples

AddThis Social Bookmark Button
These are the jsfiddle samples that Dan Wahlin and I covered in our jQuery Fundamentals workshop at DevConnections last week. These were samples we wrote on the fly in http://jsfiddle.net and wanted to share. Enjoy! Additional jQuery samples covering selectors, DOM manipulation, Ajax techniques, as well as sample applications can be found here. You can view my other sessions from DevConnections and Orlando Code Camp here: Whirlwind Tour of Building HTML5 and JavaScript Apps with MVVM and Knockout JsRender Fundamentals: Templating for HTML5 Applications Fundamentals of Windows 8 XAML/C# Metro Style
Apps View more of my presentations here.   Using the Data Function   Using Object Literals with jQuery   Using jQuery each() with string concatenation   Using on() to handle child events   jQuery - hover   jQuery - event handling variations   jQuery - Twitter (bind, append, appendTo, each, fadeOut, $.getJSON, callback, success, error, complete)r   jQuery - attr vs prop   jQuery - Simple selectors
...(Read whole news on source site)

Home