How can I set the RowKey to a new value with a sequence - azure

I have the following class:
public class Note : TableServiceEntity
{
public Note( )
{
}
public Note(string pK)
{
PartitionKey = pK,
RowKey = Seq.GetSequence().ToString();
}
public string Description { get; set; }
}
What I need is to set the RowKey to the value generated by the sequence. Can anyone explain how to do this with a constructor? What I get is a syntax error: Virtual member call in constructor.

public static class Seq
{
static int index;
public static int GetSequence()
{
return index++;
}
}
public class Note : TableServiceEntity
{
public Note(string partitionKey, string rowKey)
: base(partitionKey, Seq.GetSequence().ToString()) { }
}

Related

Setting value of Row/Partition Key based on other propert

Is there a way I can override getter of RowKey property for TableEntity ?
I would assume that it is possible to use the new keyword to override the getter as such:
public class Person : TableEntity
{
public Person(string name, string surname)
:base(surname, name)
{
}
public new string PartitionKey
{
get { return "OhLala" + base.PartitionKey; }
}
public new string RowKey
{
get { return "ohMama" + base.RowKey; }
}
}
Here is the snippet to implement the ITableEntity interface
public class TestEntity : ITableEntity
{
private string _rowKey;
public TestEntity()
{
this.Properties = new Dictionary<string, EntityProperty>();
}
private IDictionary<string, EntityProperty> Properties { get; set; }
public void ReadEntity(IDictionary<string, EntityProperty> properties, Microsoft.WindowsAzure.Storage.OperationContext operationContext)
{
this.Properties = properties;
}
public IDictionary<string, EntityProperty> WriteEntity(Microsoft.WindowsAzure.Storage.OperationContext operationContext)
{
return this.Properties;
}
}

Azure Table Storage Entity Row/Primary Key as Attribute for existing properties

I already have entity migrated from EntityFramework.
I'm don't want override some propeties and Convert it to string
public class User : TableEntity, ITableStorageEntity<int, Guid>
{
[RowKey]
public Guid ID { get; set; }
[PartitionKey]
public int LanguageID { get; set; }
It's possible ? I don't want override ReadEntity/WriteEntity.
Since your class is already based on TableEntity, you might want to try to override/replace the row key and partition key property of TableEntity using the 'new' keyword instead.
public class User : TableEntity
{
[IgnoreProperty]
public Guid ID { get; set; }
[IgnoreProperty]
public int LanguageID { get; set; }
public new string PartitionKey { get { return ID.ToString(); } set { ID = Guid.Parse(value); } }
public new string RowKey { get { return LanguageID.ToString(); } set { LanguageID = int.Parse(value); } }
}
I am not a big fan of 'new' modifier. In my opinion it is non-OOP approach.
I will suggest following
public class ConsumerApplicationEntity : TableEntity
{
public ConsumerApplicationEntity(string applicationKey, string applicationSecret)
: base(applicationKey, applicationSecret)
{
}
[IgnoreProperty]
public string ApplicationKey
{
get
{
return this.PartitionKey;
}
set
{
this.PartitionKey = value;
}
}
[IgnoreProperty]
public string ApplicationSecret
{
get
{
return this.RowKey;
}
set
{
this.RowKey = value;
}
}
}

Orchard Content Type is null

i am new in orchard module development.i create a module.when i try to save data.
i use this code fore save data
public ActionResult Create(FormCollection input)
{
var product = contentManager.New<ProductPart>("Product");
product.EmployeeName = input["EmployeeName"];
product.EmployeeFathersName = input["EmployeeFathersName"];
product.DOB = Convert.ToDateTime(input["DOB"]);
product.Email = input["Email"];
product.Address = input["Address"];
product.JoiningDate = Convert.ToDateTime(input["JoiningDate"]);
if (!ModelState.IsValid)
{
return View(product);
}
contentManager.Create(product);
return RedirectToAction("Index");
}
this class i use in Model
public class ProductRecord:ContentPartRecord
{
public virtual string EmployeeName { get; set; }
public virtual string EmployeeFathersName { get; set; }
public virtual DateTime DOB { get; set; }
public virtual string Email { get; set; }
public virtual string Address { get; set; }
public virtual DateTime JoiningDate { get; set; }
}
public class ProductPart : ContentPart<ProductRecord>
{
/*
public int Id
{
get { return Record.Id; }
set{Record.Id = value;}
}
*/
[Required]
public string EmployeeName
{
get { return Record.EmployeeName; }
set { Record.EmployeeName = value; }
}
[Required]
public string EmployeeFathersName
{
get { return Record.EmployeeFathersName; }
set { Record.EmployeeFathersName = value; }
}
[Required]
public DateTime DOB
{
get { return Record.DOB; }
set { Record.DOB = value; }
}
[Required]
public string Email
{
get { return Record.Email; }
set { Record.Email = value; }
}
[Required]
public string Address
{
get { return Record.Address; }
set { Record.Address = value; }
}
[Required]
public DateTime JoiningDate
{
get { return Record.JoiningDate;}
set { Record.JoiningDate = value; }
}
}
i use content type "Product" but when it goes orchard ContentCreateExtension in belows method
public static T New<T>(this IContentManager manager, string contentType) where T : class, IContent {
var contentItem = manager.New(contentType);
if (contentItem == null)
return null;
var part = contentItem.Get<T>();
if (part == null)
throw new InvalidCastException();
return part;
}
here i face var part is null that means it content part is null.
please help me....
Have you setup your migrations class?
i.e.
public class Migrations : DataMigrationImpl {
public int Create() {
SchemaBuilder.CreateTable("ProductRecord",
table => table
.ContentPartRecord()
.COLUMNS NEED TO BE SPECIFIED
);
ContentDefinitionManager.AlterTypeDefinition("Forum",
cfg => cfg
.WithPart("ProductPart")
.WithPart("CommonPart")
);
Also have you setup your repository?
i.e.
public class ProductPartHandler : ContentHandler {
public ProductPartHandler(IRepository<ProductPartRecord> repository) {
Filters.Add(StorageFilter.For(repository));
}
In addition to the Nicholas answer, I want to mention, that missing driver for the ProductPart can cause such error. Make sure, that you have at least empty driver defined.
public class ProductPartDriver : ContentPartDriver<ProductPart> {}
Just went through a similar situation, be sure that the handler class is declared as public.

how to confige an abstract class with structure map

is there any problem with this kinda registration via structure map??
static public class ContainerBootstrapper
{
static public void BootstrapDefaultContainer(bool test = false)
{
StructureMap.ObjectFactory.Initialize(x =>
{
x.Scan(p =>
{
p.AssemblyContainingType<IPropertyType>();
p.AddAllTypesOf<IPropertyType>();
// p.AddAllTypesOf<IPropertyType>().NameBy(c => c.Name);
});
});
}
public interface IPropertyType : IIdentityObject, IPriority
{
string PropertyName { get; set; }
ObjectType ObjectType { get; }
string DisplayName { get; set; }
IEntityType EntityType { get; set; }
IList<IPropertyRuleObject> RuleObjects { get; set; }
void AddRuleObject(IPropertyRuleObject ruleObject);
}
public abstract class PropertyTypeBase : PersistentObject, IPropertyType
{
public PropertyTypeBase()
{
}
public PropertyTypeBase(string propertyName, string displayName)
{
PropertyName = propertyName;
DisplayName = displayName;
}
....
}
public class StringType : PropertyTypeBase
{
private ObjectType _objectType;
public StringType()
{
_objectType = new ObjectType(typeof(string));
}
public StringType(string propertyName, string displayName)
: base()
{
PropertyName = propertyName;
DisplayName = displayName;
}
public override ObjectType ObjectType
{
get { return _objectType; }
}
}
when ContainerBootstrapper.BootstrapDefaultContainer(); execute I see this line of error:
StructureMap Exception Code: 200
Could not find an Instance named "StringType" for PluginType Azarakhsh.Domain.Core.AdaptiveObjectModel.Interface.IPropertyType
the calling code:
public IPropertyType GetPropertyType(IIdentityObject identityObject, string name)
{
string[] Properties = name.Split('.');
object Result = identityObject;
foreach (var Property in Properties)
Result = Result.GetType().GetProperty(Property).PropertyType.Name;
IPropertyType propertyType = StructureMap.ObjectFactory.GetNamedInstance<IPropertyType> (Result + "Type");
if (propertyType==null)
throw new Exception("Property type not found");
return propertyType;
}
what is the problem?
You are trying to get a named instance, but from what I can see of the code you have provided, you dont name your instances. The line of code that name your instances is commented out.
But even if you would just use the ObjectFactory.GetInstance<IPropertyType>(); here, you would have got an error because structuremap dont know what constructor to use. There are several solutions to theis problem.
Change your design so you only have one constructor
Mark your default constructor with the [DefaultConstructor] attribute, then it will work.
You can register it with objectFactory manually with something like this:
x.For().Use().Ctor("propertyName").Is("someValue").Ctor("displayName").Is("someValue");
You can write a custom registrationconvention as described here

Multiple Description Attribute in c#

DescriptionAttribute not allow to be set multiple times.
Is there any way to have such opportunity to set kind of DescriptionAttribute many times to property or enum for example.
Solution:
public class ExtraDescriptionAttribute : DescriptionAttribute
{
private string extraInfo; public string ExtraInfo { get { return extraInfo; } set { extraInfo = value; } }
public ExtraDescriptionAttribute(string description)
{
this.DescriptionValue = description;
this.extraInfo = String.Empty;
}
}
You should be calling the base class constructor and remove the Description property. This also shows how to set the ExtraInfo property.
public class ExtraDescriptionAttribute : DescriptionAttribute
{
public String ExtraInfo { get; private set; }
public ExtraDescriptionAttribute (String description, String extraInfo) : base(description)
{
ExtraInfo = extraInfo;
}
}
The description attribute will now look like:
[ExtraDescriptionAttribute("Description", "ExtraInfo")]

Resources