Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4614414
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:41:48+00:00 2026-05-22T01:41:48+00:00

I am delving into domain events and need some advice about persisting updates to

  • 0

I am delving into domain events and need some advice about persisting updates to an entity for history reasons. My example deals with a User entity and Signing In:

    public class UserService
    {
        private UserRepository _repository;

        public UserService()
        {
            _repository = new UserRepository();
        }

        public User SignIn(string username, string password)
        {
            var user = _repository.FindByUsernameAndPassword(username, password);
            //As long as the found object is valid and an exception has not been thrown we can raise the event.
            user.LastLoginDate = DateTime.Now;
            user.SignIn();
            return user;
        }
    }

    public class User
    {
        public User(IEntityContract entityContract)
        {
            if (!entityContract.IsValid)
            {
                throw new EntityContractException;
            }
        }

        public Guid Id { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public DateTime LastLoginDate { get; set; }

        public void SignIn()
        {
            DomainEvent.Raise(new UserSignInEvent() {User = this});
        }
    }

    public class UserSignInEvent : IDomainEvent
    {
        public User User { get; set; }
    }


    public class UserSignInHandler : Handles<UserSignInEvent>
    {
        public void Handle(UserSignInEvent arguments)
        {
            //do the stuff
        }
    }

So where I have the do the stuff, I want to update the User object LastLoginDate and possibly log the date and time the user logged in for historical reasons.
My question is, would I create a new instance of my repository and context to save the changes in the handler or pass something into the Event? This is what I am struggling with right now.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:41:49+00:00Added an answer on May 22, 2026 at 1:41 am

    So where I have the do the stuff, I want to update the User object LastLoginDate and possibly log the date and time the user logged in for historical reasons.

    Remembering last login date should be concern of user itself.
    You already have nice extension point – user has signIn method.

    My question is, would I create a new instance of my repository and context to save the changes in the handler or pass something into the Event?

    User shouldn’t know anything about entity framework.
    Therefore – User.Events shouldn’t know anything either.
    Domain event handlers shouldn’t know too.

    Those handlers that live “outside” (e.g. in application layer) are allowed to.
    But they would figure out entity framework context from elsewhere and not from user or events if necessary.


    As I see it – events here are necessary for logging functionality only.

    I would write something like this:

    public class LoginService{
       private Users _users;
       public LoginService(Users users){
         _users = users;
       }         
       public User SignIn(string username, string password){
         var user = _users.ByUsernameAndPassword(username, password);
         user.SignIn();
         return user;
       }
     }
    
    public class User{
       public DateTime LastLoginDate { get; set; }      
       public void SignIn(){
         LastLoginDate = DateTime.Now;
         Raise(new SignedIn(this));
       }
       public class SignedIn:DomainEvent<User>{
         public SignedIn(User user):base(user){}
       }
    }
    
    //outside of domain model
    public class OnUserSignedIn:IEventHandler<User.SignedIn>{
      public void Handle(User.SignedIn e){
        var u=e.Source;
        var message="User {0} {1} logged in on {1}"
          .With(u.Name,u.LastName,u.LastLoginDate);
        Console.WriteLine(message);
      }
    }
    

    Bad thing about this code is that service method is command and query simultaneously
    (it modifies state and returns result).

    I would resolve that with introducing UserContext which would be notified that user has signed in.

    That would make need for returning signed in user unnecessary,
    responsibility of serving current user would be shifted to UserContext.

    About repository and updating Your user – I’m pretty sure entity framework is smart enough to know how to track entity state changes. At least in NHibernate – only thing I’m doing is flushing changes when httprequest finishes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is about making an architectural choice prior to delving into the details
I'm trying to learn python and delving into string functions. As a simple example,
I'm delving into some AJAX and I'm trying to utilise jQuery. I have an
While delving into some old code I've stumbled upon a function which is used
I've done some delving into AppleScript but I'm interested in how difficult and long
I have lately been delving into F# and the new DSL stuff as in
I have recently begun delving into the world that is database programming with C++
I'm just delving into Objective C and Cocoa Touch and I'm trying to build
I have been delving into C# recently, and I wonder if anyone would mind
I'm working on delving into more complex WordPress theme options pages. Right now, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.