Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8370855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:00:54+00:00 2026-06-09T14:00:54+00:00

I have a situation where in MS Dynamics Crm it returns some objects using

  • 0

I have a situation where in MS Dynamics Crm it returns some objects using sealed classes and readonly properties (I can only presume using internal constructors or internal property sets). And these guys don’t inherit from an interface I can use. Obviously, if I had control over this code I would have a lot more control, but because it’s in the underlying MS Dynamics framework I’m a bit stuck.

The class I’d like to mock/override is called AliasedValue. The reason I want to mock it is I’m trying to simulate a call to dynamics and want to pretend I’m getting returned some Aliased Values.

So here’s the scenario, I want to create a sample entity that Dynamics will return… something like this:

var new_entity = new Entity("new_entity")
    {
        Attributes = new AttributeCollection
            {
                {"new_name", "BR"},
                {"createdon", DateTime.Now.AddDays(-1).Date},
                {"new_field", "My field"},
                {"new_contact", new AliasedValue {*****} }
            }
    };

So I am referencing a “contact” entity (in real time this comes back as an AliasedValue). While testing I want to make sure my code handles certain values that might come back in this field (e.g. it knows how to deal with an aliased value, and that if no linked contact is returned it doesn’t blow up, etc etc).

So if you’ve clicked on the link to AliasedValue you’ll notice the properties are read only… so I can’t set up test data…

Next up, I was going to create my own class and override the whole thing… but it’s sealed…

And on top of this, as you probably have guessed, Moq doesn’t like trying to mock a sealed class.

I’ve read I’ll need to purchase some “better” mocking framework to do this, but I really don’t want to have to fork out some extra money just to get around this 1 thing.

Anyone got a nice neat little solution for me to get around this?

Update for Clarification


A sample of how this is returned is as follows. I have a service which will be mocked to return the above object. Or what it actually returns is a collection containing the above object. So this is like so:

var service = new Mock<IOrganizationService>();
service
    .Setup(s => s.RetrieveMultiple(null))
    .Returns(new EntityCollection (new List<Entity> {new_entity}));

This is great, I can return the above entity and do what I want with it. So, after I have created the above entity and I return it via my mocked service I get the above in my “EntityCollection”. And once I have my entity I access the property as follows:

var aliasedContact = (AliasedValue)new_entity.Attributes["new_contact"];

Maybe I’m being silly but I can’t quite get how the answer (by Lunivore) will solve this… (adding a comment to get feedback…)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:00:55+00:00Added an answer on June 9, 2026 at 2:00 pm

    Abstract yourself from third-party libraries by adding an adapter class that’s so simple, you could test it by inspection. Rather than extending your target class, just compose your new class over the top of it. It can have the same names of methods, the same properties, etc. (though it doesn’t have to) – it just delegates to the sealed class.

    If you have one class which returns your sealed class, wrap them both. So it might look like this:

    var crmWrapper = new MSDynamicsCrmWrapper(_myMsDynamicsCrm);
    var entityWrapper = crmWrapper.AliasedValue(url);
    

    and the crmWrapper will do:

    public EntityWrapper AliasedValue(Url url) 
    {
        var entity = delegateCrm.AliasedValue(url);
        return new EntityWrapper(entity);
    }
    

    Simple. You only need to delegate those methods you’re actually using.

    Not only does this allow you to mock the class out, but it gives you control if, say, the next version of MS Dynamics changes the API slightly, or there turns out to be weird behavior or poor performance. Also, you’re narrowing down the API to the bits of it that you’re actually using, so the code may even be easier to maintain.

    There’s nothing in computing that another layer of abstraction won’t solve…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have situation where a user can manipulate a large set of data (presented
I'm writing my own CMS and have the situation where the some initial settings
Say I have some situation like this: class Vertex { public: Position position; Normal
Hey i have a situation in which i need to pass some variable value
Assume I have some C++ method which returns back a pointer to an object.
The situation is this: I have some JavaScript that creates an interactive dialogue (and
i have situation like this: class IData { virtual void get() = 0; virtual
I have situation, where running a query that filters by an indexed column in
I have situation in which I read a record from a database. And if
I have situation where I need to change the order of the columns/adding new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.