I'm working on an application. I'm using a mixture of TDD and DDD. I'm working hard to separate the layers of my application and that is where my question comes in.

My solution is laid out as follows

    MyApp.Domain (WinRT class library)
        Entity (Folder)
                IPost.cs (Interface)
            BlogPosts.cs(Implementation of IPost)
        Service (Folder)
                IDataService.cs (Interface)
            BlogDataService.cs (Implementation of IDataService)
    MyApp.Presentation(Windows 8 XAML + C# application)
        MainPage.xaml (Contains a property of BlogViewModel
    MyApp.Tests (WinRT Unit testing project used for my TDD)

So I'm planning to use my ViewModel with the XAML UI

I'm writing a test and define my interfaces in my system and I have the following code thus far.

    public void Get_Zero_Blog_Posts_From_Presentation_Layer_Returns_Empty_Collection()
        IBlogViewModel viewModel = _container.Resolve<IBlogViewModel>();


        Assert.AreEqual(0, viewModel.BlogPosts.Count, "There should be 0 blog posts.");


viewModel.BlogPosts is an ObservableCollection<IPost>

Now.. my first thought is that I'd like the LoadBlogPosts method on the ViewModel to call a static method on the BlogPost entity.

My problem is I feel like I need to inject the IDataService into the Entity object so that it promotes loose coupling. Here are the two options that I'm struggling with:

  1. Not use a static method and use a member method on the BlogPost entity. Have the BlogPost take an IDataService in the constructor and use dependency injection to resolve the BlogPost instance and the IDataService implementation.

  2. Don't use the entity to call the IDataService. Put the IDataService in the constructor of the ViewModel and use my container to resolve the IDataService when the viewmodel is instantiated.

So with option one the layers will look like this

ViewModel(Presentation layer) -> Entity (Domain layer) -> IDataService (Service Layer)


ViewModel(Presentation layer) -> IDataService (Service Layer)

migrated from stackoverflow.com Nov 25 '12 at 15:01

This question came from our site for professional and enthusiast programmers.


I am not sure I understood eveything you wrote. I prefer my domain object not to know too much about persistence objects, but there is a huge debate on this in the ddd community. This is how I do things usually :

The viewModel takes a dependency on a application service (eg : BlogService). The bogService takes a dependency on a repository service ( eg: RepositoryBlog).

blogService is used to fetch data through its repository, and start the action of the aggregate root. The dependency injector framework might take care of all your dependency injection.

When thinking about injection of dependence, you might think of the lifetime of your object. if the dependency equals or is greater than the object, think ctor, otherwise, you might prefer injection through the method.

pseudoCode with Article as the aggregateroot:


ViewModel (IArticleService service) 

public PostNewArticle(string postText, Guid idUser)
    _service.PostNewArticle(postText, idUser)


ArticleService(IRepositoryArticle repository )
    _repository = repository;

public PostNewArticle(string postText, Guid idUser)
    var user= _repository.GetUser(idUser);

    var article = user.CreateArticle(postText);




public Article CreateArticle(string text)
       throw new Exception("user not active")

     return new Article(text);


Article(string text)
    _text= text;

Hope it helped.

  • Yes that does help. Thank you for your reply. I've actually found that using the Layer Model functionality in Visual Studio 2012 to be very effective. Thanks again your assistance. – Buddy James Nov 26 '12 at 1:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.