PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, May 18, 2022

[FIXED] What is the substitution for partial property in C#?

 May 18, 2022     asp.net, asp.net-mvc-3, c#, partial, properties     No comments   

Issue

I have an application (mvc 3) where the code is autogenerated for the datacontext class. And I need to expand the functionality, so I created partial class with the same name. But I found that only "void methods" could be marked as partial, while I need kind of a partial property.

So, is there any way to expand property's functionality in C#?

Updated:

Here is the code:

    public Table<Post> Posts
    {
        get
        {
            // writing info into Trace file
            Log = Console.Out;
            var result = this.GetTable<Post>();
            Log = new LogLinqToSql();
            SubmitChanges();
            return result;
        }
    }

The thing is that if I make any change to the data model this code will disappear, so how can I move it to the "safer" place?


Solution

The solution for my problem (provide logging) is simple: In the autogenerated code there is a bunch of partial helper methods. And there is a method "OnCreated" which is called when the instance of MyTypeDataContext class is created. So, I just need to do the following:

public partial class WebStoreDataContext
{
    partial void OnCreated()
    {
        // writing info into Trace file
        Log = Console.Out;
        Log = new LogLinqToSql();
        SubmitChanges();
    }
}

If You want to add Attributes to the properties this post provides all the information)



Answered By - Aleksei Chepovoi
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing