0

My company wanted to implement audits for a SQL Server database. The result of combining quite a few really good solutions found on SO I wound up with the following:

An interface:

public interface IAuditable
{
    string CreatedBy { get; set; }
    DateTime? CreatedDate { get; set; }
    string CreatedIpAddress { get; set; }
    string ModifiedBy { get; set; }
    DateTime? ModifiedDate { get; set; }
    string ModifiedIpAddress { get; set; }
}

A BaseClass that implements the interface. The annotations are supposed to hide the properties from the designer:

public abstract class BaseModel : IAuditable
{
    [Browsable(false)]
    [Bindable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public string CreatedBy { get; set; }

    [Browsable(false)]
    [Bindable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public DateTime? CreatedDate { get; set; }
    <snip>
    ...
    </snip>
}

A domain model that derives from the BaseClass:

public class Person : BaseModel
{
    public int Id { get; set; }

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

I override SaveChanges() to populate the properties.

If I bind the collection to a datagridview it does not show these columns. However intellisense still offers them in a lambda Expression. How do I hide these properties from intellisense?

I have exposed the model to the UI (yes I know, bad practice). I was thinking of trying to hide these in what Julie Lerman calls a bounded context. But I think my SaveChanges() override won’t be able to populate the properties. OR I might need to push this to an SQL function like I do with SoftDelete. Any help will be appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Randy
  • 1,137
  • 16
  • 49

1 Answers1

1

Not quite sure why you would want to hide these from Intellisense, but you can implement your interface explicitly.

public abstract class BaseModel : IAuditable
{
    string IAuditable.CreatedBy { get; set; }

    DateTime? IAuditable.CreatedDate { get; set; }
}

These properties are still there, but will only be accessible in code if you are explicitly working with a variable or property defined as IAuditable.

TyCobb
  • 8,909
  • 1
  • 33
  • 53
  • Thanks for your answer. However when I explicitly implement these properties they are no longer part of the Database Table. I want to hide them from the Designer. The SaveChanges method should be able to modify them on save. But a UI developer should never even know they are there. – Randy Sep 16 '15 at 16:51
  • They are part of the object though. The UI developer should know to ignore those properties and if the UI does not call for showing that information they should be ignoring those properties anyway. Right? This is the only way to hide the properties from Intellisense. If this effect Entity Framework or any other ORM, then you really don't have a choice other than doing a ViewModel and not defining those properties. – TyCobb Sep 16 '15 at 16:57