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

Handling Null source objects #60

Closed
tjs-shah opened this issue Apr 15, 2016 · 4 comments
Closed

Handling Null source objects #60

tjs-shah opened this issue Apr 15, 2016 · 4 comments
Labels

Comments

@tjs-shah
Copy link

In our coding we have a situation where source object doesn't have any value. E.g. We are getting user information from database, but id pass is not valid, so user object is null.

One way if to sprinkle null check before invoking Adapt method
if (user != null)
{
_adapter.Adapt(user)
}

If there better way to handle Null object in Mapster?

I have seen that AutoMapper supports something like below:
Mapper.CreateMap<User, UserViewModel>()
.ConvertUsing();

class WsMembershipToDeMemberConverter
: AutoMapper.ITypeConverter<User, UserViewModel>
{
public UserViewModel Convert(AutoMapper.ResolutionContext context)
{
if (context == null || context.IsSourceValueNull)
{
return null;
}

    var User = context.SourceValue as User;

    var UserViewModel = new UserViewModel();

    // conversion rules and logics here

    return UserViewModel 
}

}

Do we have any such support? Any thing one can suggest so we don't have to sprinkle null check in entire code base.

@chaowlert
Copy link
Collaborator

Hi @Shah-123,

Sorry for late reply.

This need to be fixed. Thank you for report.
Mapster already support mapping null value. But there is bug when we infer type of source object.
To workaround, specifying both sourceType and destinationType will solve the problem.

_adapter.Adapt<SourceType, DestinationType>(user);

With above configuration, when user is null, function will return null instead of error.

@chaowlert chaowlert added the bug label Apr 26, 2016
@tjs-shah
Copy link
Author

Hi !!
When do we expect next release of Mapster?
Thanks,
Tejas

  From: chaowlert <[email protected]>

To: eswann/Mapster [email protected]
Cc: shah-123 [email protected]; Mention [email protected]
Sent: Tuesday, April 26, 2016 10:12 AM
Subject: Re: [eswann/Mapster] Handling Null source objects (#60)

Hi @Shah-123,Sorry for late reply.This need to be fixed. Thank you for report.
Mapster already support mapping null value. But there is bug when we infer type of source object.
To workaround, specifying both sourceType and destinationType will solve the problem._adapter.Adapt<SourceType, DestinationType>(user);
With above configuration, when user is null, function will return null instead of error.—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub

@chaowlert
Copy link
Collaborator

I have no plan yet. Please specify both source type and destination type to solve problem for now.

kellyethridge pushed a commit to kellyethridge/Mapster that referenced this issue Oct 1, 2016
@brgrz
Copy link

brgrz commented Dec 11, 2024

@chaowlert

With above configuration, when user is null, function will return null instead of error.

Mapster just silently ignores null references for example

.Map(dest => dest.PostName, src => src.Postcode.Name)

if Postcode is null, it sets destination property as null and all is good.

But what if we want to throw an exception when dealing with null references? Can't seem to find that configuration option anywhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants