Learn Ethical Hacking, SQL Injection and XSS Script.

Protect your website from SQL Injection, XSS Script and avoid vulnerability.

Learn SharePoint 2010,MOSS and Convert ASP.Net application to SharePoint

Learn Dotnet,SQL,NO-SQL ,C#,VB.NET,Java Script,Dotnet Open source project.

Check the free hosting in LINUX and Windows Server

Showing posts with label nosql. Show all posts
Showing posts with label nosql. Show all posts

Raven DB, A Kick Starter using Tier 3 IaaS


I’m putting together a pretty sweet little application. It does some basic things that are slowly but surely expanding to cover some interesting distributed cloud computing business cases. But today I’m going to dive into my Raven DB experience. The idea is that Raven DB will act as the data repository for a set of API web services (that seems kind of obvious, I state the obvious sometimes).
The first thing we need is a server instance to get our initial node up and running. You can use whatever service, virtualization tools, or a physical server if you want. I’m going to use Tier 3′s Services in my example, so just extrapolate for your own situation.
First I’ve logged in to the Tier 3 Control Site and am going to create a server instance.

Building the Tier 3 Node for Raven DB

Creating the Server Instance (Click for full size image)


Next step is to assign resources. Since this is just a single Raven DB node, and I won’t have it under heavy load, I’ve minimized the resources. This is more of a developers install, but it could easily be a production deploy, just allocate more resources as needed. Also note, I’ve added 50 GB of storage to this particular instance.
Setting Resources (Click for full size image)
Setting Resources (Click for full size image)
Now that we’ve set these, click next and on the next screen click on server task. Here add the public IP option and select the following services to open their ports.
Setting up a Public IP and the respective ports/services (Click for full size image)
Setting up a Public IP and the respective ports/services (Click for full size image)
The task will display once added as an item on the create server view. Once that is done, clickcreate server so the server build out will start.
Creating the Server (Click for full size image)
Creating the Server (Click for full size image)
Now log in with RDP to start setting up the server in preparation of loading Raven DB. The first thing you’ll want to do is go ahead and get Windows Update turned on. My preference is to just turn it on and get every update that is available. Once that is done, make sure to get the latest .NET 4 download from Windows Update too.
Getting Windows Update Turned On (Click for full size image)
Getting Windows Update Turned On (Click for full size image)
Once all of the updates are finished and .NET 4 is installed we’ll get down to the business of getting Raven DB Installed. In this specific example I’ll be installing the Raven DB as a windows service, it however can be installed under IIS so there are many other options depending on how you need it installed.

Installing Raven DB

To get the software to install, navigate over to the Raven DB site at http://ravendb.net/ from the new instance we’ve just spun up. Click on the Download button and you’ll find the latest build over on the right hand side. Click to download the latest software package to a preferred location on the system.
Raven DB - Open Source 2nd Generation Document DB (Click to navigate to the site)
Raven DB – Open Source 2nd Generation Document DB (Click to navigate to the site)
Once you’ve downloaded it (I’ve put my download in the root R:\ partition I created) unzip it into a directory (I’ve just unzipped it here into R:\ to make the paths easy to find, feel free to put it anywhere you would prefer. In our Tier 3 environment the R drive is on a higher speed, thus higher IOP drive system, thus the abilities exceed your standard EBS/AMI or S3 style storage mechanisms.).
Saving the Raven DB Download (Click for full size image)
Saving the Raven DB Download (Click for full size image)
Saving to R:/ (Click for full size image)
Saving to R:/ (Click for full size image)
At this point, open a command prompt to install Raven DB as a service. Navigate to the drive and folder location you’ve saved the file to. Below I displayed a list of the folder and files in the structure.
CLI actions (click for full size image)
CLI actions (click for full size image)
Once you’re in the path of the Raven.Server.exe file then run a slash install on it to get a Windows Service of the Raven DB running.
Raven DB Installation results (click for full size image)
Raven DB Installation results (click for full size image)
To verify that it is up and running (which if you’ve gotten these results, you can rest assured it is, but I always like to just see the services icon) check out the services MMC.
Launching services (Click for full size image)
Launching services (Click for full size image)
There it is running…
Now, you’re not complete yet. There are a few other things you may want to take note of to be sure you’re up and running in every way you need to be.
The management and http transport for Raven DB is done on port 8080. So you’ll have to open that port if you want to connect to the services of the database externally. On windows, open up the Windows Firewall. Right click on the Inbound Rules and click Add Rule.
Select Port (Click for full size image)
Select Port (Click for full size image)
Select the Raven.Server.exe (click to see full size)
Select the Raven.Server.exe (click to see full size)
Inbound Rule (Click for full size image)
Inbound Rule (Click for full size image)
Open up however needed. (Click for full size image)
Open up however needed. (Click for full size image)
Public Private etc. (Click for full size image)
Public Private etc. (Click for full size image)
Enter a name and description on the next wizard dialog screen and click on Finish.
Displayed active firewall rule (Click for full size image)
Displayed active firewall rule (Click for full size image)
Now if you navigate to the IP of the instance with port 8080 you’ll be able to load the management portal for Raven DB and verify it is running and you have remote access.
Raven DB Management Screen
Raven DB Management Screen (Click for full size image)
At this point, if you’d like more evidence of success, click on the “Create Sample Data” button and the management screen will generate some data.
Raven DB Management console with data (Click for full size image)
Raven DB Management console with data (Click for full size image)
At this point you have a live Raven DB instance up and running in Tier 3. Next step is to break out and add nodes for better data integrity, etc.
Summary
In this write up I’ve shown a short how-to on installing and getting Raven DB ready for use on Windows Server 2008 in Tier 3′s Enterprise Cloud environment. In the very near future I’ll broach the topics of big data with Raven DB, and other databases like Riak and their usage in a cloud environment like Tier 3. Thanks for reading, cheers!

Easily connect to RavenDB data via C#


Working with the .NET client API

The easiest way to interface with a RavenDB document store is through the .NET client API. The API is accessed via the Raven.Client.Lightweight-3.5.dll file, which can be found in the Client-3.5 directory of your RavenDB installation. (The version number — 3.5 in this case — may change with future RavenDB releases.) If you’re using Visual Studio, you add a reference to this DLL in your project. Once this reference is added, a using statement can be used to add a reference within the code (e.g., using Raven.Client).
With the reference added, there are two steps to working with the document store:
  1. Connect to the document store: You connect to your RavenDB document store instance. It is recommended that one document store instance is created per application.
  2. Create a session: You establish a session via the document store. You should create a session instance each time you have to interact with the document store.
Once the document store and session objects are instantiated, you may interact with your RavenDB installation; this may include adding documents, updating documents, querying the data store, and so forth. The following code snippet instantiates the document store and session objects using my local RavenDB installation.
var documentStore = new Raven.Client.Document.DocumentStore { Url = "http://localhost:8080" };
documentStore.Initialize();
using(var session = documentStore.OpenSession())
{
// Work with document store
}
The code uses the complete object path to demonstrate where they are located within the namespace, but as previously mentioned, this can be shortened via the using keyword in the code header. The first line creates an instance of the DocumentStore class using the Web address of the RavenDB installation. The next line instantiates the DocumentStore object with the following line (using statement) creating a session via the DocumentStore. With the session created, you can easily interact with the RavenDB document store.

Working with documents

As we explored in my previous Software Engineer post, RavenDB is a document-centric data store. As an example, we will store website information in our RavenDB backend. The website URL will be stored along with a title and notes. The following C# class will map this to our document type.
public class Site
{
public string Title { get; set; }
public string URL { get; set; }
public string Notes { get; set; }
}
The code to add a document builds upon the earlier example where a session is created. The code takes it a bit further by adding a document (using the class) to the store. Once the session is established, an instance of the Site class is created and populated with the data to be stored. The session Store method stores the new document in the session. You can create multiple documents and store them until you’re ready to save the changes. The SaveChanges method puts the data in the RavenDB instance.
var documentStore = new Raven.Client.Document.DocumentStore { Url = "http://localhost:8080" };
documentStore.Initialize();
var site = new Site {
Title = "CNet TV",
URL = "http://cnettv.cnet.com",
Notes = "Tech shows"
};
using(var session = documentStore.OpenSession())    {
session.Store(site);
session.SaveChanges();
}
Figure A shows the document added to the data store via the RavenDB Web interface.
Figure A
Document added to RavenDB displayed in the Web interface (Click the image to enlarge.)
It is interesting to see what is added to the document metadata (see the Metadata tab in Figure A), as well as what the following listing shows; RavenDB uses the namespace of the application (RavenDB1) and the class name of the entity (Sites) to assign metadata properties.
{
"Raven-Entity-Name": "Sites",
"Raven-Clr-Type": "RavenDB1.Site, RavenDB1"
}
An interesting aspect of adding this document is how do you identify it after it is added? That is, in the relational world, there are key values on tables that serve many purposes with one assigning a unique identifying value to a row of data. While RavenDB isn’t relational, it does utilize key values to identify a document, and this can easily be incorporated into the code. The first step is adding a string Id property to our object. Once this is added, you do not populate it when a document is created, but RavenDB will populate it with an identifying value. The following console application demonstrates this approach.
namespace RavenDB1 {
public class Site  {
public string Id { get; set; }
public string Title { get; set; }
public string URL { get; set; }
public string Notes { get; set; }
}
class Program  {
static void Main(string[] args)  {
var documentStore = new Raven.Client.Document.DocumentStore { Url = "http://localhost:8080" };
documentStore.Initialize();
var site = new Site { Title = "CNet TV", URL = "http://cnettv.cnet.com", Notes = "Tech shows" };
using(var session = documentStore.OpenSession())  {
session.Store(site);
session.SaveChanges();
Console.WriteLine("ID: " + site.Id);
}  }  }  }
The final line displays the Id value of the newly created document (sites/193 in my case); this isn’t the only way to work with ID values — an overview is available online. While adding documents is useful, the real power in a data store is querying the data. The RavenDB platform utilizes the LINQsyntax, so once again it does not follow the traditional RDBMS world where SELECT statements are used. The following example builds upon the previous code to pull all documents from the data store that have a value of CNET as its title.
var documentStore = new Raven.Client.Document.DocumentStore { Url = "http://localhost:8080" };
documentStore.Initialize();
using(var session = documentStore.OpenSession())  {
var sites = session.Query<Site>()
.Where(x => x.Title == "CNet")
.ToList();
for (int i = 0; i < sites.Count; i++)   {
Console.WriteLine(sites[i].Title + " - " + sites[i].URL);
}  }
The session Query method is used to issue the LINQ command, which includes a Where to define what to select, and the ToList method converts the results to a GenericList object. Finally, a for loop is used to loop through the results, which are displayed in the console.

Starting point

As a developer who has worked with relational database systems for years, working with RavenDB has been a pleasant experience with some uneasiness as I wade through a less rigid system. The .NET client API included with a RavenDB installation makes it easy to get up and running with the document store in no time. The simple examples in this post build the groundwork for much more advanced integration.
Have you jumped on the NoSQL bandwagon, or does it make you uneasy? Are you currently using RavenDB or an alternative? Share your thoughts and experiences with the community.

Building an ASP.NET MVC app using RavenDB


In a previous article, I had demonstrated how we could introduce RavenDB as an auto-save cache for information that resides in a traditional RDBMS. Today we are going to look at how we can use it as the only backing store of an ASP.NET MVC application.
Note: If you are new to NoSql or RavenDB or Document Databases, make sure you read Hello RavenDB! Introducing RavenDB for .NET Developers
Using RavenDB as a backing store is actually very easy. In a development environment, all you have to do is install using Nuget package manager and start the Raven.Server.exe from the packages folder. However in a production environment, you may or may not be able to run an additional HTTP server for yourself. If you have a hosting provider where your site is shared with multiple other sites, you may not have the access to run a separate HTTP server. Beauty of RavenDB is that even in such a constrained environment, you can easily run RavenDB and not require SQL Server backend. RavenDB has special embedded variant that allows you to run RavenDB in medium trust.

In this article, we will see how to build an ASP.NET MVC app using RavenDB Embedded and then publish it to a hosted source.
Again for simplicity purposes we will take our tried and tested Blog example. The blog data model is simple enough to do quickly but deep enough to explain basic relationships.
1: Start with a usual ASP.NET MVC, Internet project template
create-new-project
2. Add RavenDB (Embedded) reference using the Package Manager (Tools > Extension Manager).
add-raven-embedded-reference
3. Review the License Agreement. RavenDB is NOT free for commercial use. Accept the license to complete installation.
accept-raven-license
4. Add a class library project to the solution. Call it RaBlog.Service.
5. Modeling the Blog Document database. To start of with we will consider a very simple model
image
- Add all the classes in a Model folder.
Note: The ‘Comment’ object is typically not a part of the Blog Post update. We will see how we have to use a workaround because of this. In a future post, we will see how to use a ‘denormalized’ document structure. For now, let’s focus on using the embedded version to publish a live site.
6. With the Model out of the way, let's look a different way of implementing persistence layer, one without a repository pattern.
RavenDB backing store is represented by the DocumentStore object. It implements the IDocumentStore interface. Every 'connection' to the DocumentStore is a RavenDB DocumentSession. This implements the IDocumentSession interface. To build our RavenDB access layer we will do the following
- Create a RavenController class that inherits from the MVC's Controller class.
add-raven-controller
- We will inject the instance of IDocumentStore and instead of using the MVC provided HTTPServerUtility.Session object we will hide it by using the new keyword and add a Session of type IDocumentSession.
override-mvc-session-by-raven-doc-session
- We override the OnActionExecuting method. This is called just before the action begins execution.
We will use OpenSession in this method. OpenSession is akin to document connection open in ADO.NET. So just before the Action Execution starts we open a connection to our RavenDB store
override-on-action-executing
- We override the OnActionExecuted method. This is called once the action is executed. In this method we will check if there is an error, if not, the document session is saved. This saves whatever entities we put in the RavenDB's document Session.
override-on-action-executed
- The simplicity of this pattern is we don't have to explicitly call the RavenDB's document Save method in any of our controllers. Any object we put in the session will get persisted once the action completes. On the flip side, this couples data persistence to the View layer. So use it with caution.
7. With our base controller ready we move on to the HomeContoller and change it to inherit from RavenController instead of Controller and add a constructor.
override-home-controller-to-inherit-from-raven
8. We load a single 'row' of data from RavenDB by making the following call in our controller.
get-single-blog-from-raven
Example here is of the Details action that pulls up a particular Blog Post. The Session property is here the same as the one we defined in RavenController earlier. It is of type IDocumentSession. Hear the Load<T> method loads the required Document type from RavenDB. In our case it's the Blog with the id as provided in the id parameter.
9. Similarly, entire list of Blogs can be loaded as follows
load-all-blogs-from-raven
10. Updates are recorded as follows. We load the existing Blog from the session again, using the current blog’s id. Update the Title and Content and store it into the Session. Remember the ActionExecuted override from earlier? That gets called once this method finishes and thus Save is called which persists changes to the database.
home-controller-post-edit-action
Note: RavenDB’s document session does not do partial updates to the document. If we directly put the blog object passed to the action method in session store, since it does not have the related Comments, RavenDB will assume the comments have been deleted and will do the same in the database.
In future we will see how to ‘denormalize’ objects and resolve this issue.
11. I am not detailing the UI changes for Index/Add/Edit/Delete. They are standard MVC strongly typed views. I have made some HTML/CSS changes to make them look a little pretty. However we will look at the Details page where we will show the Comments that were made for the blog.
12. Implementing the Details view.
- The Details view will be actually the entire post and hence will present users with the opportunity to view existing comments and add their own comments if required.
- We update the Details.cshtml to show the user comments that have already been posted
render-all-comments-in-details-view
- Next we add a Partial View called CreateComments.cshtml. We can use the Add New View wizard and Select the ‘Comments’ Entity and the ‘Create’ template
image
- We make some cosmetic changes to make it look nicer. The final markup for CreateComments.cshtml is as follows:
create-comments-partial-view
- Now update the Details.cshtml to show the Partial View in the Details Page. We pass a new instance of the Comment object with the current Blog Id to the child partial view.
include-partial-view-in-details
- In the Controller we need to handle the POST from the CreateComments view.
- We add the following code to handle the POST
post-details-action
  • We load the current blog instance from the Session.
  • Check if the blog previously contained comments or not. If not initialize it.
  • Add the comment object and store the entire blog into the Session.
  • When this method completes the overloaded ActionExecuted method is called which saves the changes to the Store.
13. Finish off with the composition root and controller factory to inject the DocumentStore object. You can review the theory behind this pattern in my DI Introduction post.
14. Point of interest for us in this is the CreateControllerFactory() method in CompositionRoot.
if-debug-in-create-controller-factory
15. As seen above we are creating an instance of the EmbeddableDocumentStore() and giving it the ConnectionStringName. The connection string is defined in web.config. It is added when you install RavenDB from the package manager. The value looks as follows:
raven-db-embedded-connection-string
16. Notice the single statement inside a compiler directive allowing it to be applicable only for DEBUG builds.
if-debug-use-embedded-httpserver
This enables an embedded HttpServer that allows you to run the RavenDB console. Setting this to true requires your account to have netsh access to the port on which it tries to run. This may not be possible in production so we are disabling the admin console for now. In my case, my host does not allow netsh to port 8080 and if the pre-processor directives are not there you will get an Access Denied exception at run-time.
Even in Dev environment if Visual Studio is not running in Administrator mode you will get the above error. Solution for it is to run the following command in an Administrator mode, console:
netsh http add urlacl url=http://+:8080/ user="<your domain>\<your user id>"
17. With the Application built and ready to roll, switch to a Release build and build the project. Publish it to your production server through your established processes. I used Solution Explorer > RaBlog > Right Click > Publish to bring up my publish profile and push the changes using my publishing profile.
publish-profile
18. Hit publish and wait for the upload to complete. Navigate to the URL location and you should see the Home Page. There you go, you have successfully created and published an application using embedded RavenDB. This sample application is running at http://blog.sumitmaitra.com/
Note: If you are getting the yellow screen of death, turn off custom errors and checkout the error details.

Conclusion

We saw how we can use Embedded RavenDB to build and publish our own ASP.NET MVC application with very little effort.
The entire source code of this article can be downloaded over here

RavenDB on the Cloud

Hibernating Rhinos (the authors of RavenDB) have just launched a public beta of RavenDB on the cloud called RavenHQ. Currently the beta is available only for apps deployed to AppHarbor. Check it out if you want to.
Disclaimer: www.dotnetcurry.com is not associated with Hibernating Rhinos or AppHarbor.


Raven DB Documentation


Connecting to a RavenDB data store

As we have seen, RavenDB can run in one of two modes: a client/server mode, where communication is made via HTTP; and an embedded mode, in which the client API makes direct calls against the Database API.
The recommended mode for RavenDB to run in is the server mode. We discuss the various deployment options for the server mode later in the documentation.
Since a document store instance is not cheap to create but is thread-safe, the general suggestion is to have one instance of it per-database per-application.
In either mode, when the application shuts down, the document store instance(s) used should be disposed to ensure proper cleanup.

Running in server mode

To run in a server mode, add a reference to Raven.Client.Lightweight.dll to your application, and after launching the server instance separately connect to it using the following code:
?
1
2
var documentStore = new DocumentStore { Url = "http://myravendb.mydomain.com/" };
documentStore.Initialize();


Where http://myravendb.mydomain.com/ is your the RavenDB server's address.
This will instantiate a communication channel between your application and the RavenDB server instance in the network address you provided.

Running in embedded mode

When running in embedded mode, the data store is actually a server instance running on top of a local data directory, as opposed to connecting to a separate server instance.
To have this, you will need the entire EmbeddedClient folder from the build package in your solution. Note that you cannot use RavenDB Embedded on the Client Profile, you have to change the project properties to use the full .NET framework profiler.
After referencing Raven.Client.Embedded.dll, you need to initialize a new instance of EmbeddableDocumentStore. This is done by passing the path to the directory that the database resides in to the EmbeddableDocumentStore (the database will be created if it doesn't exists yet):
?
1
2
var documentStore = new EmbeddableDocumentStore  {  DataDirectory = "path/to/database/directory"  };
documentStore.Initialize();

Silverlight support

If you are accessing a RavenDB instance from Silverlight, you are going to need the Silverlight folder from the build package, and add a reference to all the DLLs in it to your application.
Using Silverlight, you can only access an external RavenDB server; there's still no embedded RavenDB implementation for Silverlight available to public use. You initialize a document store in Silverlight using:
?
1
2
var documentStore = new DocumentStore { Url = "http://myravendb.mydomain.com/" };
documentStore.Initialize();
Where http://myravendb.mydomain.com/ is your the RavenDB server's address.

Using a connection string

To make things even simpler, the RavenDB Client API supports .NET's named connection strings. You can use that by setting the ConnectionStringName, and the RavenDB client will initialize automatically based on the connection string's parameters:
?
1
2
3
4
new DocumentStore
{
  ConnectionStringName = "MyRavenConStr"
}
You can then define the connection string in the app.config:
?
1
2
3
4
5
<connectionStrings>
  <add name="Local" connectionString="DataDir = ~\Data"/>
  <add name="Server" connectionString="Url = http://localhost:8080"/>
</connectionStrings>
RavenDB connection string format is:
  • DataDir - run in embedded mode, specify which directory to run from. This requires that you'll initialize anEmbeddableDocumentStore, not just DocumentStore.
  • Url - for server mode only, specify where to locate the server.
  • User / Password - for server mode only, the credentials to use when accessing the server.
  • Enlist - whatever RavenDB should enlist in distributed transactions. Not applicable for Silverlight.
  • ResourceManagerId - Optional, for server mode only, the Resource Manager Id that will be used by the Distributed Transaction Coordinator (DTC) service to identify Raven. A custom resource manager id will need to be configured for each Raven server instance when Raven is hosted more than once per machine. Not applicable for Silverlight.
  • Database - for server mode only, use a specific database, not the default one. Using this will also ensure that the database exists.
The following are samples of a few RavenDB connection strings:
  • Url=http://ravendb.mydomain.com - connect to a remote RavenDB instance at ravendb.mydomain.com, to the default database
  • Url=http://ravendb.mydomain.com;Database=Northwind - connect to a remote RavenDB instance at ravendb.mydomain.com, to the Northwind database there
  • Url=http://ravendb.mydomain.com;User=user;Password=secret- connect to a remote RavenDB instance at ravendb.mydomain.com, with the specified credentials
  • DataDir=~\App_Data\RavenDB;Enslist=False - use embedded mode with the datbase located in the App_Data\RavenDB folder, without DTC support.

Configuration

Conventions

The RavenDB Client API uses several conventions to control how it works, these can be modified at the DocumentStore level.
  • FindIdentityProperty - Tell the RavenDB Client API how to find the property serving as the id property (the one holding the document key). Defaults to property named Id.
  • FindTypeTagName - Find the tag name for the entity, a tag name is the collection name in which an entity will be enrolled, as well as the default entity key namespace. Defaults to the plural of the entity type. (An entity of type Post would be Posts, an entity of type Person would be People, etc).
  • GenerateDocumentKey - Allows you to control the generation of keys for new entities. The rules for returned values follow the Raven document key generation strategies. By default, RavenDB concatenate the type tag name with an increasing numeric id (posts/1, posts/2, post/3, etc).
  • IdentityPartsSeparator - A string that allows you to customize part of the document key generation. By default, Raven uses "plural_entity_name/id", which some users don't like because it makes putting the document key in the URL harder in some cases. You can set this to another value (such as "-"), which would generate: "plural_entity_name-id". This is an alternative to replacing the whole document key generation process.
Reference:http://ravendb.net