Search code examples
c#separation-of-concernsn-tier-architecture

Multi-tier Application Design


Before everyone throws stones at me, I have searched Google / MSDN / StackOver flow for related questions and answers but none of them suited my needs.

I'm working on a rather large application in C# - Windows Forms that is currently divided into the following:

  1. Data-Layer
  2. Domain-Layer
  3. UI-Layer

Basically in my current situation the roles of this layers are the following

  1. The Data-Layer's responsability is to communicate with the data-store, basically CRUD operations.
  2. The Domain-Layer's responsability is to hold the model of our objects, create the objects, apply business-rules etc.
  3. The UI-Layer, well, basically this is what the user sees and interacts with.

My problem is the following:

From the UI Layer the user has access to fields like: Name, Project Name, Project Number which basically are TextBoxes, Calendars etc - all of them are UI Components.

After the input of the user I call a method named: AddExplorerNode(string name, string projectName, int projectNumber) which resides in the Domain-Layer. This method is responsible based on the passed parameters to create an ExplorerNode Object ( a "special" TreeNode ) which requires the passed parameters to actually be valid.

After the object has been created, sanitized, validated etc - the same method mentioned above passes the created object to the Data-Layer which pushes it to a Cache-Repository and then to persists it to the data-store if everything went OK in the Cache.

So until now, basically everything is separated UI -> Domain -> DataLayer.

My question is, could I replace the signature of the Domain method from

AddExplorerNode(string name, string projectName, int projectNumber) to AddExplorerNode(TreeNode node) and based on the TreeNode object and its properties, construct the actual object I need ? I'm asking this because, if the Domain-Layers knows about the UI ( in this case the TreeNode UI Component ) basically we break the separation.

For example, if next year we swap WindowsForms to a Console Application, then the project is broken due to the fact that a Console Application will not have a TreeNode UI Component.

In this case, is it better to have a domain method which takes for example 5-10 parameters ( int's, strings, etc ) and based on those parameters to create my object or to replaces the parameters with a TreeNode UI Component which ?

Thank you in advance.

@EDIT:

I am asking this question, because a colleague of mine reviewed my code and started to refactor it. By refactoring he was exposing the actual TreeNode UI Component to the Domain-Layer. My approach was AddExplorerNode(string name, string projectName, int projectNumber) etc.


Solution

  • You can make your own class that acts in a similar way to TreeNode without actually being a TreeNode.

    class TreeNodeModel
    {
        public string Name { get; set; }
        public string ProjectName { get; set; }
        public int ProjectNumber { get; set; }
    }
    

    Then you can write a method in the UI to map (copy) TreeNodeModel to an actual TreeNode.