Yaplex
Yaplex

Reputation: 2284

What architecture would be better

Could you suggest what would be better from architecture point of view and why in this two examples. In the first I have a separate class which responsible for business logic related to object and object does not have any methods, it's just data there. In second example object knows how to save himself (using same repository) and all business logic located inside.

I have an application where first is implemented, but it's too much services to deal with objects, would not it be easy to re-write it to second sample? I just trying to understand if it will work or give me more problems.

First:

class Person{
public stirng FirstName {get;set;}
}



class PersonService<Person> : IService<Person>{
    ctor (IRepository<Person> repository){
        _repository = repository;
    }
    public void Save(Person p){
        // business logic there
        _repository.Save(p)
    }
}

Second:

class Person{
public stirng FirstName {get;set;}

    ctor(IRepository repository){
        _repository = repository;
    }

    public void Save(){
        // business logic there
        _repository.Save(p)
    }
}

Upvotes: 1

Views: 70

Answers (1)

Alex G.
Alex G.

Reputation: 949

As requested, moved from comments to answers

As I understood, you're concerned about anemic domain model you have to deal with. I'd recommend you to read first (there's a link to Fowler's post). It's considered to be an anti-pattern, but it doesn't mean that you need to rewrite your system. It takes months/years to master DDD and nobody can really guarantee a successful outcome of such redesign especially if the system is large. If anemic model works for you, stick to it. If it doesn't, then start using DDD.

Upvotes: 1

Related Questions