Signum Framework Logo
"Open framework that encourages convention over configuration, using C# code,
not XML files, to model at the right level of abstraction and achieve deadlines.
...but also has a full Linq provider, and syncs the schema for you!"
Login
RSS

Search

»



Main
Index
Map
Videos
Download
Source code
Tutorials
Forum
FAQ



Image



PoweredBy

Introduction

Constructor static class is used internally by Singnum.Windows any-time an Entity needs to be constructed (A create button is pressed).

Why worry? Wasn't entity constructor designed for that purpose?

Actually yes, but maybe you need to make some initialization when the entity is constructed on the client (not in the server):

  • Initialize some DateTime fields to DateTime.Now.
  • Set the current user as the owner (or creator) of the entity.
  • Initialize collections or Embedded Entities.

In the old days of .Net 1.0 / 1.1 / 2.0 was a common pattern to initialize collections and sub-entities on the constructor, or initialize them lazily when they are accessed through properties. Nowadays object and collection initializers make it much more convenient to just keep it null on the constructor and let client code set sub entities and collections manually, maybe using Linq queries, using a more functional style.

We found this approach simple, but it has the downside of you having to initialize them on the client side, where the user mainly is, not your code, which builds the entity.

Fortunately EntityControls initializes the collections for you, but you have to take care of Embedded Entities manually.

How it works

Constructor is an static class that is used as a holder of a ConstructorManager object.

This ConstructorManager contains a dictionary like this:

   public Dictionary<Type, Func<Window, object>> Constructors;

This dictionary allows you to override the code to build an entity of an specific type, this way:

    Constructor.ConstructorManager = new ConstructorManager
    {
        Constructors = new Dictionary<Type, Func<Window, object>>
        {
             {typeof(BugDN), win=>new BugDN{ Discoverer = ((UserDN)Thread.CurrentPrincipal).Developer, Start = DateTime.Now }}
        }
    };

With this declarative syntax we are telling Signum.Windows that every time a BugDN needs to be built, initialize the Discoverer with the current user DeveloperDN, and the start date to now.

If no constructor is provided, the entity will be created using its' default constructor.

AddFiltersProperties

The default constructor, increases the user's productivity a bit, when the entity is built from a SearchWindow, and it contains filters with:

  • 'equals' as the operator.
  • A column with the same name and a compatible type of some entity's property.

Then this property gets initialized with the current filter value. This is convenient because if you're looking for a employee from the Financial department, you don't find it, and you create it, then the department property gets automatically initialized to the Financial department.

You can disable this behaviour for a type just by overriding it with the default constructor:

{typeof(BugDN), win=>new BugDN()}

Also, you can add these capabilities to your custom constructor by using AddFilterProperties method:

{typeof(BugDN), win=> AddFilterProperties( win, new BugDN{ Discoverer = ((UserDN)Thread.CurrentPrincipal).Developer, Start = DateTime.Now })}

remember you can still write your own custom constructor by implementing the Creating event on a particular EntityControl
Creative Commons License Signum Framework Site by Signum Software is licensed under a Creative Commons Attribution 3.0 License.
Powered by ScrewTurn Wiki version 3.0.5.600.