Kiwi MVC.NET : Why this, why that ? I explain my architecture choices Part 5 [source on codeplex]
So, I work hard these days to finish my data access. I would like to explain you how I see application’s design.
As I told you before, I want to separate my application in 3 parts. I know that architecture could be discussed but I would make this point clear, it should be “re-factored”. Anyway, for the moment, I think it is enough flexible for what I want to do NOW.
Here is a reminder of my layers:
- The Web application which will be the view of my application.
- The controller with all my business logic embedded in a library Service.
- The model library which will contain my models, my data access (repository) and all my filter static classes.
To improve my explanations, I hand draw a little pseudo UML diagram :

What I want it is to keep as much as I can the “Soc” (separation of concern). My service should make what a service should do and if I want to change my repository with a Oracle one, I could change it only by modifying the instantiation of my service (with DI for the future). I want to keep that “philosophy” all along the development but philosophy is not obligation.
In the same way, you’ve certainly noticed that I often return an IQueryable for my “get” method (see listing 1). I know this should be better to return a List type cause it is a more “standard” and you do not have to use the Linq library. Nevertheless I think I will keep it. I do not want to call to often the .IEnumerable function and also it will be easier to use my filter classes.
As the only developer for this application, I assume my choices.
If you have remarks about this, please leave a comment.
Listing 1 : Example of the getPages method who retreive all the page from my db
public IQueryable<Page> getPages()
{
return from p in _db.Posts
where p.Type == "Page"
let lp = getLocalizedPages(p.PostID)
select new Page
{
PageID = p.PostID,
Author = p.Author,
Date = p.Date,
PageLozalized = new LazyList<PageLocalized>(lp),
ModifiedDate = p.ModifiedDate,
Password = p.Password ?? string.Empty,
Permalink = p.Permalink,
Status = p.PostStatusID,
Version = p.RevisionOf
};
}
private IQueryable<PageLocalized> getLocalizedPages(int postID)
{
return from lp in _db.PostCultureDetails
where lp.PostID == postID
select new PageLocalized()
{
Description = lp.Excerpt,
Text = lp.Text,
Title = lp.Title,
LanguageCode = (from culture in _db.Cultures
where culture.CultureID == lp.CultureID
select culture.LanguageCode).SingleOrDefault()
};
}
Why I did not use Entity framework ? I hate when the framework decide for me which object I should use. Also, the time I would spend to make it work as I want is doubtless longer than the time I wrote my custom Linq to Sql Class. Entity framework could be great for simple application but for an application who will grow in the future (as I expect Kiwi will do) I do not think it’s a good choice.
Why I did not use NHibernate ? I could have chosen NHibernate, but I absolutely wanted to play with Linq. Also, maybe in the future I could make the data access with it and compare what’s better for my application. Anyway, this is not the time to do this.
If you have other questions like this, please, do not hesitate.
And now the service :
As I mentioned, my data access is almost finished (for the moment). I am going to create the services I need for my application. Evidently, always in a TDD approach.
Kiwi Part 4 was bullshit :
Maybe it was but the aim of that post was to show you how to start with TDD. And I know that I was going in front of a wall. Indeed, I had an empty db and the post object was the most connected object. As you know in TDD, you write a test, you write some code. I realized that I should have written a lot (before creating the “SaveFunction” for it). I needed the category object, the comment object,… Thus, I begin to create a save function for the category object (alway with a tdd style).
Create the test :
//Spike Test !!!
[TestMethod]
public void SqlPostRepositoryTest_can_add_category()
{
Category category = new Category();
category.CreatedDate = DateTime.Now;
category.CategoryParentID = 0;
var categoryEn = new CategoryLocalized() {Description = "This is a test", Value = "Software Development", LangageCode = "EN"};
var categoryFR = new CategoryLocalized() { Description = "Ceci est un test", Value = "Développement Logiciel", LangageCode = "FR" };
var list = new LazyList<CategoryLocalized>();
list.Add(categoryEn);
list.Add(categoryFR);
category.LocalizedCategory = list;
_repository.Save(category);
Assert.IsNotNull(category);
}
And after the function I need :
//save or add a category
public void Save (Category category)
{
var dbCategory = (from c in _db.Categories
where c.CategoryID == category.CategoryID
select c).SingleOrDefault();
if(dbCategory == null)
{
dbCategory = new SqlRepository.Category();
dbCategory.CreatedDate = DateTime.Now;
dbCategory.ParentID = category.CategoryParentID;
//commit the change to have the id and insert the culture
_db.Categories.InsertOnSubmit(dbCategory);
_db.SubmitChanges();
//foreach culture we have in the objec we add it in the table CategoryCultureDetail
foreach (CategoryLocalized item in category.LocalizedCategory)
{
var dbitem = new CategoryCultureDetail();
//some Object to DB
dbitem.CategoryID = dbCategory.CategoryID;
dbitem.Description = item.Description;
dbitem.Value = item.Value;
dbitem.CultureID =
(from culture in _db.Cultures
where culture.LanguageCode == item.LangageCode
select culture.CultureID).SingleOrDefault();
dbCategory.CategoryCultureDetails.Add(dbitem);
_db.SubmitChanges();
}
}
else
{
foreach (CategoryLocalized item in category.LocalizedCategory)
{
var thisCategoryLocalized = (from cn in _db.CategoryCultureDetails
where cn.CategoryID == category.CategoryID
where cn.CultureID == (from culture in _db.Cultures
where culture.LanguageCode == item.LangageCode
select culture.CultureID).SingleOrDefault()
select cn).SingleOrDefault();
thisCategoryLocalized.Description = item.Description;
thisCategoryLocalized.Value = item.Value;
_db.CategoryCultureDetails.InsertOnSubmit(thisCategoryLocalized);
_db.SubmitChanges();
}
}
}
Of course, you see the “final” result. To have that, I went by little piece of code. Test, write, build, test, write, build, test …
I also know the test you see above is not a unit test, it is more an integration test or a spike test and it will be deleted or commented in the future.
But the name is test driven design and not “make pure unit test” design.
NOTE : I know I am saying that each time I post but I will try to add the source code in my codeplex project this afternoon.
UPDATE : I put the source on code plex, if you would like to see it : http://dervalp.codeplex.com/
I have some troubles with codeplex, I will check this out tomorrow cause I am a bit tired today.
Filed under: Kiwi MVC on May 29th, 2009








Leave a Reply