Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MetadataTypeAttribute #40

Closed
daniel-white opened this issue Apr 8, 2020 · 22 comments · Fixed by #142
Closed

Add support for MetadataTypeAttribute #40

daniel-white opened this issue Apr 8, 2020 · 22 comments · Fixed by #142
Labels
enhancement New feature or request

Comments

@daniel-white
Copy link

daniel-white commented Apr 8, 2020

System.ComponentModel.DataAnnotations.MetadataTypeAttribute is a handy bridge to attributes defined in other files, especially in code generated files. It works with the validation frameworks. I don't see why it wouldn't here.

Example:

[GeneratedCode]
[MetadataTypeAttribute(typeof(DtoMetadata))]
public partial class Dto
{
    public string Private { get; set; }
}

internal class DtoMetadata
{
     [NotLogged]
     public object Private { get; set; }
}

I think it would be great if both sources could be utilized, but the configuration on the primary class would win in the event of a conflict.

@sungam3r
Copy link
Member

sungam3r commented Apr 8, 2020

What problem do you solve?

@daniel-white
Copy link
Author

What problem do you solve?

It solves one case where the properties are defined in a code generated class file. One wouldn’t want to put the attributes in there as they’d get overwritten by the generator

@sungam3r
Copy link
Member

sungam3r commented Apr 8, 2020

ok

@daniel-white
Copy link
Author

@nblumhardt is this something you would take a PR for? I'd be happy to start looking how to add this.

I've found this as a potential option: https://stackoverflow.com/questions/17873576/metadata-were-not-loaded-using-metadatatype

@nblumhardt
Copy link
Contributor

Hi @daniel-white - I think so 👍

@daniel-white
Copy link
Author

doh. the attribute isn't available in .net standard, so i could add one to the library and reflect on that or just wait till everything is .net 5.

@nblumhardt
Copy link
Contributor

Targeting netcoreapp3.0 or 3.1 would also be fine 👍

@digitalsigi
Copy link
Contributor

I am scaffolding repeatedly a database structure and separated all attributes in a separate file. So I have exactly the same problem.

@nblumhardt May be we can somehow work together to get it done. I am not experienced in multi-targeting, so I would need some help here.

According to Micosoft it sounds to go straight to net5.0.

@digitalsigi
Copy link
Contributor

So was digging a litte deeper into this.

@daniel-white is right, metadatatype comes from Net Framework and is available in net core 3.x but not in net standard.
So I will proceed with net5.0

@SimonCropp
Copy link
Contributor

@digitalsigi are you still interested in submitting a PR for this?

@digitalsigi
Copy link
Contributor

Unfortunately not for the moment. I am still busy with a larger project. Had a quick look into the code, it looks feasable, but its more than just hack an go. If it helps, I could provide some code snippets showing how to dig into attributes hierarchies.

Copy link

This issue was marked as stale since it has not been active for a long time

@github-actions github-actions bot added the stale label Jan 22, 2024
@digitalsigi
Copy link
Contributor

OK, got a ping from github. Here a code snippet I am using to get all properties which are then filtered to select only those with my Custom Attribute:

           var propBaseType = vmType.GetProperties().ToList();
           var metaType = vmType.GetCustomAttributes<MetadataTypeAttribute>(true).ToList().FirstOrDefault();
           var metaProp = new List<PropertyInfo>();
           if (metaType != null)
           {
               var metaClass = metaType.MetadataClassType;
               metaProp = metaClass.GetProperties().ToList();
           }
 
           var propConcat = metaProp.Concat(propBaseType) // Concatenate properties of MetaData class and Base class
                                                          // Take only first -> the one of Metadata, assumed metadata has the avs attribute
                                                          // if base class bears an avs attibute it is silently ignored
                                   .OrderBy(m => m.Name)
                                   ;

I guess something like this has to be added in GetPropertiesRecursive, but I still can't find the time to do it.

@sungam3r sungam3r added enhancement New feature or request and removed stale labels Jan 23, 2024
@sungam3r
Copy link
Member

It definetely can be added. The main thing here is to match attributes from other type to the properties of original type.

@sungam3r sungam3r added the help wanted Extra attention is needed label Jan 28, 2024
@sungam3r
Copy link
Member

If someone wants to work on this issue, I will be grateful.

@sungam3r
Copy link
Member

sungam3r commented Jan 28, 2024

I don't think I will work on this feature. Anyone can start up from #91.

@sungam3r
Copy link
Member

ping @daniel-white @digitalsigi

Copy link

This issue was marked as stale since it has not been active for a long time

@github-actions github-actions bot added the stale label Feb 28, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 28, 2024
@sungam3r sungam3r removed the stale label Dec 12, 2024
@sungam3r sungam3r reopened this Dec 12, 2024
@digitalsigi
Copy link
Contributor

Ok folks,

I just synced my fork and I'll have at look at it.

@sungam3r It looks like you are working on something to provide a solution. Whats your status?

@sungam3r
Copy link
Member

Nope. See #40 (comment)

@digitalsigi
Copy link
Contributor

Just saw a pending PR. I will hab a look but possibly open a new PR is this OK for you?

@sungam3r
Copy link
Member

Yes.

@sungam3r sungam3r linked a pull request Dec 18, 2024 that will close this issue
@sungam3r sungam3r removed the help wanted Extra attention is needed label Dec 22, 2024
@sungam3r sungam3r linked a pull request Dec 22, 2024 that will close this issue
@sungam3r sungam3r removed a link to a pull request Dec 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants