Reputation: 1378
I read a lot about auto-incremental id (guid, interlocked.increment, ObjectIdGenerator...) but don't find nothing for my situation.
In my domain model user ask to have an automatic progressive numeric Id for each Activity they create.
Since it's a user request I want to put it in my domain model, but the mode I usually do it in older application without good architecture ) is accessing database, retrieve the max and add 1; so I can't do it in my object since domain layer must not be aware of db.
Don't like db identity for lack of control (sometimes db administrator have to change the id for user error on creation of activity).
interlocked.increment look fine but my application is installed on every user machine so I can't use it
Since it must be intelligible and progressive I can't use guid
I find a good idea in Lev Gorodinski article about Service Domain in Domain-Driven-Design: define the interface of a GenerateActivityId in domain layer as a Domain Service, but I don't find a way to make a good implementation of it.
Any suggestion?
EDIT: Lev Gorodinski idea:
public class Activity {
public int Id {get; private set;}
public string Description {get;set;}
public Activity (string description){
this.Description = description
this.Id = generator.GenerateId()
}
}
public interface IIdGenerator{
int GenerateId()
}
but i don't see where "generator" is defined and don't found IIdGenerator implementation: where should i put the implementation? In ActivityRepository? If yes i can omit IIdGenerator for the IActiviryRepositoryInteface ?
Upvotes: 2
Views: 994
Reputation: 1378
My idea:
Domain Layer
public class Activity {
public int Id {get; private set;}
public string Description {get;set;}
public Activity (string description){
this.Description = description
var activityRepository = kernel.Get<IActivityRepository>();
this.Id = activityRepository .GetMaxId() + 1
}
}
public interface IActivityRepository{
public int GetMaxId();
}
Data Access layer
public class ActivityRepository
{
pulic int GetMaxId()
{
// query for retriving max id from table
}
}
Somewhere (where?) i define dependecy injection with Ninject
var kernel = new StandardKernel();
kernel.Bind<IActivityRepository>().To<ActivityRepository>();
Upvotes: 1