Shortcut to generate field from property in Visual studio - visual-studio-2012

Is there any shortcut in VS to define a field from corresponding property?

if you mean to generate a backing store for an auto generated property:
public string MyProperty { get; set; }
To:
public string _myProperty;
public string MyProperty {
get { return _myProperty; }
set { _myProperty = value; }
}
Then there is no shortcut that does that in Visual Studio. Refactoring tools like Resharper and CodeRush offer this feature.
In DevExpress CodeRush Refactor it's under ctrl+`, Convert to Property with Backing Store.
In Resharper it's under alt+enter, To Property with Backing Field.
In Visual Studio, there is the "Encapsulate field" refactoring that works the other way around ctrl+r,e.
public string _myProperty;
To:
public string _myProperty;
public string MyProperty {
get { return _myProperty; }
set { _myProperty = value; }
}

Related

Automapper ProjectTo does not map multi lingual entity

The ABP multi lingual mapping was not being called by AutoMapper ProjectTo. If it work, it suppose to map the translation entity to DTO's name property. It works with my old code without using ProjectTo. Please refer to the sample code below:
NOTE : I am using ABP version 4.8.1
NOTE : I have included the Translation DTO in ProductDTO to force auto mapper's projectTo to eagerly load Translation data when generating the IQueryable.
ProductAppService.cs
public async Task<ICollection<ProductListDto>> GetAll()
{
return await repository.GetAll().ProjectTo<ProductListDto>().ToListAsync();
}
Product.cs
public class Product : Entity, IMultiLingualEntity<ProductTranslation>
{
public ICollection<ProductTranslation> Translations { get; set; }
}
ProductTranslation.cs
public class ProductTranslation : Entity, IEntityTranslation<Product>
{
public string Name { get; set; }
}
ProductDto.cs
public class ProductListDto
{
// Mapped from ProductTranslation.Name
public string Name { get; set; }
// Purposely include the translations dto here to force automapper to eagerly load the translation
// data from DB
[JsonIgnore]
public ICollection<ProductTranslationDto> Translations { get; set; }
}
Module.cs
public override void PreInitialize()
{
Configuration.Modules.AbpAutoMapper().Configurators.Add(cfg =>
{
MultiLingualMapContext context = new MultiLingualMapContext(IocManager.Resolve<ISettingManager>());
cfg.DisableConstructorMapping();
cfg.AddCollectionMappers();
CustomDtoMapper.CreateMappings(cfg);
});
}
CustomDtoMapper.cs
cfg.CreateMultiLingualMap<Product, ProductTranslation, ProductListDto>(context);
could you check those links, it appears to be an old issue based on github
https://github.com/aspnetboilerplate/aspnetboilerplate/issues/3356
https://github.com/aspnetboilerplate/aspnetboilerplate/blob/e0ded5d8702f389aa1f5947d3446f16aec845287/test/Abp.ZeroCore.Tests/Zero/MultiLingual/MultiLingual_Entity_Tests.cs
https://github.com/aspnetboilerplate/aspnetboilerplate/blob/e0ded5d870/test/Abp.ZeroCore.SampleApp/Application/Shop/ProductAppService.cs#L48
And Here is an StackOverFlow Question about it.
Data localization in mapping in ASP.NET Zero
Please let me know if one of them works for you.

NotMapped attribute keeps related property from saving?

I have a few properties that I don't have a direct mapping in the database for, so I'm using the convention of having another variable that is mapped to the database, and a public variable that will be used to do all of my actual work. The common one is [mapping a boolean property to a char column][1], but I also have a StatusID property whose C# enum is different based on the derived type.
My public property has the [NotMapped] attribute on it, and my internal property has the [Column] attribute. I think there's something that because the public property isn't mapped, it's keeping the other property from being mapped as well.
In my project, I start with an abstract base Message class:
[Table("tblMessage")]
public abstract class Message {
[Column("msgIsSample")]
[Required]
internal string dbIsSample { get; set; }
[Column("msgStatusID")]
internal int? dbStatusId { get; set; }
[NotMapped]
public bool IsSample {
get {
return dbIsSample.ToUpper() == "Y";
}
set {
dbIsSample = value ? "Y" : "N";
}
}
public Message() {
this.IsSample = false;
this.dbStatusId = null;
}
}
Right now I only have a single class implementing the base class, Request:
public class Request : Message {
[NotMapped]
public int Status {
get {
return this.dbStatusId.HasValue ? this.dbStatusId.Value : 1;
}
set {
this.dbStatusId = value;
}
}
public Request()
: base() {
this.Status = 1;
}
}
Here is my context:
public class MyContext : DbContext {
public DbSet<Message> Messages { get; set; }
static MyContext() {
Database.SetInitializer<MyContext>(null);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<Message>()
.Map<Request>(m => m.Requires("msgTypeID").HasValue(1));
}
}
Is this something that anyone else has run across? I haven't been able to find anything about why this isn't working, even though this looks like the accepted convention until the EF team adds additional custom mapping. Someone else has to have run across this issue.
When I try to execute this code, I get a DbUpdateException saying that it can't insert a NULL into column "msgIsSample" due to my having set that in the table creation script. This doesn't make any sense because the msgIsSample is defaulted to have a "N".
Instead of making it internal, make it protected internal.
At runtime, EF will subclass your entity dynamically. These extended classes are called dynamic proxies.
EF cannot set your property because it does not have access. To give EF access to your property, it must have either public or protected access. You can still have internal properties, but give subclasses access by adding the protected modifier.
[Table("tblMessage")]
public abstract class Message {
[Column("msgIsSample")]
[Required]
public string dbIsSample { get; protected internal set; }
[Column("msgStatusID")]
public int? dbStatusId { get; protected internal set; }

Orchard CMS Exposing Custom Part data to Query filters

I have a custom part "Feature" here's the model
public class FeatureRecord : ContentPartRecord
{
public virtual bool IsFeatured { get; set; }
public virtual string Text { get; set; }
}
public class FeaturePart : ContentPart<FeatureRecord>
{
[Display(Name = "Is featured")]
public bool IsFeatured
{
get { return Record.IsFeatured; }
set { Record.IsFeatured = value; }
}
[StringLength(400)]
[Display(Name = "Feature Text")]
public string FeatureText
{
get { return Record.Text; }
set { Record.Text = value; }
}
}
I want to set up a query for a projection widget, with a filter biased on the IsFeatured value being true
However only Filed values appear on the list of options for the filter. How can I expose custom part data to the filter option values ?
You can add new properties to use with projections through the Bindings tab under projections.

Generic lists in web references

I have a generic response object. This object accepts a type (this will be either an object or a generic list) and contains a generic object and some error information:
[DataContract]
public class Response<T>
{
[DataMember]
public T ReturnObject { get; set; }
[DataMember]
public string ErrorMessage { get; set; }
[DataMember]
public string StackTrace { get; set; }
public void SetErrorInformation(string message, string stackTrace)
{
ErrorMessage = message;
StackTrace = stackTrace;
}
}
I an attempting to pass this object with the needed information through a WCF service. This works fine until I pass a list as the type. The web reference turns the list into an array so when I attempt to use it as a list it cannot convert types. Is there a way to prevent this from happening? Can I get around this?
When you add the reference to your service in visual studio, click on the advanced button on the add service dialog. Here you can change collection type from array to list.

Edit string collection in VS2010 collection editor

As all my my doubts are used to vanish here... :) I have got an other question.
I have a custom control in which I have a list of strings List and I'd like the user of my control to be able to edit the list in the properties editor but I doesn't work.. I can click on the button to make the Collection editor visible but the add key is not enabled and there's a message 'Property editing is not available'.
I made a custom quick and dirty class
public class DataUrl
{
public string Url {get; set;}
public DataUrl() { }
public override string ToString()
{
return Url.ToString();
}
}
and with this it works but its...
I suspect it doesn't work because string (or String) does not have a parameter-less constructor. I also tried to use the attribute
[NewItemTypesAttribute(typeof(string))]
but worthless..
Could someone help me ?
public class DataUrl : Component
{
private readonly List<string> _urlList = new List<string>();
public DataUrl() : base() {}
public DataUrl(IContainer container) : base()
{
container.Add(this);
InitializeComponent();
}
[Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(System.Drawing.Design.UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<string> UrlList { get { return _urlList; } }
public override string ToString()
{
return Url.ToString();
}
}

Resources