Default values of class fields in DART - oop

I know it's a silly question but I'm stuck on it for quite a while now.
How to set default values of class members in DART.
This is how i provide the default values of the members but it's always null.
If these values are provided in constructor then it should use those values otherwise use the default value.
class BillingInfoDetails {
bool billToClient = false;
String clientName = "";
String reasonOfTravel = "";
String remarks = "";
BillingInfoDetails({
this.billToClient,
this.clientName,
this.reasonOfTravel,
this.remarks,
});
}

Like this:
class BillingInfoDetails {
bool billToClient;
String clientName;
String reasonOfTravel;
String remarks;
BillingInfoDetails({
this.billToClient = false,
this.clientName = "",
this.reasonOfTravel = "",
this.remarks = "",
});
}
You can find this information in the Dart Tour Guide: https://dart.dev/guides/language/language-tour#parameters in section "Default parameter values".

Related

MVC Model Object losing value

ok... I'm Stumped
public ActionResult addSite(SiteViewModel aModel)
{
if (ModelState.IsValid)
{
siteID = Guid.NewGuid().ToString();
aModel.siteId = siteID;
AddSite2Azure();
return RedirectToAction("manageProfile", "User");
}
else { return View(aModel); }
}
private void AddSite2Azure()
{
EmPmSiteEntity aSite = aEnty.AssetRegistry.CreateSite(new EmPmSiteEntity()
{
UserId = aUserId,
Id = aModel.siteId,
Name = aModel.siteName,
ZipCode = aModel.siteZip,
});
}
When Debugging, aModel.siteID has a guid at the end of my actionResult. But when we get to the next method, the value of aModel.siteID is "null"
It appears as though you have two scopes for aModel - one at the class level (not shown in your code), and one at the method level (passed as a parameter to addSite(...)).
You're setting the value of the method-level variable in addSite(). To use this value in AddSite2Azure(), either pass the method-level aModel to AddSite2Azure(), or set the class-level aModel in addSite() by using this.aModel.

NHibernate Dynamic Component Default Value Issue

All of my entities (that are mapped to a database table) inherit from an entity class with a dynamic component on it called Attributes e.g.:
public abstract class Entity<T> {
public virtual T Id { get; set; }
private IDictionary _attributes;
public virtual IDictionary Attributes {
get { return _attributes ?? (_attributes = new Hashtable()); }
set { _attributes = value; }
}
}
The Attributes collection allows me to add extra fields to each entity without directly changing the entity itself. This allows me to make my application more modular.
For example say I have the following entity:
public class User : Entity<int> {
public virtual string Name { get; set; }
}
Now say I have a Forum module which needs a NumPosts property against the User. I would add the field against the Users table in the database. This field is non nullable and has a default value of 0. I then map the field using the dynamic component against the User entity.
However when I try inserting the user by saying:
session.Save(new User() { Name = "Test" });
It throws an error as it's expecting me to set a value for NumPosts and the generated SQL would be something like:
INSERT INTO Users (Name, NumPosts) VALUES ('Test', NULL)
However NumPosts does not allow nulls and hence the error. Ideally I'd like it to say the following if the Attributes collection does not contain an entry for NumPosts:
INSERT INTO Users (Name) VALUES ('Test')
An alternative is to say the following which would work fine:
session.Save(new User() { Name = "Test", Attributes = new Hashtable() { { "NumPosts", 0 } } });
The problem I have is that I don't want the modules to have a dependency on each other and I can't really say this.
For reference here's a bare bones version of session factory method which maps the NumPosts field:
return Fluently.Configure()
...
.ExposeConfiguration(c => {
// Get the persistent class
var persistentClass = c.GetClassMapping("User");
// Create the attributes component
var component = new Component(persistentClass);
// Create a simple value
var simpleValue = new SimpleValue(persistentClass.Table);
// Set the type name
simpleValue.TypeName = "Int32";
// Create a new db column specification
var column = new Column("NumPosts");
column.Value = simpleValue;
column.Length = 10;
column.IsNullable = false;
column.DefaultValue = "0";
// Add the column to the value
simpleValue.AddColumn(column);
// Ad the value to the component
component.AddProperty(new Property() { Name = column.Name, Value = simpleValue });
// Add the component property
persistentClass.AddProperty(new Property() { Name = "Attributes", Value = component });
})
.BuildConfiguration();
I'd appreciate if someone could let me know if this is possible. Thanks
You know how to make it working as described above:
... An alternative is to say the following which would work fine:
session.Save(new User()
{
Name = "Test", Attributes = new Hashtable() { { "NumPosts", 0 } }
});
... The problem I have is that I don't want the modules to have a dependency on each other and I can't really say this...
In case, that the biggest issue is the explicit Attributes initialization ("...I don't want the modules to have a dependency...") we can use:
12.2. Event system
So, with Listener like this:
[Serializable]
public class MyPersistListener : NHibernate.Event.ISaveOrUpdateEventListener
{
public void OnSaveOrUpdate(SaveOrUpdateEvent #event)
{
var entity = #event.Entity as Entity<int>; // some interface IHaveAttributes
if (entity == null) // would be more appropriate
{
return;
}
var numPosts = entity.Attributes["NumPosts"] as int?;
if (numPosts.HasValue)
{
return;
}
entity.Attributes["NumPosts"] = 0;
}
}
Based on this doc snippet:
Configuration cfg = new Configuration();
ILoadEventListener[] stack = new ILoadEventListener[] { new MyLoadListener(), new DefaultLoadEventListener() };
cfg.EventListeners.LoadEventListeners = stack;
This should be the init in our case:
.ExposeConfiguration(c => {
var stack = new ISaveOrUpdateEventListener [] { new MyPersistListener() };
c.EventListeners.SaveEventListeners= stack;

How to set a derived property to lower case in Grails/GORM?

This is a newbie question -- thank you for your help. I wanted to set a derived property to lower case in my domain model. Did some search (http://grails.org/doc/latest/guide/GORM.html#ormdsl plus some other) and I thought the following would work (note: nameLowerCase formula: 'LOWER(NAME)') ...
class Item {
String name
String nameLowerCase
static constraints = {
name(blank: false)
nameLowerCase(blank: false, unique: true)
}
static mapping = {
nameLowerCase formula: 'LOWER(NAME)'
sort nameLowerCase: "asc"
}
}
However, when I do ...
new Item(name: 'A').save(failOnError: true)
new Item(name: 'c').save(failOnError: true)
new Item(name: 'B').save(flush: true, failOnError: true)
println Item.list().nameLowerCase
I was expecting it to print [a, b, c] (turning to lower case in addition to the sorting), but it prints [null, null, null], and I am unable to figure out why.
What am I doing wrong? Or, is there any other way I can achieve the lower case in my domain class itself for nameLowerCase irrespective of what is passed for the name (other than using the formula in mapping)? Any help will be appreciated.
I think storing the same data in the database is a bad idea.
It's better to do it this way:
class Item {
static transients = ['nameLoweCase']
String name
String getNameLowerCase(){
name.toLowerCase()
}
static constraints = {
name blank: false
}
}
And in the controller:
class SomeController{
def show(Long id){
def item = Item.get(id)
item.nameLowerCase // name in lower case
}
}
'transient' defines a list of property names that should not be persisted to the database (more about it).
Just add this
def beforeInsert() {
nameLowerCase = name.toLowerCase()
}
def beforeUpdate() {
nameLowerCase = name.toLowerCase()
}
and remove this
nameLowerCase formula: 'LOWER(NAME)'
and Enjoy..

Data member default values, how to figure out whether something was really sent?

By default, WCF deserializes missing elements into default values like null, 0 or false. The problem with this approach is that if it's a basic type like number 0 I'm not sure whether it means the real value sent by an external system or a default value generated by WCF.
So my question is: Is it possible to find out at run-time whether the default value means "I didn't send anything".
This is crucial because we can't update and overwrite existing data in the database with the default values just because the external system didn't send a particular element this time (data corruption).
Microsoft's short answer is "It is up to the receiving endpoint to appropriately interpret a missing element."
Data member default values
http://msdn.microsoft.com/en-us/library/aa347792.aspx
Can somebody please clarify what's that supposed to mean?
Thanks
If you define your data members as properties, you can use whether the setter was called or not to decide whether some value was sent. The code below shows one data contract which knows whether it deserialized its fields.
public class Post_51ca1ead_2f0a_4912_a451_374daab0101b
{
[DataContract(Name = "Person", Namespace = "")]
public class Person
{
string name;
int age;
bool nameWasSent;
bool ageWasSent;
[DataMember]
public string Name
{
get
{
return this.name;
}
set
{
this.nameWasSent = true;
this.name = value;
}
}
[DataMember]
public int Age
{
get
{
return this.age;
}
set
{
this.ageWasSent = true;
this.age = value;
}
}
[OnDeserializing]
void OnDeserializing(StreamingContext ctx)
{
this.ageWasSent = false;
this.nameWasSent = false;
}
public override string ToString()
{
return string.Format("Person[Name={0},Age={1}]",
nameWasSent ? name : "UNSPECIFIED",
ageWasSent ? age.ToString() : "UNSPECIFIED");
}
}
public static void Test()
{
MemoryStream ms = new MemoryStream();
DataContractSerializer dcs = new DataContractSerializer(typeof(Person));
dcs.WriteObject(ms, new Person { Name = "John", Age = 30 });
Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
string noAge = "<Person><Name>John</Name></Person>";
ms = new MemoryStream(Encoding.UTF8.GetBytes(noAge));
object p = dcs.ReadObject(ms);
Console.WriteLine("No age: {0}", p);
string noName = "<Person><Age>45</Age></Person>";
ms = new MemoryStream(Encoding.UTF8.GetBytes(noName));
p = dcs.ReadObject(ms);
Console.WriteLine("No name: {0}", p);
}
}

How to change the behavior of string objects in web service calls via Windows Communication Foundation?

I have third party api's which require string values to be submitted as empty strings.
On an asp.net page I can use this code (abbreviated here) and it works fine:
public class Customer
{
private string addr1 = "";
public string Addr1
{
get {return addr1;}
set {addr1 = value;}
}
private string addr2 = "";
public string Addr2
{
get {return addr2;}
set {addr2 = value;}
}
private string city = "";
public string City
{
get {return city;}
set {city = value;}
}
}
Customer cust = new Customer();
cust.Addr1 = "1 Main St.";
cust.City = "Hartford";
int custno = CustomerController.InsertCustomer(cust);
The Addr2 field, which was not initialized is still an empty string when inserted.
However, using the same code but called it through a web service based on Windows Communication Foundation the Addr2 field is null. Is there a way (or setting) where all string fields, even if uninitialized, would return an empty string (unless, of course, a value was set).
I could not find how to serialize the nulls to empty strings so I changed the Customer class as follows:
public class Customer
{
private string addr1 = "";
public string Addr1
{
get {return addr1;}
set {addr1 = value ?? string.Empty; }
}
private string addr2 = "";
public string Addr2
{
get {return addr2;}
set {addr2 = value ?? string.Empty; }
}
private string city = "";
public string City
{
get {return city;}
set {city = value ?? string.Empty; }
}
This guarantees that all string properties are set to string.Empty and it solves the problem. (If anyone knows of a different solution I would love to hear it.)
Do you have a reference to your dll with the customer class on both the client and the server side?
What I think is happening is that at, when you created the client you have done an add service reference. This has generated a client side class.
You then do a new object on the client side which does not include lines of the type
private string addr1 = "";
It then gets set to null on the client side, and deserialized to null on the server side.
If you have control over both the client and the server side you could reference the dll with the object references from both.