Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly implement rich domain model with Dependency Injection

I am trying to change my objects to Rich domain models. My original class, before trying to replace it with rich domain model, was like this:

public class StudentLogic : IStudentLogic
{
    private IUnitOfWork _uow;
    private IStudentRepository _studentRepository;

    public StudentLogic(IUnitOfWork uow, 
        IStudentRepository studentRepository)
    {
        _uow = uow;
        _studentRepository = studentRepository;
    }

    public int CreateStudent(IStudent newStudent)
    {
        return _studentRepository.Create(newStudent);
    }
}

With IStudent declared as:

public interface IStudent
{
    string FirstName { get; set; }
    string LastName { get; set; }
}

So now I try to convert to Rich domain model.

A student cannot exist without FirstName and LastName, so based on what I read about Rich domain model, this should be included in the constructor. My rich domain model for student would look like this:

public class Student : IStudent
{
    public Student(string firstName, string lastName)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }

    public int Create()
    {
        return _studentRepository.Create(this);
    }
}

How do I inject the UoW and Repository? It seems awkward to put it in the constructor along with the firstName and lastName. And is the pattern I'm following generally correct?

like image 380
g_b Avatar asked Dec 05 '25 07:12

g_b


1 Answers

Check that interfaces can't declare members with accessibility modifiers like public and they can't define fields but properties, events, methods... I've fixed this issue in your question editing it...

In the other hand, a rich domain model woudn't create a persisted domain object and the method representing the whole creation wouldn't be returning an integer but the created domain object.

About dependency injection, in C# and depending on the inversion of control container, you can either inject dependencies using constructor dependencies or property dependencies.

The whole point of using construction-time dependencies is that they're mandatory. If your domain object can't work without a repository and unit of work implementations, you'll want to require them in your domain objects' constructors. Otherwise, if some of the dependencies is optional, you can inject it using property injection:

public class Some 
{
    // Mandatory, because a non-optional constructor parameter
    // must be provided or C# compiler will cry
    public Some(IUnitOfWork uow) { ... }

    // Optional, because you may or may not set a property
    public IRepository<...> Repo { get; set; }
}

Finally, setting properties during construction-time isn't the requirement of a domain to be rich. What makes a domain model not anemic is that domain objects aren't just data storage but they provide behavior.

like image 82
Matías Fidemraizer Avatar answered Dec 07 '25 19:12

Matías Fidemraizer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!