Domain Models with BaseEntity or SEOEntity
First, a project named
Aaron.Domain be created, and then, I
install a package Aaron.Core by nuget. When the package installed successfully, I got a example as follow:
- Create a domain class named "Catalog" inherit from BaseEntity class from namespace Aaron.Core (using Aaron.Core) like that:
using Aaron.Core;
namespace Aaron.Domain.Catalogs
{
public class Catalog : BaseEntity<int>
{
public string CatalogName { get; set; }
public string Description { get; set; }
}
}
You see
BaseEntity<TKey> be inherited by
Catalog class. Of course, this is an important class, it includes a Indentity object in
TKey type, CreationDate and ModifiedDate datetime type, and some method utilities. Like example above,
Catalog class have
Id is
int type.
- Create a domain class named "Articles" inherit from SEOEntity like that:
using System;
using Aaron.Core;
namespace Aaron.Domain.Articles
{
public class Article : SEOEntity<Guid>
{
public string Title { get; set; }
public string Description { get; set; }
public bool? Published { get; set; }
}
}
In this example, I use
SEOEntity<TKey> with
TKey is
Guid struct (mean the
Id is
Guid). The
SEOEntity<TKey> is a class inherits from
BaseEntity<TKey>, it includes SEOUrlName, MetaTitle, MetaKeywords and MetaDescription properties.
- And now, to more interesting, I'll add a one to many (1-n) relationship, that means one catalog have more articles. So, the code will be changed as follows:
In Catalog class:
using System;
using System.Collections.Generic;
using Aaron.Core;
using Aaron.Domain.Articles;
namespace Aaron.Domain.Catalogs
{
public class Catalog : BaseEntity<int>
{
private ICollection<Article> _articles;
public string CatalogName { get; set; }
public string Description { get; set; }
public virtual ICollection<Article> Articles
{
get { return _articles ?? (_articles = new List<Article>()); }
set { _articles = value; }
}
}
}
In Article class:
using System;
using Aaron.Core;
using Aaron.Domain.Catalogs;
namespace Aaron.Domain.Articles
{
public class Article : SEOEntity<Guid>
{
public string Title { get; set; }
public string Description { get; set; }
public bool? Published { get; set; }
public int CatalogId { get; set; }
public virtual Catalog Catalog { get; set; }
}
}
You can read
more here about the
Relationships in Code First.
Summary, two basic examples above show you the concept of how use
BaseEntity and
SEOEntity to create a domain! And later, we shall discuss about use
Fluent API with Aaron to database mapping.