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

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;
}
}
}

Related

Automapper fails for objects with collections where items use ConvertUsing<>()

My example:
class BoxVM {
int BoxId {get;set;}
List<ItemVM> Items {get;set;}
}
class Box {
int BoxId {get;set;}
List<Item> Items {get;set;}
}
With mapping config:
CreateMap<BoxVM, Box>();
CreateMap<ItemVM, Item>().ConvertUsing<ItemTypeConverter>();
When converting BoxVM will Items, the ItemTypeConverter is not called. Leaving an empty Items collection in Box.
The BoxId is being mapped correctly.
Am I missing a step?
Looks like it does work.
using System.Collections.Generic;
using AutoMapper;
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<BoxVM, Box>();
cfg.CreateMap<ItemVM, Item>().ConvertUsing<ItemTypeConverter>();
});
Mapper.AssertConfigurationIsValid();
var boxVm = new BoxVM()
{
Value1 = "5",
Items = new List<ItemVM> { new ItemVM { Name = "Item1" } }
};
var result = Mapper.Map<BoxVM, Box>(boxVm);
Assert.AreEqual(1, result.Items.Count);
}
}
public class Box
{
public string Value1 { get; set; }
public List<Item> Items { get; set; }
}
public class Item
{
public string Name { get; set; }
}
public class BoxVM
{
public string Value1 { get; set; }
public List<ItemVM> Items { get; set; }
}
public class ItemVM
{
public string Name { get; set; }
}
public class ItemTypeConverter : ITypeConverter<ItemVM, Item>
{
public Item Convert(ItemVM source, Item destination, ResolutionContext context)
{
return new Item { Name = source.Name };
}
}

Orchard CMS module no values from Editor

I've created my first simple module but for some reason my editor doesn't seem to be getting any values from the form.
The code is basically a slight modification on the Maps module example. (I've trimmed usings and whatnot for brevity.) The example is here: http://docs.orchardproject.net/Documentation/Writing-a-content-part
EDIT: I narrowed it down to my bool and DateTime properties. If I only use strings and ints my module works as expected. I have looked at other migrations in my modules directory and they use the generic Column method. I tried this, but still cannot get it to work.
Model:
namespace Maps.Models
{
public class MapRecord : ContentPartRecord
{
public virtual int SenderId { get; set; }
public virtual int RecipientId { get; set; }
public virtual string Subject { get; set; }
public virtual string Body { get; set; }
public virtual DateTime Timestamp { get; set; }
public virtual bool Read { get; set; }
public virtual int ReplyTo { get; set; }
}
public class MapPart : ContentPart<MapRecord>
{
[Required]
public int SenderId
{
get { return Record.SenderId; }
set { Record.SenderId = value; }
}
[Required]
public int RecipientId
{
get { return Record.RecipientId; }
set { Record.RecipientId = value; }
}
[Required]
public string Subject
{
get { return Record.Subject; }
set { Record.Subject = value; }
}
[Required]
public string Body
{
get { return Record.Body; }
set { Record.Body = value; }
}
[Required]
public DateTime Timestamp
{
get { return Record.Timestamp; }
set { Record.Timestamp = value; }
}
[Required]
public bool Read
{
get { return Record.Read; }
set { Record.Read = value; }
}
[Required]
public int ReplyTo
{
get { return Record.ReplyTo; }
set { Record.ReplyTo = value; }
}
}
}
Migrations.cs
public class Migrations : DataMigrationImpl {
public int Create() {
// Creating table MapRecord
SchemaBuilder.CreateTable("MapRecord", table => table
.ContentPartRecord()
.Column<int>("RecipientId")
.Column<int>("SenderId")
.Column<string>("Subject")
.Column<string>("Body")
.Column<DateTime>("Timestamp")
.Column<bool>("Read")
.Column<int>("ReplyTo")
);
ContentDefinitionManager.AlterPartDefinition(
typeof(MapPart).Name, cfg => cfg.Attachable());
return 1;
}
}
If this is similar to the example there could be two possible issues. 1) Maybe you haven't added the placement file. If the migration worked and there is no placement file then you won't see the data. 2) Another possible solution if the migration is failing would be that it doesn't have the correct DataTypes for bool and DateTime. Instead try DBType.Boolean and DBType.DateTime. Let me know if that works for you.

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 can I set the RowKey to a new value with a sequence

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()) { }
}

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

Resources