Using some tutorials on the web, I created a metadata class for my needs using the MEF in C#
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class)]
public class ActionMetadataAttribute : Attribute
{
public bool HasSettingsDialog { get; set; }
public bool UseThreadedProcessing { get; set; }
}
public interface IActionMetadata
{
[DefaultValue(false)]
bool HasSettingsDialog { get; }
[DefaultValue(false)]
bool UseThreadedProcessing { get; }
}
I've got different kind of plugin types, so there is e. g. IHandlerMetadata and HandlerMetadataAttribute. Now I load it via the Lazy helper to allow strictly typed metadata.
[ImportMany(typeof(IActionPlugin))]
private IEnumerable<Lazy<IActionPlugin, IActionMetadata>> _plugins = null;
public ActionManager()
{
var catalog = new DirectoryCatalog(".");
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
foreach (var contract in _plugins)
{
Debug.WriteLine(contract.Metadata.HasSettingsDialog);
}
}
Works perfectly. Now, I also like to have some information about the plugins. So I've created an PluginInformationAttribute.
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class)]
public class PluginInformationAttribute : Attribute
{
public string Name { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string Url { get; set; }
public string Contact { get; set; }
public string Description { get; set; }
public Version Version { get; set; }
}
The question is now: how can I access this attribute for example in the for loop in the above code snippet? Is there any way or is my design wrong? I don't want to include the PluginInformation stuff to IActionMetadata because I'd like to use it on different types of plugins, e. g. on the IHandlerMetadata.
If you specify the type of your exporting class as a property of ActionMetadataAttribute, then you can access every attribute of the class by reflection with GetCustomAttributes method
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class)]
public class ActionMetadataAttribute : Attribute
{
public bool HasSettingsDialog { get; set; }
public bool UseThreadedProcessing { get; set; }
public Type ClassType { get; set; }
}
var attributes = contract.Metadata.ClassType.GetCustomAttributes(typeof(PluginInformationAttribute), true)
Related
I am just getting started with Entity Framework Core within C#, and I'm trying to set up a class structure where one class has a field that is another class. I have found that, when the classes do not have constructors, the code runs fine. However, when I introduce a constructor as follows:
public class InterestEF
{
public InterestEF(string id, int numTimesInterestSelected, AdminEditEF lastEdit, ICollection<AdminEditEF> allEdits)
{
this.id = id;
this.numTimesInterestSelected = numTimesInterestSelected;
this.lastEdit = lastEdit;
this.allEdits = allEdits;
}
[Key]
public string id { get; set; }
public int numTimesInterestSelected { get; set; }
public AdminEditEF lastEdit { get; set; }
public virtual ICollection<AdminEditEF> allEdits { get; set; }
}
public class AdminEditEF
{
public AdminEditEF(string id, string adminIdEditedBy, DateTime dateEdited, string changesMade, string reasonsForChanges, string idOfEditedEntity, EntityTypeEdited entityTypeEdited)
{
this.id = id;
AdminIdEditedBy = adminIdEditedBy;
this.dateEdited = dateEdited;
this.changesMade = changesMade;
this.reasonsForChanges = reasonsForChanges;
this.idOfEditedEntity = idOfEditedEntity;
this.entityTypeEdited = entityTypeEdited;
}
[Key]
public string id { get; set; }
public string AdminIdEditedBy { get; set; }
public DateTime dateEdited { get; set; }
public string changesMade { get; set; }
public string reasonsForChanges { get; set; }
public string idOfEditedEntity { get; set; }
public EntityTypeEdited entityTypeEdited { get; set; }
}
public class MySQLEFContext : DbContext
{
public DbSet<AdminEditEF> AdminEdits { get; set; }
public DbSet<InterestEF> interests { get; set; }
public MySQLEFContext(DbContextOptions<MySQLEFContext> options): base(options) { }
}
I get the following error:
System.InvalidOperationException: 'No suitable constructor found for entity type 'InterestEF'. The following constructors had parameters that could not be bound to properties of the entity type: cannot bind 'lastEdit' in 'InterestEF(string id, int numTimesInterestSelected, AdminEdit lastEdit)'.'
Basically, I'm just wondering if it's possible to have a class that has classes as fields that also has a set of constructors with parameters I can call elsewhere within code?
Any help would be greatly appreciated.
Thank you so much for reading!
From the docs:
EF Core cannot set navigation properties (such as Blog or Posts above) using a constructor.
So you would need a constructor that looks like this:
public InterestEF(string id, int numTimesInterestSelected)
{
this.id = id;
this.numTimesInterestSelected = numTimesInterestSelected;
}
Or a parameterless one:
public InterestEF()
{
}
I have a custom Metadata for MEF which is declared as below
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class ,AllowMultiple = false)]
public class ModuleAttribute : ExportAttribute ,IModuleMetadata
{
public ModuleAttribute(string contract)
: base(typeof(IScreen))
{
Name = contract;
Region = Regions.Sidebar;
IsVisible = true;
}
public string Name { get; set; }
public string Module { get; set; }
public Regions Region { get; set; }
public string DisplayName { get; set; }
public bool IsVisible { get; set; }
public string Description { get; set; }
public short Order { get; set; }
public short Permissions { get; set; }
}
As of now, I import using ImportingConstructor
private readonly IEnumerable<Lazy<IScreen, IModuleMetadata>> _modules;
[ImportingConstructor]
public SidebarViewModel([ImportMany] IEnumerable<Lazy<IScreen, IModuleMetadata>> modules)
{
_modules= modules;
}
This gives me both the Object and Metadata using Lazy<>.
The problem here is, all class that I export using MEF are NonShared, and I want to create a new object every time. But LazyObject.Value give me the same instance everytime.
Is there a way in MEF, where I can only Import all the Metadata and then using ExportProvider, I can get the new ViewModal object.
We are currently developping an application using XmlSerializer from the .NET framework.
Here is the structure of our classes:
[XmlInclude(typeof(TimeLineMediaClass))]
[XmlInclude(typeof(ImageMediaClass))]
public abstract class MediaClass
{
public string filename { get; set; }
public string maintitle { get; set; }
public string subtitle { get; set; }
public Type typeOfMedia { get; set; }
}
[XmlInclude(typeof(AudioMediaClass))]
[XmlInclude(typeof(VideoMediaClass))]
public abstract class TimeLineMediaClass : MediaClass
{
public string title { get; set; }
public TimeSpan length { get; set; }
public string genre { get; set; }
}
public class AudioMediaClass : TimeLineMediaClass
{
public string artist { get; set; }
}
public class VideoMediaClass : TimeLineMediaClass
{
public string director { get; set; }
public string studios { get; set; }
}
public class ImageMediaClass : MediaClass
{
public string width { get; set; }
public string height { get; set; }
}
Several medias of different types are added to a List, and this is what we want to serialize.
This is how the serializer is instanciated:
XmlSerializer serializer = new XmlSerializer(typeof(List<MediaClass>));
But when we launch the program and try to serialize, an exception is thrown, stating that "AudioMediaClass was not expected".
EDIT: A few things were missing in the code I have provided. I have added some corrections in it ; more details in comments.
You need to decorate your MediaClass class with
[XmlInclude(typeof(TimeLineMediaClass))]
In your example above, you have the casing wrong on TimelineMediaClass meaning the sample won't compile for me. If you remove it, or if you do have a different class with this name, you'll get the error you describe.
Once you correct the casing, it should work - it does for me [noting that I also had to remove the attribute for ImageMediaClass which also doesn't exist in your sample].
I copy your code, and run removing [XmlInclude(typeof(ImageMediaClass))] and correcting this attribute: [XmlInclude(typeof(TimelineMediaClass))] to [XmlInclude(typeof(TimeLineMediaClass))]. Now, running your code, it works fine.
I have the following code files:
public interface IMod
{
string Name { get; set; }
string Description { get; set; }
bool Enabled { get; set; }
List<IClassFile> ClassFiles { get; set; }
}
public interface IClassFile
{
string Path { get; set; }
string FileName { get; set; }
bool Enabled { get; set; }
}
public class ClassFile : IClassFile
{
public string Path { get; set; }
public string FileName { get { return System.IO.Path.GetFileName(Path); } }
public bool Enabled { get; set; }
....
}
public class ZippedMod : IMod
{
public string Name { get; set; }
public string Description { get; set; }
public bool Enabled { get; set; }
public List<IClassFile> ClassFiles { get; set; }
....
}
public class ConfigurationBlock
{
public List<IMod> Mods { get; set; }
....
}
Throughout the course of my program, I add a few ZippedMods to the ConfigurationBlock, but now I want to serialize them. I tried doing:
using (var stream = new StreamWriter("config.xml"))
{
var ser = new XmlSerializer(typeof(ConfigurationBlock));
ser.Serialize(stream, configBlock);
}
But I get this error:
There was an error reflecting type 'MinecraftModManager.ConfigurationBlock'.
|-Inner Exception:
Cannot serialize member 'MinecraftModManager.ConfigurationBlock.Mods' of type 'System.Collections.Generic.List`1[[MinecraftModManager.IMod, MinecraftModManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]', see inner exception for more details.
|-Inner Exception:
Cannot serialize member MinecraftModManager.ConfigurationBlock.Mods of type MinecraftModManager.IMod because it is an interface.
Help?
You can't serialize an interface due to the abstract nature of them. Lots of concrete types can implement the same interface, so it creates an ambiguity. You must use a concrete type.
You can't serialize an interface, only a concrete class with the default XmlSerializer. You may be able to implement IXmlSerializable to override this behavior.
I'm encountering an issue using Silverlight4, Ria Services and Entity Framework.
From my sl client I try to get some data through ria services, in my domainService class this method gets called:
public IQueryable<LastMinuteWachtLijstPromotie> GetLastMinuteWachtLijstPromoties(){
IQueryable<LastMinuteWachtLijstPromotie> list = (IQueryable<LastMinuteWachtLijstPromotie>)this.ObjectContext.LastMinuteWachtLijstPromoties.Include("Promotie");
return (from LastMinuteWachtLijstPromotie lwmp in list where lwmp.Actief select lwmp);
}
when I check the contents of the list, in debug mode, it's filled with objects of type LastMinuteWachtLijstPromotie.
these objects have a navigation property to an Object named Promotie.
And i can access the properties of these Promotie objects.
On the silveright client however a method gets invoked when loading is complete:
public void OnLoadEntitiesCompleted(ServiceLoadResult<T> result) {
}
In this method I get all the requested LastMinuteWachtLijstPromotie objects as expected, the property
Promotie however is null.
I have set the [Include] tag on the property Promotie in the auto generated metadata class
and I use the .Include("Promotie")
These same methods are used for different objects from my Domain Model, this works perfectly.
Also, I cannot seem to find differences in the .edmx file with the database mappings and navigation properties.
Has anyone encountered the same issue or know a solution for it?
the metadata classes:
[MetadataTypeAttribute(typeof(LastMinuteWachtLijstPromotie.LastMinuteWachtLijstPromotieMetadata))]
public partial class LastMinuteWachtLijstPromotie
{
// This class allows you to attach custom attributes to properties
// of the LastMinuteWachtLijstPromotie class.
//
// For example, the following marks the Xyz property as a
// required property and specifies the format for valid values:
// [Required]
// [RegularExpression("[A-Z][A-Za-z0-9]*")]
// [StringLength(32)]
// public string Xyz { get; set; }
internal sealed class LastMinuteWachtLijstPromotieMetadata
{
// Metadata classes are not meant to be instantiated.
private LastMinuteWachtLijstPromotieMetadata()
{
}
public int AlertId { get; set; }
public string ArtikelNummer { get; set; }
public Nullable<int> ArtikelVariant { get; set; }
public int LastMinuteWachtLijstPromotieId { get; set; }
[Include]
public Promotie Promotie { get; set; }
public int PromotieArtikelId { get; set; }
public int PromotieId { get; set; }
public bool Actief { get; set; }
public DateTime Aanmaakdatum { get; set; }
}
}
[MetadataTypeAttribute(typeof(Promotie.PromotieMetadata))]
public partial class Promotie
{
// This class allows you to attach custom attributes to properties
// of the Promotie class.
//
// For example, the following marks the Xyz property as a
// required property and specifies the format for valid values:
// [Required]
// [RegularExpression("[A-Z][A-Za-z0-9]*")]
// [StringLength(32)]
// public string Xyz { get; set; }
internal sealed class PromotieMetadata
{
// Metadata classes are not meant to be instantiated.
private PromotieMetadata()
{
}
public string ActieType { get; set; }
public string AssortimentsManagerNaam { get; set; }
public string AssortimentsManagerTeamIds { get; set; }
[Display(Name = "Commerciele tekst")]
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Nokavision.ReclameFolder.UI.Web.Resources.ValidationResources))]
public string CommercieleTekst { get; set; }
[Display(Name = " ")]
public string CommercieleTekstDetails { get; set; }
[Include]
public Frame Frame { get; set; }
public Nullable<int> FrameId { get; set; }
public Nullable<DateTime> LastMinuteWijzigingsDatum { get; set; }
public string Opmerkingen { get; set; }
[Display(Name = "Op wachtlijst")]
public Nullable<bool> OpWachtLijst { get; set; }
//public Nullable<int> PromotieCopyId { get; set; }
public int PromotieId { get; set; }
[Include]
public EntityCollection<PromotieLeverancier> PromotieLeveranciers { get; set; }
[Include]
public EntityCollection<PromotieMutatie> PromotieMutaties{ get; set; }
//public Nullable<int> PromotieOrigineleId { get; set; }
[Include]
public EntityCollection<PromotieSymbool> PromotieSymbolen { get; set; }
public string Status { get; set; }
[Display(Name = "Promotie inhoud")]
public string PromotieInhoud { get; set; }
[Display(Name = "Promotie eenheid")]
public string PromotieEenheid { get; set; }
[Display(Name = "Promotie prijs")]
public decimal PromotiePrijs { get; set; }
}
}
Add the Composition attribute to the property Promotie property of the LastMinuteWachtLijstPromotieMetadata class. Then it should work.
public partial class LastMinuteWachtLijstPromotie {
internal sealed class LastMinuteWachtLijstPromotieMetadata{
[Include]
[Composition]
public Promotie Promotie { get; set; }
}
}
I know this is an older thread and it may well have been answered elsewhere but I just stumbled upon it and since nobody has provided a link or a better answer.
I'm currently using Silverlight 5 and this is what worked for me (I think the process is the same in SL4 IIRC).
When propegating navigation properties to the client you need to tell RIA services that there is a relationship somewhere using the [Key] and [Association] attributes, this, not unlike the entity framework just describes how to map the relationship to the proper object.
First the metadata classes:
[MetadataTypeAttribute(typeof(Category.CategoryMetadata))]
public partial class Category
{
internal sealed class CategoryMetadata
{
private CategoryMetadata() {
}
[Key]
public int Id { get; set; }
public string NAME { get; set; }
[Association("CategoryToProducts", "Id", "CAT")]
[Include]
public EntityCollection<Product> Products { get; set; }
}
}
[MetadataTypeAttribute(typeof(Order.OrderMetadata))]
public partial class Order
{
internal sealed class OrderMetadata
{
// Metadata classes are not meant to be instantiated.
private OrderMetadata() {
}
[Key]
public int Id { get; set; }
public int PRODID { get; set; }
public DateTime DATE { get; set; }
public bool DONE { get; set; }
public int QTY { get; set; }
[Association("OrderToProduct", "PRODID", "Id", IsForeignKey = true)]
[Include]
public Product Product { get; set; }
}
}
[MetadataTypeAttribute(typeof(Product.ProductMetadata))]
public partial class Product
{
internal sealed class ProductMetadata
{
private ProductMetadata() {
}
[Key]
public int Id { get; set; }
public int CAT { get; set; }
public string NAME { get; set; }
public string DESC { get; set; }
public decimal PRICE { get; set; }
public int QTY { get; set; }
public long UPC { get; set; }
[Association("ProdToCat", "CAT", "Id", IsForeignKey = true)]
[Include]
public Category Category { get; set; }
[Association("ProductToOrders", "Id", "PRODID")]
[Include]
public EntityCollection<Order> Orders { get; set; }
}
}
Now we need to tell RIA services we want it to load the association:
(Note: Intellisense says it's a dot separated list of property names to include, however I tried something like .Include("Category.SubCategory") and this failed with an exception... though .Include("Category").Include("SubCategory") worked like a charm!)
public IQueryable<Product> GetProducts() {
return this.ObjectContext.Products.Include("Category");
}
I can now access my "Category" property from the Silverlight client and it is not NULL :)
Same as SilverX: just had the issue, solved it and thought it could be useful to someone.
I too had all the configuration stuff correct ([Include] for RIA S, Include() for EF) but a navigation property was still null on the Silverlight side.
Turns out the domain service method was using the [Invoke] attribute (and returning a IEnumerable<T>). Removing this attribute solved the issue.
(just for the record, [Invoke] was being used because the method had a List<Entity> parameter)