Kiwi MVC.NET : Prepare your data access Part3

26 May
2009

So, I decide to rebuild a new solution. I will carry on the membership layer later cause I want a system who could use Sql, Active Directory or OpenID for the login and only use Sql for the membership role. I have an idea to do that but as I told you in a previous post, a security layer is a kind of plugin. Therefore, we could add it later.

Today, I will show you how I am going to create the models and prepare my data access with the help of LinqToSql class.

I choose to make Kiwi multilingual.

1) The DB

I am not a dba, you are welcome with your feedback.

Here is the design I choose :

kiwi_db

2) The models

I decided to create five objects to begin the project. An object Post, Page, Comment, Tag and Category.

  • The object Post : It is a content of a news (for the moment). A post must be in a category. There are status for this type. A post cannot have subposts. This object is localized
  • The object Page : It cannot be a part of a category, the page can have subpages. This object is localized and have the same status (puslihed, offline,…) than a post object.
  • The object Comment : A classic comment object, with the author name, the ip address,… A comment can have child (qutoes). This object is not localized (for the moment, we could think of an automatic translation but KISS).
  • The object Category :  This object contains the post. It is localized by the name.

Example for Page:

I use the LazyList pattern (more info here). I will put the project on codeplex, like this, you will able to browse and download the code.


namespace KIWI.MVC.Data
{
class Page
{
public int PageID { get; set; }
public string Author { get; set; }
public string Status { get; set; }
public DateTime Date { get; set; }
public DateTime ModifiedDate { get; set; }
public string Version { get; set; }
public string Permalink { get; set; }
public string Password { get; set; }
public LazyList<Page> PageChild{ get;set;}
public LazyList<Page> PageLozalized { get; set; }
}
public class PageLocalized
{
public string Title { get; set; }
public string Text { get; set; }
public string Excerpt { get; set; }
public string LanguageCode { get; set; }
}
}

3) The SqlCMSRepository

This repository will be the link between my object and my db. It will contain all the main method I need. I will always return an IQueryable type.  It will allow you to use the pipeline pattern to create some filter functions.

  • Create a LinqToSql class.

kiwi_linq_to_sql

After creating the linq to sql class, drop the table you need from the server explorer of visual studio to the dml file.

kiwi_class_cms

You will have the representation of you db (like the first page of this post). After, change some properties of you LinqCMSRespository class. Double clik on the LinqCMDRespository.dbml and right click on the screen where your tables appear. Here change the Name to “DB”. It is only to use the same name for you future dbml and improve the visibilty of your code.

kiwi_class_linq_properties

Build you solution to see if everything goes well.

If you have built successfully, then create you SqlCMSRespository class.

Here is the code :

using CMS.MVC.Data.SqlRepository;

namespace CMS.MVC.Data
{
class SqlCMSRespository
{
//the name we changed in the properties of our Linq to Sql class
private DB _db;

//constructor
#region .ctor

//we instanciate the db when we instantciate the SqlCMSRespository
//default constructor
public SqlCMSRespository()
{
_db = new DB();
}

public SqlCMSRespository(DB dataContext)
{
//override the current context with the on passed in
//for TDD
_db = dataContext;
}

#endregion

#region Post
//there we will put our main functions like GetPost(),...
//But before, let's go to TDD !!!!
//TEST DRIVEN DESIGN
#endregion
}
}

Now we are able to begin all our data access method but before writing some code in your repository, we’ll go with the TDD approach.
And by creating our tests, we will see what we need in our application.

So this is the end for today, as usual, feedback and comments are very appreciated.

Comment Form

top