In my database I want to save the user GUI settings for one of the "views", as a semicolon-separated string, i.e. : "1;31;5;411;".
I use NNibernate for this project, and this is the mapping and the code for this specific private member;
C# entity code for User.Settings
private string _linkConsole = "0";
[DataMember]
public LinkConsole LinkConsole
{
get { return new LinkConsole(_linkConsole); }
set { _linkConsole = value.GetSqlString(); }
}
XML mapping:
<property name="LinkConsole" type="System.String" access="field.camelcase-underscore" not-null="true"/>
LinkConsole has a lot of public methods for GUI-specific settings, which is toggling the values of certain private members.
When its time to save this settings to the database, .GetSqlString(); formats the string and writes it to the private member _linkConsole , which is the private member exposed to NHibernate & the database.
Problem:
When I try to do something with the GUI, i.e.:
User.Settings.LinkConsole.SetRightPaneContent(50);
This is never stored on the object LinkConsole, and I can't seem to figure out why?
When I set a breakpoint, I get a message in Visual Studio stating:
Your step-into request resulted in an automatic step-over of a property or operator
Maybe this is a result of the problem, that I'm designing this in a wrong way?
If so, is it possible to achieve this in combination with a private string member?
Any help would be greatly appreciated! Thanks! :-)
Have you considered implementing a custom IUserType for LinkConsole for use as the NHibernate type?
The use case you're describing is exactly what IUserType is for.
This isn't really an NHibernate issue. LinkConsole is a reference type. If you set a member of it, it isn't going to call the set method on the property you got it from, it's going to set the value on the instance that was returned from the original get. If you want this code to work, you'd need to get a new LinkConsole instance from the User, call the methods to set the settings, then set the LinkConsole property of the user to your configured instance.
The problem is probably with how you are setting the private variable that is being written to the database. Without seeing your code I can't be sure but look at the following article to be able to step into properties with visual studio:
http://msdn.microsoft.com/en-us/library/cc667388.aspx
Related
I'm developing a set of C# classes in order to be used with Powershell.
The properties of the classes are private in order not to be available to end users.
The issue I'm facing is that when trying to display the object (e.g. $myVar), the output is null since the properties are private.
Is there a way to use a function to return the properties I need?
Thanks in advance,
Chris
I'm developing an application which can deal with a MS-ADLDS-Service.
Currently it is possible to create Directory-Entries and assign values to some properties.
Not a realy exciting task until this:
Im my application it's possible (it should be) to configure which properties of a class (for instance: the CN=Person class) should be assigned with values which are evaluated at runtime in my application.
Long story short:
I want to retrieve all (writeable) properties of a class. Without creating and saving a new CN=Person-Object before.
Currently i use my schemaBinding to get the Directory-classSchema-Entry of the Person-Class (CN=Person) from where i read some property-values (like "AllowedAttributesEffective", "mayContain", "AllowedAttributes") - i get the most properties by this way - but some Properties are missing! For instance the "telephoneNumber"-Property (attributeSchema: CN=Telephone-Number)
Does anybody know how to get these properties of a class? ADSI-Edit does this: when i create a new object with adsi-edit i can assign values to all possible properties before committing the new entry.
thanks a lot for any hint!
(.net code is welcome)
I have found the solution for my task!
Some of these properties are "calculated" and not persistent at the directoryentry.
So its meant to call the RefreshCache() Method and pass the needed property names as an string array.
directoryEntry.RefreshCache(new string[] { "allowedAttributesEffective",
"allowedAttributes",
"systemMayContain",
"systemMustContain" });
After that call, the properties have values....
if (directoryEntry.Properties["systemMayContain"]).Value != null)
{
/// Success
}
I have to make many combination of field and property. I cannot use the implicit property version "{ get; set; }" since the fields need some attributes.
So, in Visual Studio Express 2013, is there a way to have a shortcut to create a property associated with a field I just ended up writing?
Let's say I write;
private MyType myData;
and I press CTRL+P (whatever the shortcut), and it adds
public MyType MyData
{
get { return myData; }
set { myData = value; }
}
just after.
Is it possible?
EDIT:
The Express version however does only have two refactoring functionalities: rename and extract method, the other functionalities like encapsulate are not present.
I guess I'm stuck.
By what you are asking 3 simple clicks will do it like this:
rigth-click in your field then Refactor-->EncapsulateField
And your done.
Check this out:
http://msdn.microsoft.com/en-us/library/z41h7fat.aspx
this might also be useful (Create your own snippets) :
http://msdn.microsoft.com/en-us/library/vstudio/ms165394.aspx
here you have all the code snippets you need including "prop", which is the one you need now!
Hope it helps
Type prop
then press tab
then enter the property name
then press ctrl+.
then click on convert to full property
You should use code snippet and assign hotkey for it.
More information about managing code snippets here: http://msdn.microsoft.com/en-us/library/ms165394.aspx
You know how you can have a property that automatically generates a backing field? Like if I go:
public String SomeProperty {get; set;}
I know that if I want to add code to that property I have to create the backing field as so:
public string someProperty = string.Empty;
public string SomeProperty
{
get { return someProperty; }
set
{
someProperty = value;
DoSomething();
}
}
Basically, what I want to know is... is there any way to do this but without having to create the backing field? For example I could use it to trigger some kind of event that occurs when a property is set. I'm looking for something like this:
public string SomeProperty
{
get;
set { this.OnSomeEvent; }
}
But I know that'll cause a compile error because get needs do declare a body if set does.
I've researched and I cant find anything, but I thought I'd check to see if anyone knew.
I guess what I'm really after is some way to trigger an event when a property is changed but without having to add all that extra clutter. Any suggestions?
Simple answer is no, you can't have it both ways. From .NET Docs:
In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors.
There are not any solutions for this built into the framework, and you cannot modify existing types via reflection (in order to add the logic at runtime). The only way to accomplish this seems to be at compile time.
There is a product http://www.postsharp.net/ that can accomplish this (intercept property/method calls), and there does appear to be a free edition.
The field keyword might be added to C#, see https://github.com/dotnet/csharplang/issues/140, which removes "when no additional logic" requirement for auto properties.
It didn't make it into C# 10 nor 11, but latest comment from compiler team says C# version 12 might have it. They release yearly, so that would be Nov 2023.
I am currently writing an financial application, and we have a pretty standard customer table. It consists of many mandatory fields, and some optional like Cell/Fax etc.. I'm using NHibernate as a ORM and have all the mappings right. It already works.
I just wonder, how do I "express" in code that a field is not-null without commenting? I have the hbm.xml files that document this, but it's kinda awkward to look at them for things like this.
The other thing that comes to mind is that I don't want the repository to throw NHibernate Exceptions at my Logic, so maybe I should go the validation route in the Controller.
Still, how can I make the POCO code express that some fields can be null?
As you can see, I want to have Cellular and Fax be optional while Phone mandatory. They are all just composite mappings, so the mapping file just specifies that the single elements of each have to be not-null, but I hate to do the Person.Cellular != null check all the time to avoid having a NullReferenceException.
There are a few ways of doing this depending on your POCO behaviour and coding style.
Firstly, you could use nullable types to express that this field is nullable and it would therefore be implicit that the rest are not nullable.
Alternatively you could introduce a Phone value type as the type for the Phone property of the POCO you illustrated, implying that because it is not a primitive type it is "more important" - this would also enable you to encapsulate phone number validation within the class itself.
In my mind, to be a true POCO object, it need not worry about the underlying nullability within the database table it is persited in... it should actually have validation and value types that express its behaviour as a stand alone entity; thus before it gets to NHibernate it is already in a valid state.
Make notnull properties readonly and write to them via a public constructor. Make the default constructor protected or private.
public class DomainObject{
private string nnp;
protected DomainObject(){}
public DomainObject(string nnp){
this.nnp = nnp;
}
public string NotNullProp {get {return nnp;}}
public string NullableProp {get;set;}
}