Home
Gallery
GuestBook
SourceCode
Contact

DDDBlog Implementation. Part 2

Posted in DDD
This post has been read 6566 times

In my previous post, we looked at setting up the project structure and the tools and libraries that we’ll need to build the application. In this post we’ll look at creating a few base classes.

Layer SuperType: The Entity Base Class

Before we start of creating the domain objects, lets first create the base entity object. As discussed before, an entity is something that has an identity. This would be our layer supertype because this class will be inherited by all our domain objects that needs to have an identity.

namespace DDDBlog.Infrastructure.BaseClasses

{

    using System;

 

    public interface IEntity

    {

        int Key { get; }

    }

 

    public abstract class Entity : IEntity

    {

        private int key;

        public int Key

        {

            get { return key; }

        }

 

        protected Entity()

        {

        }

 

        protected Entity(int key)

        {

            this.key = key;

        }

 

        public bool Equals( Entity other )

        {

            return Equals( this, other );

        }

 

        public override bool Equals( object obj )

        {

            return Equals( this, obj as Entity );

        }

 

        public static bool Equals( Entity obj1, Entity obj2 )

        {

            if ( Object.Equals( obj1, null ) || Object.Equals( obj2, null ) ||

                obj1.GetType() != obj2.GetType() )

                return false;

 

            if ( ReferenceEquals( obj1, obj2 ) ) return true;

 

            return obj1.Key == obj2.Key;

        }

 

        public override int GetHashCode()

        {

            return Key.GetHashCode();;

        }

    }

}

The Entity base class is defined in the Infrastructure layer as the definition of entity is not part of the domain itself but it will be used by the domain. As you can see the Entity class has a key property which would be used as an identity. Also I am taking the key to be integer for simplicity. I have also implemented the equality comparison on the entity object. This is used to compare the entities.

Entity Tests

Now lets see some tests on the Entity class. A thing to remember is that in TDD, we would have to write the tests firsts and then come at the object.

namespace DDDBlog.Tests.Infrastructure.Tests

{

    using DDDBlog.Infrastructure.BaseClasses;

    using Xunit;

 

    public class FakeEntity : Entity

    {

        public FakeEntity()

            : base( 0 )

        {

        }

 

        public FakeEntity( int key )

            : base( key )

        {

        }

    }

 

    public class EntityTests

    {

        [Fact]

        public void Can_Create_An_Entity()

        {

            var entity = new FakeEntity();

            Assert.NotNull( entity );

        }

 

[Fact]

        public void Newly_Created_Entity_Id_Should_Be_Zero()

        {

            var entity = new FakeEntity();

            Assert.Equal( entity.Key, 0 );

        }

 

        [Fact]

        public void Two_Entities_With_Same_Key_Should_Be_Equal()

        {

            var entity1 = new FakeEntity( 21 );

            var entity2 = new FakeEntity( 21 );

 

            Assert.Equal( entity1, entity2 );

            Assert.Equal( entity1.GetHashCode(), entity2.GetHashCode() );

        }

 

        [Fact]

        public void Two_Entities_With_Diffrent_Key_Should_Not_Be_Equal()

        {

            var entity1 = new FakeEntity( 21 );

            var entity2 = new FakeEntity( 3 );

 

            Assert.NotEqual( entity1, entity2 );

            Assert.NotEqual( entity1.GetHashCode(), entity2.GetHashCode() );

        }

 

 

        [Fact]

        public void Reference_To_Same_Entity_Should_Be_Equal()

        {

            var entity1 = new FakeEntity( 21 );

            var entity2 = entity1;

 

            Assert.Equal( entity1, entity2 );

            Assert.Equal( entity1.GetHashCode(), entity2.GetHashCode() );

        }

    }

}

AggregateRoot And IRepository<T>

Along with the Entity base class, we also need a repository base, and a way to mark an entity as an aggregate root. These are shown in the following snippets

namespace DDDBlog.Infrastructure.BaseClasses

{

    public interface IAggregateRoot : IEntity { }

}

namespace DDDBlog.Infrastructure.Repository

{

    using System.Linq;

    using BaseClasses;

 

    public interface IRepository<T> where T : IAggregateRoot

    {

        T FindBy( int key );

        IQueryable<T> FindAll();

        void Save( T entity );

        void Delete( T entity );

    }

}

As you can see our Aggregate root base is just an interface that inherits from IEntity. This is done because a repository is ( should be ) provided only on the Aggregate root. You can read a bit on Aggregates and Repositories here.

Now that our base classes are ready, we can look at the domain objects of our sample application, which I will be talking about in the next post.

You must know that, I am by no means an expert in DDD, TDD. The code shown above might be wrong as I am just stepping into this part of the world. Please leave a comment/ suggestion if you find something to be wrong or if it could be done better.




Comment posted on Thursday, June 14, 2012 7:46 AM
Internet is the best place where you can gather information about the poor credit card offers.When a person searches on the Internet with the keywords “zero fee bad credit cards”,then he will get a thousand of results about the companies which come with such offering.
Comment posted on Saturday, June 30, 2012 5:00 PM
Thank you for this great information, you write very well which i like very much. I am really impressed by your post.
Comment posted on Monday, August 06, 2012 4:45 PM
Thanks a lot for sharing this code.From last few days i was searching for this code but was not getting it in the proper format.I have bookmarked your post for further reference.
Comment posted on Tuesday, August 21, 2012 11:34 PM
Keep silent let everybody thought that you are a fool than speak to let everyone make sure you are a fool is much better.
Comment posted on Tuesday, October 02, 2012 12:49 PM
I went through the above post and code.One thing i didn't understood why you have over ride the method GetHashCode?
Comment posted on Wednesday, November 07, 2012 11:46 AM
This is really very informative post.I liked the way you have presented the information.Hope you continue sharing such post in near future.
Comment posted on Thursday, January 24, 2013 9:58 PM
Thank you for this great information, you write very well which i like very much. I am really impressed by your post.
Comment posted on Thursday, March 20, 2014 1:51 AM
I don't think you should put IEntity inside the Infrastructure layer. The concept of separation here is based on the Onion architecture. The core, which is your domain layer, should not depend on anything outside of it. Your domain layer is your abstraction, your infrastructure layer is your implementation of said abstraction for a particular infrastructure. IEntity is an abstraction, not an implementation, it should be a part of your core. You can tell this pretty simply, your Domain layer should be able to compile without a reference to your Infrastructure layer, if it doesn't, you broke the onion.
Comment posted on Thursday, March 20, 2014 1:51 AM
I don't think you should put IEntity inside the Infrastructure layer. The concept of separation here is based on the Onion architecture. The core, which is your domain layer, should not depend on anything outside of it. Your domain layer is your abstraction, your infrastructure layer is your implementation of said abstraction for a particular infrastructure. IEntity is an abstraction, not an implementation, it should be a part of your core. You can tell this pretty simply, your Domain layer should be able to compile without a reference to your Infrastructure layer, if it doesn't, you broke the onion.
Name:
E-mail:
Website:
Comment:
 
Anti Bot Image:

Insert Cancel


Subscribe

Random Photo

My Tweets


Top Posts

Source Code

The source code to this site is open-source. You can download the code from here.

Categories


Recent Blogs


Archives


Blogroll