开发者

How can I enter in date and username automatically when updating storage?

开发者 https://www.devze.com 2023-03-23 07:05 出处:网络
I have the following class that\'s used by my MVC3 application. I would like to simplify the updating of the class so that when a new class object is

I have the following class that's used by my MVC3 application. I would like to simplify the updating of the class so that when a new class object is created then the Created and CreatedBy fields get set automatically.

I'd also like to make it so开发者_如何学编程 that the Modified and ModifiedBy fields get updated automatically.

Is there a way that I could do this?

The class is used in MVCnamespace Storage.Models
{
    public class Topic : TableServiceEntity
    {
        [DisplayName("Partition Key")]
        public override string PartitionKey { get; set; }
        [DisplayName("Row Key")]
        public override string RowKey { get; set; }
        [DisplayName("Description")]
        public string Description { get; set; }
        public DateTime Created { get; set; }
        public DateTime Modified { get; set; }
        public String CreatedBy { get; set; }
        public string ModifiedBy { get; set; }
    }
}


Set the defaults in the constructor for the class

public class Topic 
{
    public Topic()
    {
        this.Created = DateTime.Now;
        this.CreatedBy = UserName;
    }
    [DisplayName("Partition Key")]
    public override string PartitionKey { get; set; }
    [DisplayName("Row Key")]
    public override string RowKey { get; set; }
    [DisplayName("Description")]
    public string Description { get; set; }
    public DateTime Created { get; set; }
    public DateTime Modified { get; set; }
    public String CreatedBy { get; set; }
    public string ModifiedBy { get; set; }
}


jonathan,

Jason's answer above with the logic contained within the constructors is a perfectly valid and clean way of doing this and I wouldn't argue with that (and have done it myself for more 'static' properties). However, given that there could be a timelapse between the creation of the object and the actual save, then you may also want to consider putting this logic into your controller (or service layer).

this would look roughly like this:

public ActionResult Create(MyCreateViewModel viewModel)
{
    if (ModelState.IsValid)
    {
        viewModel.Entity.Created = DateTime.UtcNow;
        _myService.Insert(viewModel.Entity);
        _myService.SaveChanges();
        return this.RedirectToAction(x => x.Index());
    } else {
        PopulateViewModel(viewModel);
        return View(viewModel);
    }
}

likewise, you may have a LastEdit datetime that you want to track. use the Edit action similarly:

public ActionResult Edit(MyEditViewModel viewModel)
{
    if (ModelState.IsValid)
    {
        viewModel.Entity.LastEditDate= DateTime.UtcNow;
        _myService.AttachAndUpdate(viewModel.Entity);
        _myService.SaveChanges();
        return this.RedirectToAction(x => x.Index());
    } else {
        PopulateViewModel(viewModel);
        return View(viewModel);
    }
}

just another approach to ensure that datetime related properties are truly reflected.


In this solution i think you may have to make big change in your Repository/Service layer

define an Interface like :

public interface IHistoryLog
{
   DateTime Created { get; set; }
   DateTime Modified { get; set; }
   string CreatedBy { get; set; }
   string ModifiedBy { get; set; } 
}

then:

public class Topic:IHistoryLog
{
// Implement interface..
}

then create a generic service class:

     public abstract class CRUDService<TModel>
        {
           protected CRUDService(DataContext dataContext)
           {
             // data context to do generic CRUD stuff
           }
           public virtual Save(TModel model) 
           {
              if(model is IHistoryLog)
              {
                 // assign Createdby and Created
              }
           }
           public virtual Update(TModel model) 
           {
              if(model is IHistoryLog)
              {
                // assign ModifiedBy and Modified
              }
           }
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消